Skip to content

Commit cd02b1d

Browse files
authored
fix flaky tests and fix passDefaultLocalCheck (#3367)
* fix test case SentinelDubboConsumerFilterTest#testDegradeSync * When test is run slow, count bucket will count on next time span, causing failed test. * dos2unix ParamFlowDefaultCheckerTest.java * fix testParamFlowDefaultCheckSingleValueCheckQpsMultipleThreads by rule.setDurationInSec(2) * set threshold as count in 2 seconds to prevent the failure of the unit test when the unit test runs longer than 1 second. * fix quarkus test by set /txt sleep 300 * If /txt sleep 500 ms, in testSentinelJaxRsQuarkusAdapter, may cause 2 request intervals of more than 1 s, which cause rate limit policy is not effective. * fix testDegradeAsync * When test is run slow, count bucket will count on next time span, causing failed test. * use testcontainers to fix testConsulDataSourceWhenInit * Project embedded-consul has been deprecated in favour of org.testcontainers:consul * use consul testcontainers to fix testConsulDataSourceWhenInit, which means docker is required to run tests. ``` Error: com.alibaba.csp.sentinel.datasource.consul.ConsulDataSourceTest.testConsulDataSourceWhenInit -- Time elapsed: 34.47 s <<< ERROR! com.pszymczyk.consul.EmbeddedConsulException: Could not start Consul process in 30 seconds at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:72) ``` * introduce intermediate node to avoid ABA problem
1 parent befdc56 commit cd02b1d

File tree

18 files changed

+650
-547
lines changed

18 files changed

+650
-547
lines changed

sentinel-adapter/sentinel-apache-dubbo-adapter/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
<dependency>
4040
<groupId>org.mockito</groupId>
41-
<artifactId>mockito-core</artifactId>
41+
<artifactId>mockito-inline</artifactId>
4242
<scope>test</scope>
4343
</dependency>
4444
<dependency>

sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/BaseTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 1999-2018 Alibaba Group Holding Ltd.
2+
* Copyright 1999-2024 Alibaba Group Holding Ltd.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616
package com.alibaba.csp.sentinel;
1717

18+
import com.alibaba.csp.sentinel.adapter.dubbo.AbstractTimeBasedTest;
1819
import com.alibaba.csp.sentinel.adapter.dubbo.config.DubboAdapterGlobalConfig;
1920
import com.alibaba.csp.sentinel.adapter.dubbo.fallback.DefaultDubboFallback;
2021
import com.alibaba.csp.sentinel.config.SentinelConfig;
@@ -37,7 +38,7 @@
3738
* @author cdfive
3839
* @author lianglin
3940
*/
40-
public class BaseTest {
41+
public class BaseTest extends AbstractTimeBasedTest {
4142

4243

4344
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 1999-2024 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.adapter.dubbo;
17+
18+
import com.alibaba.csp.sentinel.util.TimeUtil;
19+
import org.mockito.MockedStatic;
20+
import org.mockito.Mockito;
21+
22+
public abstract class AbstractTimeBasedTest {
23+
24+
private long currentMillis = 0;
25+
26+
public MockedStatic<TimeUtil> mockTimeUtil() {
27+
MockedStatic<TimeUtil> mocked = Mockito.mockStatic(TimeUtil.class);
28+
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
29+
return mocked;
30+
}
31+
32+
protected final void useActualTime(MockedStatic<TimeUtil> mocked) {
33+
mocked.when(TimeUtil::currentTimeMillis).thenCallRealMethod();
34+
}
35+
36+
protected final void setCurrentMillis(MockedStatic<TimeUtil> mocked, long cur) {
37+
currentMillis = cur;
38+
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
39+
}
40+
41+
protected final void sleep(MockedStatic<TimeUtil> mocked, long timeInMs) {
42+
currentMillis += timeInMs;
43+
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
44+
}
45+
46+
protected final void sleepSecond(MockedStatic<TimeUtil> mocked, long timeSec) {
47+
sleep(mocked, timeSec * 1000);
48+
}
49+
}

sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilterTest.java

+52-43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 1999-2018 Alibaba Group Holding Ltd.
2+
* Copyright 1999-2024 Alibaba Group Holding Ltd.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,11 +36,15 @@
3636
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
3737
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
3838

39+
import com.alibaba.csp.sentinel.util.TimeUtil;
3940
import org.apache.dubbo.rpc.*;
4041
import org.apache.dubbo.rpc.support.RpcUtils;
4142
import org.junit.After;
4243
import org.junit.Before;
4344
import org.junit.Test;
45+
import org.junit.runner.RunWith;
46+
import org.mockito.MockedStatic;
47+
import org.mockito.junit.MockitoJUnitRunner;
4448

4549
import java.util.*;
4650

@@ -53,6 +57,7 @@
5357
* @author cdfive
5458
* @author lianglin
5559
*/
60+
@RunWith(MockitoJUnitRunner.class)
5661
public class SentinelDubboConsumerFilterTest extends BaseTest {
5762

5863
private final SentinelDubboConsumerFilter consumerFilter = new SentinelDubboConsumerFilter();
@@ -94,62 +99,68 @@ public void testInterfaceLevelFollowControlAsync() throws InterruptedException {
9499

95100
@Test
96101
public void testDegradeAsync() throws InterruptedException {
102+
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
103+
setCurrentMillis(mocked, 1740000000000L);
97104

98-
Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
99-
Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
100-
101-
when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
102-
initDegradeRule(DubboUtils.getInterfaceName(invoker));
105+
Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
106+
Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
103107

104-
Result result = invokeDubboRpc(false, invoker, invocation);
105-
verifyInvocationStructureForCallFinish(invoker, invocation);
106-
assertEquals("normal", result.getValue());
108+
when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
109+
initDegradeRule(DubboUtils.getInterfaceName(invoker));
107110

108-
// inc the clusterNode's exception to trigger the fallback
109-
for (int i = 0; i < 5; i++) {
110-
invokeDubboRpc(true, invoker, invocation);
111+
Result result = invokeDubboRpc(false, invoker, invocation);
111112
verifyInvocationStructureForCallFinish(invoker, invocation);
112-
}
113+
assertEquals("normal", result.getValue());
113114

114-
Result result2 = invokeDubboRpc(false, invoker, invocation);
115-
assertEquals("fallback", result2.getValue());
115+
// inc the clusterNode's exception to trigger the fallback
116+
for (int i = 0; i < 5; i++) {
117+
invokeDubboRpc(true, invoker, invocation);
118+
verifyInvocationStructureForCallFinish(invoker, invocation);
119+
}
116120

117-
// sleeping 1000 ms to reset exception
118-
Thread.sleep(1000);
119-
Result result3 = invokeDubboRpc(false, invoker, invocation);
120-
assertEquals("normal", result3.getValue());
121+
Result result2 = invokeDubboRpc(false, invoker, invocation);
122+
assertEquals("fallback", result2.getValue());
121123

122-
Context context = ContextUtil.getContext();
123-
assertNull(context);
124+
// sleeping 1000 ms to reset exception
125+
sleep(mocked, 1000);
126+
Result result3 = invokeDubboRpc(false, invoker, invocation);
127+
assertEquals("normal", result3.getValue());
128+
129+
Context context = ContextUtil.getContext();
130+
assertNull(context);
131+
}
124132
}
125133

126134
@Test
127-
public void testDegradeSync() throws InterruptedException {
135+
public void testDegradeSync() {
136+
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
137+
setCurrentMillis(mocked, 1740000000000L);
128138

129-
Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
130-
Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
131-
initDegradeRule(DubboUtils.getInterfaceName(invoker));
132-
133-
Result result = invokeDubboRpc(false, invoker, invocation);
134-
verifyInvocationStructureForCallFinish(invoker, invocation);
135-
assertEquals("normal", result.getValue());
139+
Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
140+
Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
141+
initDegradeRule(DubboUtils.getInterfaceName(invoker));
136142

137-
// inc the clusterNode's exception to trigger the fallback
138-
for (int i = 0; i < 5; i++) {
139-
invokeDubboRpc(true, invoker, invocation);
143+
Result result = invokeDubboRpc(false, invoker, invocation);
140144
verifyInvocationStructureForCallFinish(invoker, invocation);
141-
}
145+
assertEquals("normal", result.getValue());
142146

143-
Result result2 = invokeDubboRpc(false, invoker, invocation);
144-
assertEquals("fallback", result2.getValue());
147+
// inc the clusterNode's exception to trigger the fallback
148+
for (int i = 0; i < 5; i++) {
149+
invokeDubboRpc(true, invoker, invocation);
150+
verifyInvocationStructureForCallFinish(invoker, invocation);
151+
}
145152

146-
// sleeping 1000 ms to reset exception
147-
Thread.sleep(1000);
148-
Result result3 = invokeDubboRpc(false, invoker, invocation);
149-
assertEquals("normal", result3.getValue());
153+
Result result2 = invokeDubboRpc(false, invoker, invocation);
154+
assertEquals("fallback", result2.getValue());
150155

151-
Context context = ContextUtil.getContext();
152-
assertNull(context);
156+
// sleeping 1000 ms to reset exception
157+
sleep(mocked, 1000);
158+
Result result3 = invokeDubboRpc(false, invoker, invocation);
159+
assertEquals("normal", result3.getValue());
160+
161+
Context context = ContextUtil.getContext();
162+
assertNull(context);
163+
}
153164
}
154165

155166
@Test
@@ -183,7 +194,6 @@ public void testInvokeAsync() {
183194

184195
when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
185196
final Result result = mock(Result.class);
186-
when(result.hasException()).thenReturn(false);
187197
when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
188198
verifyInvocationStructureForAsyncCall(invoker, invocation);
189199
return result;
@@ -203,7 +213,6 @@ public void testInvokeSync() {
203213

204214
final Result result = mock(Result.class);
205215
when(result.hasException()).thenReturn(false);
206-
when(result.getException()).thenReturn(new Exception());
207216
when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
208217
verifyInvocationStructure(invoker, invocation);
209218
return result;

sentinel-adapter/sentinel-apache-dubbo3-adapter/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
<dependency>
4040
<groupId>org.mockito</groupId>
41-
<artifactId>mockito-core</artifactId>
41+
<artifactId>mockito-inline</artifactId>
4242
<scope>test</scope>
4343
</dependency>
4444
<dependency>

sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/BaseTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 1999-2018 Alibaba Group Holding Ltd.
2+
* Copyright 1999-2024 Alibaba Group Holding Ltd.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616
package com.alibaba.csp.sentinel;
1717

18+
import com.alibaba.csp.sentinel.adapter.dubbo3.AbstractTimeBasedTest;
1819
import com.alibaba.csp.sentinel.adapter.dubbo3.config.DubboAdapterGlobalConfig;
1920
import com.alibaba.csp.sentinel.adapter.dubbo3.fallback.DefaultDubboFallback;
2021
import com.alibaba.csp.sentinel.config.SentinelConfig;
@@ -37,7 +38,7 @@
3738
* @author cdfive
3839
* @author lianglin
3940
*/
40-
public class BaseTest {
41+
public class BaseTest extends AbstractTimeBasedTest {
4142

4243

4344
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 1999-2024 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.adapter.dubbo3;
17+
18+
import com.alibaba.csp.sentinel.util.TimeUtil;
19+
import org.mockito.MockedStatic;
20+
import org.mockito.Mockito;
21+
22+
public abstract class AbstractTimeBasedTest {
23+
24+
private long currentMillis = 0;
25+
26+
public MockedStatic<TimeUtil> mockTimeUtil() {
27+
MockedStatic<TimeUtil> mocked = Mockito.mockStatic(TimeUtil.class);
28+
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
29+
return mocked;
30+
}
31+
32+
protected final void useActualTime(MockedStatic<TimeUtil> mocked) {
33+
mocked.when(TimeUtil::currentTimeMillis).thenCallRealMethod();
34+
}
35+
36+
protected final void setCurrentMillis(MockedStatic<TimeUtil> mocked, long cur) {
37+
currentMillis = cur;
38+
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
39+
}
40+
41+
protected final void sleep(MockedStatic<TimeUtil> mocked, long timeInMs) {
42+
currentMillis += timeInMs;
43+
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
44+
}
45+
46+
protected final void sleepSecond(MockedStatic<TimeUtil> mocked, long timeSec) {
47+
sleep(mocked, timeSec * 1000);
48+
}
49+
}

0 commit comments

Comments
 (0)