fix flaky tests (#3364)

* fix ParamFlowPartialIntegrationTest

mock TimeUtil to avoid time changed, and bucket moved to next.

* remove csp.sentinel.log.output.type

remove sysprop csp.sentinel.log.output.type in TimeUtilTest,
to avoid a few outputs.

* try fix testConcurrentAcquireAndRelease

* remove stdout

remove sout in ParamFlowThrottleRateLimitingCheckerTest,
to avoid a few outputs.

* ConcurrentClusterFlowCheckerTest,djust the flowId to prevent conflicts

* CurrentConcurrencyManagerTest,adjust the flowId to prevent conflicts

* update year
This commit is contained in:
Robert Lu 2024-03-25 10:09:16 +08:00 committed by GitHub
parent 24c93c8a1a
commit befdc56885
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 53 additions and 55 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.CurrentConcurr
import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.TokenCacheNodeManager;
import com.alibaba.csp.sentinel.cluster.server.connection.ConnectionManager;
import com.alibaba.csp.sentinel.cluster.server.AbstractTimeBasedTest;
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
import com.alibaba.csp.sentinel.slots.block.ClusterRuleConstant;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig;
@ -47,7 +48,7 @@ public class ConcurrentClusterFlowCheckerTest extends AbstractTimeBasedTest {
ClusterFlowConfig config = new ClusterFlowConfig();
config.setResourceTimeout(500);
config.setClientOfflineTime(1000);
config.setFlowId(111L);
config.setFlowId(179L);
config.setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL);
rule.setClusterConfig(config);
rule.setClusterMode(true);
@ -64,7 +65,7 @@ public class ConcurrentClusterFlowCheckerTest extends AbstractTimeBasedTest {
public void testEasyAcquireAndRelease() throws InterruptedException {
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
setCurrentMillis(mocked, System.currentTimeMillis());
FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(111L);
FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(179L);
ArrayList<Long> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
TokenResult result = ConcurrentClusterFlowChecker.acquireConcurrentToken("127.0.0.1", rule, 1);
@ -83,7 +84,7 @@ public class ConcurrentClusterFlowCheckerTest extends AbstractTimeBasedTest {
result.getStatus() == TokenResultStatus.RELEASE_OK);
}
Assert.assertTrue("fail to release token",
CurrentConcurrencyManager.get(111L).get() == 0 && TokenCacheNodeManager.getSize() == 0);
CurrentConcurrencyManager.get(179L).get() == 0 && TokenCacheNodeManager.getSize() == 0);
}
}
@ -91,22 +92,22 @@ public class ConcurrentClusterFlowCheckerTest extends AbstractTimeBasedTest {
public void testConcurrentAcquireAndRelease() throws InterruptedException {
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
setCurrentMillis(mocked, System.currentTimeMillis());
final FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(111L);
final FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(179L);
final CountDownLatch countDownLatch = new CountDownLatch(1000);
ExecutorService pool = Executors.newFixedThreadPool(100);
ExecutorService pool = Executors.newFixedThreadPool(100,
new NamedThreadFactory("ConcurrentClusterFlowCheckerTest", true)
);
for (long i = 0; i < 1000; i++) {
Runnable task = new Runnable() {
@Override
public void run() {
assert rule != null;
Runnable task = () -> {
Assert.assertNotNull(rule);
TokenResult result = ConcurrentClusterFlowChecker.acquireConcurrentToken("127.0.0.1", rule, 1);
Assert.assertTrue("concurrent control fail", CurrentConcurrencyManager.get(111L).get() <= rule.getCount());
String msg = String.format("concurrent control fail %s<%s", CurrentConcurrencyManager.get(179L).get(), rule.getCount());
Assert.assertTrue(msg, CurrentConcurrencyManager.get(179L).get() <= rule.getCount());
if (result.getStatus() == TokenResultStatus.OK) {
ConcurrentClusterFlowChecker.releaseConcurrentToken(result.getTokenId());
}
countDownLatch.countDown();
}
};
pool.execute(task);
}
@ -121,7 +122,7 @@ public class ConcurrentClusterFlowCheckerTest extends AbstractTimeBasedTest {
@Test
public void testReleaseExpiredToken() throws InterruptedException {
ConnectionManager.addConnection("test", "127.0.0.1");
FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(111L);
FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(179L);
for (int i = 0; i < 10; i++) {
ConcurrentClusterFlowChecker.acquireConcurrentToken("127.0.0.1", rule, 1);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,7 +15,7 @@
*/
package com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent;
import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.CurrentConcurrencyManager;
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
import org.junit.Assert;
import org.junit.Test;
@ -26,24 +26,23 @@ import java.util.concurrent.Executors;
public class CurrentConcurrencyManagerTest {
@Test
public void updateTest() throws InterruptedException {
CurrentConcurrencyManager.put(111L, 0);
CurrentConcurrencyManager.put(222L, 0);
CurrentConcurrencyManager.put(113L, 0);
CurrentConcurrencyManager.put(223L, 0);
final CountDownLatch countDownLatch = new CountDownLatch(1000);
ExecutorService pool = Executors.newFixedThreadPool(100);
ExecutorService pool = Executors.newFixedThreadPool(100,
new NamedThreadFactory("CurrentConcurrencyManagerTest", true)
);
for (int i = 0; i < 1000; i++) {
Runnable task = new Runnable() {
@Override
public void run() {
CurrentConcurrencyManager.addConcurrency(111L, 1);
CurrentConcurrencyManager.addConcurrency(222L, 2);
Runnable task = () -> {
CurrentConcurrencyManager.addConcurrency(113L, 1);
CurrentConcurrencyManager.addConcurrency(223L, 2);
countDownLatch.countDown();
}
};
pool.execute(task);
}
countDownLatch.await();
pool.shutdown();
Assert.assertEquals(1000, CurrentConcurrencyManager.get(111L).get());
Assert.assertEquals(2000, CurrentConcurrencyManager.get(222L).get());
Assert.assertEquals(1000, CurrentConcurrencyManager.get(113L).get());
Assert.assertEquals(2000, CurrentConcurrencyManager.get(223L).get());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,10 +32,6 @@ import com.alibaba.csp.sentinel.util.function.Tuple2;
*
*/
public class TimeUtilTest {
@Before
public void initLogging() {
System.setProperty("csp.sentinel.log.output.type", "console");
}
private void waitFor(int step, int seconds) throws InterruptedException {
for (int i = 0; i < seconds; i ++) {

View File

@ -3,10 +3,13 @@ package com.alibaba.csp.sentinel.slots.block.flow.param;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.block.flow.param.AbstractTimeBasedTest;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.TimeUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import java.util.ArrayList;
import java.util.Collections;
@ -18,7 +21,7 @@ import static org.junit.Assert.assertTrue;
* @author quguai
* @date 2023/10/27 13:44
*/
public class ParamFlowPartialIntegrationTest {
public class ParamFlowPartialIntegrationTest extends AbstractTimeBasedTest {
@Before
public void setUp() throws Exception {
@ -32,6 +35,8 @@ public class ParamFlowPartialIntegrationTest {
@Test
public void testParamFlowRegex() {
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
setCurrentMillis(mocked, 1800000000000L);
ParamFlowRule rule = new ParamFlowRule(".*")
.setParamIdx(0)
.setCount(1);
@ -49,6 +54,7 @@ public class ParamFlowPartialIntegrationTest {
verifyFlow("testParamFlowRegex_2", false, "args");
verifyFlow("testParamFlowRegex_2", false, "args_1");
}
}
private void verifyFlow(String resource, boolean shouldPass, String... args) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -70,7 +70,6 @@ public class ParamFlowThrottleRateLimitingCheckerTest {
}
assertEquals(successCount, threshold);
System.out.println("testSingleValueThrottleCheckQps: sleep for 3 seconds");
TimeUnit.SECONDS.sleep(3);
currentTime = TimeUtil.currentTimeMillis();
@ -104,7 +103,6 @@ public class ParamFlowThrottleRateLimitingCheckerTest {
metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
int threadCount = 40;
System.out.println(metric.getRuleTimeCounter(rule));
final CountDownLatch waitLatch = new CountDownLatch(threadCount);
final AtomicInteger successCount = new AtomicInteger();
@ -125,10 +123,8 @@ public class ParamFlowThrottleRateLimitingCheckerTest {
waitLatch.await();
assertEquals(successCount.get(), 1);
System.out.println(threadCount);
successCount.set(0);
System.out.println("testSingleValueThrottleCheckQpsMultipleThreads: sleep for 3 seconds");
TimeUnit.SECONDS.sleep(3);
successCount.set(0);