diff --git a/sentinel-adapter/sentinel-apache-dubbo-adapter/pom.xml b/sentinel-adapter/sentinel-apache-dubbo-adapter/pom.xml
index 356cae7c..97aa419b 100644
--- a/sentinel-adapter/sentinel-apache-dubbo-adapter/pom.xml
+++ b/sentinel-adapter/sentinel-apache-dubbo-adapter/pom.xml
@@ -38,7 +38,7 @@
org.mockito
- mockito-core
+ mockito-inline
test
diff --git a/sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/BaseTest.java b/sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/BaseTest.java
index db78c366..c7f843f1 100644
--- a/sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/BaseTest.java
+++ b/sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/BaseTest.java
@@ -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,6 +15,7 @@
*/
package com.alibaba.csp.sentinel;
+import com.alibaba.csp.sentinel.adapter.dubbo.AbstractTimeBasedTest;
import com.alibaba.csp.sentinel.adapter.dubbo.config.DubboAdapterGlobalConfig;
import com.alibaba.csp.sentinel.adapter.dubbo.fallback.DefaultDubboFallback;
import com.alibaba.csp.sentinel.config.SentinelConfig;
@@ -37,7 +38,7 @@ import java.util.ArrayList;
* @author cdfive
* @author lianglin
*/
-public class BaseTest {
+public class BaseTest extends AbstractTimeBasedTest {
/**
diff --git a/sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo/AbstractTimeBasedTest.java b/sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo/AbstractTimeBasedTest.java
new file mode 100644
index 00000000..d23f8002
--- /dev/null
+++ b/sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo/AbstractTimeBasedTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alibaba.csp.sentinel.adapter.dubbo;
+
+import com.alibaba.csp.sentinel.util.TimeUtil;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+public abstract class AbstractTimeBasedTest {
+
+ private long currentMillis = 0;
+
+ public MockedStatic mockTimeUtil() {
+ MockedStatic mocked = Mockito.mockStatic(TimeUtil.class);
+ mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
+ return mocked;
+ }
+
+ protected final void useActualTime(MockedStatic mocked) {
+ mocked.when(TimeUtil::currentTimeMillis).thenCallRealMethod();
+ }
+
+ protected final void setCurrentMillis(MockedStatic mocked, long cur) {
+ currentMillis = cur;
+ mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
+ }
+
+ protected final void sleep(MockedStatic mocked, long timeInMs) {
+ currentMillis += timeInMs;
+ mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
+ }
+
+ protected final void sleepSecond(MockedStatic mocked, long timeSec) {
+ sleep(mocked, timeSec * 1000);
+ }
+}
diff --git a/sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilterTest.java b/sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilterTest.java
index a350a47d..4bebcbcc 100644
--- a/sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilterTest.java
+++ b/sentinel-adapter/sentinel-apache-dubbo-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilterTest.java
@@ -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.
@@ -36,11 +36,15 @@ import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
+import com.alibaba.csp.sentinel.util.TimeUtil;
import org.apache.dubbo.rpc.*;
import org.apache.dubbo.rpc.support.RpcUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockedStatic;
+import org.mockito.junit.MockitoJUnitRunner;
import java.util.*;
@@ -53,6 +57,7 @@ import static org.mockito.Mockito.*;
* @author cdfive
* @author lianglin
*/
+@RunWith(MockitoJUnitRunner.class)
public class SentinelDubboConsumerFilterTest extends BaseTest {
private final SentinelDubboConsumerFilter consumerFilter = new SentinelDubboConsumerFilter();
@@ -94,62 +99,68 @@ public class SentinelDubboConsumerFilterTest extends BaseTest {
@Test
public void testDegradeAsync() throws InterruptedException {
+ try (MockedStatic mocked = super.mockTimeUtil()) {
+ setCurrentMillis(mocked, 1740000000000L);
- Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
- Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
+ Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
+ Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
- when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
- initDegradeRule(DubboUtils.getInterfaceName(invoker));
+ when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
+ initDegradeRule(DubboUtils.getInterfaceName(invoker));
- Result result = invokeDubboRpc(false, invoker, invocation);
- verifyInvocationStructureForCallFinish(invoker, invocation);
- assertEquals("normal", result.getValue());
-
- // inc the clusterNode's exception to trigger the fallback
- for (int i = 0; i < 5; i++) {
- invokeDubboRpc(true, invoker, invocation);
+ Result result = invokeDubboRpc(false, invoker, invocation);
verifyInvocationStructureForCallFinish(invoker, invocation);
+ assertEquals("normal", result.getValue());
+
+ // inc the clusterNode's exception to trigger the fallback
+ for (int i = 0; i < 5; i++) {
+ invokeDubboRpc(true, invoker, invocation);
+ verifyInvocationStructureForCallFinish(invoker, invocation);
+ }
+
+ Result result2 = invokeDubboRpc(false, invoker, invocation);
+ assertEquals("fallback", result2.getValue());
+
+ // sleeping 1000 ms to reset exception
+ sleep(mocked, 1000);
+ Result result3 = invokeDubboRpc(false, invoker, invocation);
+ assertEquals("normal", result3.getValue());
+
+ Context context = ContextUtil.getContext();
+ assertNull(context);
}
-
- Result result2 = invokeDubboRpc(false, invoker, invocation);
- assertEquals("fallback", result2.getValue());
-
- // sleeping 1000 ms to reset exception
- Thread.sleep(1000);
- Result result3 = invokeDubboRpc(false, invoker, invocation);
- assertEquals("normal", result3.getValue());
-
- Context context = ContextUtil.getContext();
- assertNull(context);
}
@Test
- public void testDegradeSync() throws InterruptedException {
+ public void testDegradeSync() {
+ try (MockedStatic mocked = super.mockTimeUtil()) {
+ setCurrentMillis(mocked, 1740000000000L);
- Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
- Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
- initDegradeRule(DubboUtils.getInterfaceName(invoker));
+ Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
+ Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
+ initDegradeRule(DubboUtils.getInterfaceName(invoker));
- Result result = invokeDubboRpc(false, invoker, invocation);
- verifyInvocationStructureForCallFinish(invoker, invocation);
- assertEquals("normal", result.getValue());
-
- // inc the clusterNode's exception to trigger the fallback
- for (int i = 0; i < 5; i++) {
- invokeDubboRpc(true, invoker, invocation);
+ Result result = invokeDubboRpc(false, invoker, invocation);
verifyInvocationStructureForCallFinish(invoker, invocation);
+ assertEquals("normal", result.getValue());
+
+ // inc the clusterNode's exception to trigger the fallback
+ for (int i = 0; i < 5; i++) {
+ invokeDubboRpc(true, invoker, invocation);
+ verifyInvocationStructureForCallFinish(invoker, invocation);
+ }
+
+ Result result2 = invokeDubboRpc(false, invoker, invocation);
+ assertEquals("fallback", result2.getValue());
+
+ // sleeping 1000 ms to reset exception
+ sleep(mocked, 1000);
+ Result result3 = invokeDubboRpc(false, invoker, invocation);
+ assertEquals("normal", result3.getValue());
+
+ Context context = ContextUtil.getContext();
+ assertNull(context);
}
-
- Result result2 = invokeDubboRpc(false, invoker, invocation);
- assertEquals("fallback", result2.getValue());
-
- // sleeping 1000 ms to reset exception
- Thread.sleep(1000);
- Result result3 = invokeDubboRpc(false, invoker, invocation);
- assertEquals("normal", result3.getValue());
-
- Context context = ContextUtil.getContext();
- assertNull(context);
}
@Test
@@ -183,7 +194,6 @@ public class SentinelDubboConsumerFilterTest extends BaseTest {
when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
final Result result = mock(Result.class);
- when(result.hasException()).thenReturn(false);
when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
verifyInvocationStructureForAsyncCall(invoker, invocation);
return result;
@@ -203,7 +213,6 @@ public class SentinelDubboConsumerFilterTest extends BaseTest {
final Result result = mock(Result.class);
when(result.hasException()).thenReturn(false);
- when(result.getException()).thenReturn(new Exception());
when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
verifyInvocationStructure(invoker, invocation);
return result;
diff --git a/sentinel-adapter/sentinel-apache-dubbo3-adapter/pom.xml b/sentinel-adapter/sentinel-apache-dubbo3-adapter/pom.xml
index 6752376c..af797414 100644
--- a/sentinel-adapter/sentinel-apache-dubbo3-adapter/pom.xml
+++ b/sentinel-adapter/sentinel-apache-dubbo3-adapter/pom.xml
@@ -38,7 +38,7 @@
org.mockito
- mockito-core
+ mockito-inline
test
diff --git a/sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/BaseTest.java b/sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/BaseTest.java
index e4642cc9..4e19aef2 100644
--- a/sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/BaseTest.java
+++ b/sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/BaseTest.java
@@ -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,6 +15,7 @@
*/
package com.alibaba.csp.sentinel;
+import com.alibaba.csp.sentinel.adapter.dubbo3.AbstractTimeBasedTest;
import com.alibaba.csp.sentinel.adapter.dubbo3.config.DubboAdapterGlobalConfig;
import com.alibaba.csp.sentinel.adapter.dubbo3.fallback.DefaultDubboFallback;
import com.alibaba.csp.sentinel.config.SentinelConfig;
@@ -37,7 +38,7 @@ import java.util.ArrayList;
* @author cdfive
* @author lianglin
*/
-public class BaseTest {
+public class BaseTest extends AbstractTimeBasedTest {
/**
diff --git a/sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo3/AbstractTimeBasedTest.java b/sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo3/AbstractTimeBasedTest.java
new file mode 100644
index 00000000..d2a0c421
--- /dev/null
+++ b/sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo3/AbstractTimeBasedTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alibaba.csp.sentinel.adapter.dubbo3;
+
+import com.alibaba.csp.sentinel.util.TimeUtil;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+public abstract class AbstractTimeBasedTest {
+
+ private long currentMillis = 0;
+
+ public MockedStatic mockTimeUtil() {
+ MockedStatic mocked = Mockito.mockStatic(TimeUtil.class);
+ mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
+ return mocked;
+ }
+
+ protected final void useActualTime(MockedStatic mocked) {
+ mocked.when(TimeUtil::currentTimeMillis).thenCallRealMethod();
+ }
+
+ protected final void setCurrentMillis(MockedStatic mocked, long cur) {
+ currentMillis = cur;
+ mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
+ }
+
+ protected final void sleep(MockedStatic mocked, long timeInMs) {
+ currentMillis += timeInMs;
+ mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
+ }
+
+ protected final void sleepSecond(MockedStatic mocked, long timeSec) {
+ sleep(mocked, timeSec * 1000);
+ }
+}
diff --git a/sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo3/SentinelDubboConsumerFilterTest.java b/sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo3/SentinelDubboConsumerFilterTest.java
index a91d67d4..b2cd294f 100644
--- a/sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo3/SentinelDubboConsumerFilterTest.java
+++ b/sentinel-adapter/sentinel-apache-dubbo3-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/dubbo3/SentinelDubboConsumerFilterTest.java
@@ -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.
@@ -32,12 +32,15 @@ import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
-
+import com.alibaba.csp.sentinel.util.TimeUtil;
import org.apache.dubbo.rpc.*;
import org.apache.dubbo.rpc.support.RpcUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockedStatic;
+import org.mockito.junit.MockitoJUnitRunner;
import java.util.*;
@@ -50,6 +53,7 @@ import static org.mockito.Mockito.*;
* @author cdfive
* @author lianglin
*/
+@RunWith(MockitoJUnitRunner.class)
public class SentinelDubboConsumerFilterTest extends BaseTest {
private final SentinelDubboConsumerFilter consumerFilter = new SentinelDubboConsumerFilter();
@@ -91,62 +95,67 @@ public class SentinelDubboConsumerFilterTest extends BaseTest {
@Test
public void testDegradeAsync() throws InterruptedException {
+ try (MockedStatic mocked = super.mockTimeUtil()) {
+ setCurrentMillis(mocked, 1740000000000L);
+ Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
+ Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
- Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
- Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
+ when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
+ initDegradeRule(DubboUtils.getInterfaceName(invoker));
- when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
- initDegradeRule(DubboUtils.getInterfaceName(invoker));
-
- Result result = invokeDubboRpc(false, invoker, invocation);
- verifyInvocationStructureForCallFinish(invoker, invocation);
- assertEquals("normal", result.getValue());
-
- // inc the clusterNode's exception to trigger the fallback
- for (int i = 0; i < 5; i++) {
- invokeDubboRpc(true, invoker, invocation);
+ Result result = invokeDubboRpc(false, invoker, invocation);
verifyInvocationStructureForCallFinish(invoker, invocation);
+ assertEquals("normal", result.getValue());
+
+ // inc the clusterNode's exception to trigger the fallback
+ for (int i = 0; i < 5; i++) {
+ invokeDubboRpc(true, invoker, invocation);
+ verifyInvocationStructureForCallFinish(invoker, invocation);
+ }
+
+ Result result2 = invokeDubboRpc(false, invoker, invocation);
+ assertEquals("fallback", result2.getValue());
+
+ // sleeping 1000 ms to reset exception
+ sleep(mocked, 1000);
+ Result result3 = invokeDubboRpc(false, invoker, invocation);
+ assertEquals("normal", result3.getValue());
+
+ Context context = ContextUtil.getContext();
+ assertNull(context);
}
-
- Result result2 = invokeDubboRpc(false, invoker, invocation);
- assertEquals("fallback", result2.getValue());
-
- // sleeping 1000 ms to reset exception
- Thread.sleep(1000);
- Result result3 = invokeDubboRpc(false, invoker, invocation);
- assertEquals("normal", result3.getValue());
-
- Context context = ContextUtil.getContext();
- assertNull(context);
}
@Test
- public void testDegradeSync() throws InterruptedException {
+ public void testDegradeSync() {
+ try (MockedStatic mocked = super.mockTimeUtil()) {
+ setCurrentMillis(mocked, 1750000000000L);
- Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
- Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
- initDegradeRule(DubboUtils.getInterfaceName(invoker));
+ Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
+ Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
+ initDegradeRule(DubboUtils.getInterfaceName(invoker));
- Result result = invokeDubboRpc(false, invoker, invocation);
- verifyInvocationStructureForCallFinish(invoker, invocation);
- assertEquals("normal", result.getValue());
-
- // inc the clusterNode's exception to trigger the fallback
- for (int i = 0; i < 5; i++) {
- invokeDubboRpc(true, invoker, invocation);
+ Result result = invokeDubboRpc(false, invoker, invocation);
verifyInvocationStructureForCallFinish(invoker, invocation);
+ assertEquals("normal", result.getValue());
+
+ // inc the clusterNode's exception to trigger the fallback
+ for (int i = 0; i < 5; i++) {
+ invokeDubboRpc(true, invoker, invocation);
+ verifyInvocationStructureForCallFinish(invoker, invocation);
+ }
+
+ Result result2 = invokeDubboRpc(false, invoker, invocation);
+ assertEquals("fallback", result2.getValue());
+
+ // sleeping 1000 ms to reset exception
+ sleep(mocked, 1000);
+ Result result3 = invokeDubboRpc(false, invoker, invocation);
+ assertEquals("normal", result3.getValue());
+
+ Context context = ContextUtil.getContext();
+ assertNull(context);
}
-
- Result result2 = invokeDubboRpc(false, invoker, invocation);
- assertEquals("fallback", result2.getValue());
-
- // sleeping 1000 ms to reset exception
- Thread.sleep(1000);
- Result result3 = invokeDubboRpc(false, invoker, invocation);
- assertEquals("normal", result3.getValue());
-
- Context context = ContextUtil.getContext();
- assertNull(context);
}
@Test
@@ -180,7 +189,6 @@ public class SentinelDubboConsumerFilterTest extends BaseTest {
when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
final Result result = mock(Result.class);
- when(result.hasException()).thenReturn(false);
when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
verifyInvocationStructureForAsyncCall(invoker, invocation);
return result;
@@ -200,7 +208,6 @@ public class SentinelDubboConsumerFilterTest extends BaseTest {
final Result result = mock(Result.class);
when(result.hasException()).thenReturn(false);
- when(result.getException()).thenReturn(new Exception());
when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
verifyInvocationStructure(invoker, invocation);
return result;
@@ -263,7 +270,7 @@ public class SentinelDubboConsumerFilterTest extends BaseTest {
ClusterNode methodClusterNode = methodNode.getClusterNode();
ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
assertNotSame(methodClusterNode,
- interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode
+ interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode
// As context origin is "", the StatisticNode should not be created in originCountMap of ClusterNode
Map methodOriginCountMap = methodClusterNode.getOriginCountMap();
@@ -315,7 +322,7 @@ public class SentinelDubboConsumerFilterTest extends BaseTest {
ClusterNode methodClusterNode = methodNode.getClusterNode();
ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
assertNotSame(methodClusterNode,
- interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode
+ interfaceClusterNode);// Different resource->Different ProcessorSlot->Different ClusterNode
// As context origin is "", the StatisticNode should not be created in originCountMap of ClusterNode
Map methodOriginCountMap = methodClusterNode.getOriginCountMap();
@@ -360,8 +367,8 @@ public class SentinelDubboConsumerFilterTest extends BaseTest {
private void initDegradeRule(String resource) {
DegradeRule degradeRule = new DegradeRule(resource)
- .setCount(0.5)
- .setGrade(DEGRADE_GRADE_EXCEPTION_RATIO);
+ .setCount(0.5)
+ .setGrade(DEGRADE_GRADE_EXCEPTION_RATIO);
List degradeRules = new ArrayList<>();
degradeRules.add(degradeRule);
degradeRule.setTimeWindow(1);
@@ -382,7 +389,7 @@ public class SentinelDubboConsumerFilterTest extends BaseTest {
result = exception ? new AppResponse(new Exception("error")) : new AppResponse("normal");
} else {
result = exception ? AsyncRpcResult.newDefaultAsyncResult(new Exception("error"), invocation)
- : AsyncRpcResult.newDefaultAsyncResult("normal", invocation);
+ : AsyncRpcResult.newDefaultAsyncResult("normal", invocation);
}
when(invoker.invoke(invocation)).thenReturn(result);
return consumerFilter.invoke(invoker, invocation);
diff --git a/sentinel-demo/sentinel-demo-quarkus/src/main/java/com/alibaba/csp/sentinel/demo/quarkus/GreetingResource.java b/sentinel-demo/sentinel-demo-quarkus/src/main/java/com/alibaba/csp/sentinel/demo/quarkus/GreetingResource.java
index b716e530..f9deb561 100644
--- a/sentinel-demo/sentinel-demo-quarkus/src/main/java/com/alibaba/csp/sentinel/demo/quarkus/GreetingResource.java
+++ b/sentinel-demo/sentinel-demo-quarkus/src/main/java/com/alibaba/csp/sentinel/demo/quarkus/GreetingResource.java
@@ -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.
@@ -37,7 +37,7 @@ public class GreetingResource {
@Path("/txt")
@Produces(MediaType.TEXT_PLAIN)
public String hello() throws InterruptedException {
- TimeUnit.MILLISECONDS.sleep(500);
+ TimeUnit.MILLISECONDS.sleep(300);
return "hello";
}
diff --git a/sentinel-extension/sentinel-datasource-consul/pom.xml b/sentinel-extension/sentinel-datasource-consul/pom.xml
index 20e4fac1..276842a2 100644
--- a/sentinel-extension/sentinel-datasource-consul/pom.xml
+++ b/sentinel-extension/sentinel-datasource-consul/pom.xml
@@ -16,7 +16,6 @@
1.8
1.8
1.4.5
- 2.2.0
@@ -36,9 +35,9 @@
${consul.version}
- com.pszymczyk.consul
- embedded-consul
- ${consul.process.version}
+ org.testcontainers
+ consul
+ 1.19.7
test
diff --git a/sentinel-extension/sentinel-datasource-consul/src/test/java/com/alibaba/csp/sentinel/datasource/consul/ConsulDataSourceTest.java b/sentinel-extension/sentinel-datasource-consul/src/test/java/com/alibaba/csp/sentinel/datasource/consul/ConsulDataSourceTest.java
index f5b531c6..416b518d 100644
--- a/sentinel-extension/sentinel-datasource-consul/src/test/java/com/alibaba/csp/sentinel/datasource/consul/ConsulDataSourceTest.java
+++ b/sentinel-extension/sentinel-datasource-consul/src/test/java/com/alibaba/csp/sentinel/datasource/consul/ConsulDataSourceTest.java
@@ -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.
@@ -21,15 +21,10 @@ import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
-
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
-import com.pszymczyk.consul.ConsulProcess;
-import com.pszymczyk.consul.ConsulStarterBuilder;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.*;
+import org.testcontainers.consul.ConsulContainer;
import java.util.ArrayList;
import java.util.List;
@@ -40,11 +35,12 @@ import java.util.concurrent.TimeUnit;
* @author wavesZh
*/
public class ConsulDataSourceTest {
+ @ClassRule
+ public static ConsulContainer consulContainer = new ConsulContainer("hashicorp/consul:1.15");
private final String ruleKey = "sentinel.rules.flow.ruleKey";
private final int waitTimeoutInSecond = 1;
- private ConsulProcess consul;
private ConsulClient client;
private ReadableDataSource> consulDataSource;
@@ -53,11 +49,8 @@ public class ConsulDataSourceTest {
@Before
public void init() {
- this.consul = ConsulStarterBuilder.consulStarter()
- .build()
- .start();
- int port = consul.getHttpPort();
- String host = "127.0.0.1";
+ int port = consulContainer.getMappedPort(8500);
+ String host = consulContainer.getHost();
client = new ConsulClient(host, port);
Converter> flowConfigParser = buildFlowConfigParser();
String flowRulesJson =
@@ -76,9 +69,6 @@ public class ConsulDataSourceTest {
if (consulDataSource != null) {
consulDataSource.close();
}
- if (consul != null) {
- consul.close();
- }
FlowRuleManager.loadRules(new ArrayList<>());
}
diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowChecker.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowChecker.java
index bd5ed8a0..de7089b0 100644
--- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowChecker.java
+++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowChecker.java
@@ -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,16 +15,6 @@
*/
package com.alibaba.csp.sentinel.slots.block.flow.param;
-import java.lang.reflect.Array;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicLong;
-
import com.alibaba.csp.sentinel.cluster.ClusterStateManager;
import com.alibaba.csp.sentinel.cluster.TokenResult;
import com.alibaba.csp.sentinel.cluster.TokenResultStatus;
@@ -37,6 +27,13 @@ import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.statistic.cache.CacheMap;
import com.alibaba.csp.sentinel.util.TimeUtil;
+import java.lang.reflect.Array;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
+
/**
* Rule checker for parameter flow control.
*
@@ -46,7 +43,7 @@ import com.alibaba.csp.sentinel.util.TimeUtil;
public final class ParamFlowChecker {
public static boolean passCheck(ResourceWrapper resourceWrapper, /*@Valid*/ ParamFlowRule rule, /*@Valid*/ int count,
- Object... args) {
+ Object... args) {
if (args == null) {
return true;
}
@@ -79,7 +76,7 @@ public final class ParamFlowChecker {
Object value) {
try {
if (Collection.class.isAssignableFrom(value.getClass())) {
- for (Object param : ((Collection)value)) {
+ for (Object param : ((Collection) value)) {
if (!passSingleValueCheck(resourceWrapper, rule, count, param)) {
return false;
}
@@ -117,7 +114,7 @@ public final class ParamFlowChecker {
int itemThreshold = rule.getParsedHotItems().get(value);
return ++threadCount <= itemThreshold;
}
- long threshold = (long)rule.getCount();
+ long threshold = (long) rule.getCount();
return ++threadCount <= threshold;
}
@@ -127,16 +124,16 @@ public final class ParamFlowChecker {
static boolean passDefaultLocalCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int acquireCount,
Object value) {
ParameterMetric metric = getParameterMetric(resourceWrapper);
- CacheMap