Support jdk17 (#3339)
Adjust the test logic, use jdk17 to build, 8, 11, 17 to run
This commit is contained in:
parent
2b7a989e29
commit
8fef1b5eee
|
|
@ -1,16 +1,5 @@
|
|||
version: 2
|
||||
jobs:
|
||||
integration-test:
|
||||
docker:
|
||||
- image: cimg/openjdk:8.0
|
||||
working_directory: ~/sentinel
|
||||
environment:
|
||||
MAVEN_OPTS: -Xmx3200m
|
||||
steps:
|
||||
- checkout
|
||||
# Run tests
|
||||
- run: mvn integration-test
|
||||
|
||||
document-lint:
|
||||
docker:
|
||||
# this image is build from Dockerfile
|
||||
|
|
@ -49,5 +38,4 @@ workflows:
|
|||
version: 2
|
||||
ci:
|
||||
jobs:
|
||||
- integration-test
|
||||
- document-lint
|
||||
|
|
|
|||
|
|
@ -3,9 +3,7 @@ name: Sentinel CI
|
|||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- "1.8"
|
||||
- "2.0"
|
||||
- '*'
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
|
|
@ -17,20 +15,26 @@ jobs:
|
|||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
java: [8, 11]
|
||||
java: [8, 11, 17]
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v3
|
||||
- name: Setup Java for test
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: ${{ matrix.java }}
|
||||
distribution: 'temurin'
|
||||
architecture: x64
|
||||
cache: 'maven'
|
||||
|
||||
- name: Setup Java for mvn
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: 17
|
||||
distribution: 'temurin'
|
||||
|
||||
- name: Test with Maven
|
||||
run: mvn --batch-mode test
|
||||
run: mvn --batch-mode test -Dsurefire.jdk-toolchain-version=${{ matrix.java }}
|
||||
|
||||
- name: Build with Maven
|
||||
run: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V -DminimumPriority=1
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ jobs:
|
|||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
|
|
@ -48,6 +48,11 @@ jobs:
|
|||
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
|
||||
# queries: security-extended,security-and-quality
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: 17
|
||||
distribution: 'temurin'
|
||||
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
|
|
|
|||
74
pom.xml
74
pom.xml
|
|
@ -46,7 +46,7 @@
|
|||
|
||||
<!-- Test libs -->
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito.version>2.28.2</mockito.version>
|
||||
<mockito.version>3.12.4</mockito.version>
|
||||
<assertj.version>3.12.1</assertj.version>
|
||||
<awaitility.version>3.1.5</awaitility.version>
|
||||
<powermock.version>2.0.0</powermock.version>
|
||||
|
|
@ -56,14 +56,14 @@
|
|||
<java.source.version>1.8</java.source.version>
|
||||
<java.target.version>1.8</java.target.version>
|
||||
<java.encoding>UTF-8</java.encoding>
|
||||
<maven.compiler.version>3.8.0</maven.compiler.version>
|
||||
<maven.surefire.version>2.22.1</maven.surefire.version>
|
||||
<maven.compiler.version>3.12.0</maven.compiler.version>
|
||||
<maven.surefire.version>3.2.5</maven.surefire.version>
|
||||
<maven.source.version>3.0.1</maven.source.version>
|
||||
<maven.javadoc.version>3.0.1</maven.javadoc.version>
|
||||
<maven.deploy.version>2.8.2</maven.deploy.version>
|
||||
<maven.gpg.version>1.6</maven.gpg.version>
|
||||
<maven.jacoco.version>0.8.3</maven.jacoco.version>
|
||||
<maven.jar.version>3.1.0</maven.jar.version>
|
||||
<maven.jar.version>3.3.0</maven.jar.version>
|
||||
<maven.pmd.version>3.8</maven.pmd.version>
|
||||
</properties>
|
||||
|
||||
|
|
@ -189,6 +189,12 @@
|
|||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-inline</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
|
@ -207,18 +213,6 @@
|
|||
<version>2.0.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
@ -271,15 +265,33 @@
|
|||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven.compiler.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>default-compile</id>
|
||||
<configuration>
|
||||
<source>${java.source.version}</source>
|
||||
<target>${java.target.version}</target>
|
||||
<encoding>${java.encoding}</encoding>
|
||||
<release>17</release>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>base-compile</id>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>module-info.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<release>8</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven.surefire.version}</version>
|
||||
<configuration>
|
||||
<!-- CircleCI build workaround -->
|
||||
<argLine>@{argLine} -Xms1024m -Xmx2048m</argLine>
|
||||
|
|
@ -394,6 +406,30 @@
|
|||
</repository>
|
||||
</distributionManagement>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>custom-test-runtime-version</id>
|
||||
<activation>
|
||||
<property>
|
||||
<name>surefire.jdk-toolchain-version</name>
|
||||
</property>
|
||||
</activation>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven.surefire.version}</version>
|
||||
<configuration>
|
||||
<jdkToolchain>
|
||||
<version>${surefire.jdk-toolchain-version}</version>
|
||||
</jdkToolchain>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
</project>
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>2.2.6.RELEASE</version>
|
||||
<version>2.4.13</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
@ -66,7 +66,7 @@
|
|||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<forkMode>always</forkMode>
|
||||
<reuseForks>false</reuseForks>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
<properties>
|
||||
<servlet.api.version>3.1.0</servlet.api.version>
|
||||
<spring.boot.version>2.4.13</spring.boot.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
|
@ -37,13 +38,13 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>1.5.17.RELEASE</version>
|
||||
<version>${spring.boot.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>1.5.17.RELEASE</version>
|
||||
<version>${spring.boot.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
|||
|
|
@ -41,16 +41,6 @@
|
|||
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
|
@ -58,7 +48,7 @@
|
|||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<artifactId>mockito-inline</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright 1999-2018 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.cluster;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* Mock support for {@link TimeUtil}
|
||||
*
|
||||
* @author jason
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public abstract class AbstractTimeBasedTest {
|
||||
|
||||
private long currentMillis = 0;
|
||||
|
||||
public MockedStatic<TimeUtil> mockTimeUtil() {
|
||||
MockedStatic<TimeUtil> mocked = Mockito.mockStatic(TimeUtil.class);
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
return mocked;
|
||||
}
|
||||
|
||||
protected final void useActualTime(MockedStatic<TimeUtil> mocked) {
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenCallRealMethod();
|
||||
}
|
||||
|
||||
protected final void setCurrentMillis(MockedStatic<TimeUtil> mocked, long cur) {
|
||||
currentMillis = cur;
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleep(MockedStatic<TimeUtil> mocked, long t) {
|
||||
currentMillis += t;
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleepSecond(MockedStatic<TimeUtil> mocked, long timeSec) {
|
||||
sleep(mocked, timeSec * 1000);
|
||||
}
|
||||
}
|
||||
|
|
@ -21,14 +21,16 @@ import com.alibaba.csp.sentinel.cluster.flow.rule.ClusterFlowRuleManager;
|
|||
import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.CurrentConcurrencyManager;
|
||||
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.slots.block.ClusterRuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
|
@ -60,7 +62,8 @@ public class ConcurrentClusterFlowCheckerTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testEasyAcquireAndRelease() throws InterruptedException {
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(111L);
|
||||
ArrayList<Long> list = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
|
|
@ -82,10 +85,12 @@ public class ConcurrentClusterFlowCheckerTest extends AbstractTimeBasedTest {
|
|||
Assert.assertTrue("fail to release token",
|
||||
CurrentConcurrencyManager.get(111L).get() == 0 && TokenCacheNodeManager.getSize() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcurrentAcquireAndRelease() throws InterruptedException {
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
final FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(111L);
|
||||
final CountDownLatch countDownLatch = new CountDownLatch(1000);
|
||||
ExecutorService pool = Executors.newFixedThreadPool(100);
|
||||
|
|
@ -111,6 +116,7 @@ public class ConcurrentClusterFlowCheckerTest extends AbstractTimeBasedTest {
|
|||
Assert.assertTrue("fail to acquire and release token",
|
||||
CurrentConcurrencyManager.get(rule.getClusterConfig().getFlowId()).get() == 0 && TokenCacheNodeManager.getSize() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReleaseExpiredToken() throws InterruptedException {
|
||||
|
|
|
|||
|
|
@ -16,16 +16,16 @@
|
|||
package com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent;
|
||||
|
||||
import com.alibaba.csp.sentinel.cluster.flow.rule.ClusterFlowRuleManager;
|
||||
import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.TokenCacheNode;
|
||||
import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.TokenCacheNodeManager;
|
||||
import com.alibaba.csp.sentinel.cluster.server.AbstractTimeBasedTest;
|
||||
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;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
|
@ -52,7 +52,8 @@ public class TokenCacheNodeManagerTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testPutTokenCacheNode() throws InterruptedException {
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
|
||||
for (long i = 0; i < 100; i++) {
|
||||
final TokenCacheNode node = new TokenCacheNode();
|
||||
|
|
@ -76,4 +77,5 @@ public class TokenCacheNodeManagerTest extends AbstractTimeBasedTest {
|
|||
TokenCacheNodeManager.removeTokenCacheNode(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,10 +16,12 @@
|
|||
package com.alibaba.csp.sentinel.cluster.flow.statistic.limit;
|
||||
|
||||
import com.alibaba.csp.sentinel.cluster.server.config.ClusterServerConfigManager;
|
||||
import com.alibaba.csp.sentinel.cluster.test.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.cluster.server.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
public class GlobalRequestLimiterTest extends AbstractTimeBasedTest {
|
||||
@Before
|
||||
|
|
@ -29,7 +31,8 @@ public class GlobalRequestLimiterTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testPass() throws InterruptedException {
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
GlobalRequestLimiter.initIfAbsent("user");
|
||||
Assert.assertNotNull(GlobalRequestLimiter.getRequestLimiter("user"));
|
||||
Assert.assertEquals(3, GlobalRequestLimiter.getMaxAllowedQps("user"), 0.01);
|
||||
|
|
@ -40,11 +43,12 @@ public class GlobalRequestLimiterTest extends AbstractTimeBasedTest {
|
|||
Assert.assertEquals(3, GlobalRequestLimiter.getCurrentQps("user"), 0.01);
|
||||
|
||||
// wait a second to refresh the window
|
||||
sleep(1000);
|
||||
sleep(mocked, 1000);
|
||||
Assert.assertTrue(GlobalRequestLimiter.tryPass("user"));
|
||||
Assert.assertTrue(GlobalRequestLimiter.tryPass("user"));
|
||||
Assert.assertEquals(2, GlobalRequestLimiter.getCurrentQps("user"), 0.01);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangeMaxAllowedQps() {
|
||||
|
|
|
|||
|
|
@ -15,8 +15,10 @@
|
|||
*/
|
||||
package com.alibaba.csp.sentinel.cluster.flow.statistic.limit;
|
||||
|
||||
import com.alibaba.csp.sentinel.cluster.test.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.cluster.server.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
|
@ -24,7 +26,8 @@ public class RequestLimiterTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testRequestLimiter() {
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
RequestLimiter limiter = new RequestLimiter(10);
|
||||
limiter.add(3);
|
||||
limiter.add(3);
|
||||
|
|
@ -35,10 +38,11 @@ public class RequestLimiterTest extends AbstractTimeBasedTest {
|
|||
assertFalse(limiter.canPass());
|
||||
|
||||
// wait a second to refresh the window
|
||||
sleep(1000);
|
||||
sleep(mocked, 1000);
|
||||
limiter.add(3);
|
||||
assertTrue(limiter.tryPass());
|
||||
assertTrue(limiter.canPass());
|
||||
assertEquals(4, limiter.getSum());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,15 +16,18 @@
|
|||
package com.alibaba.csp.sentinel.cluster.flow.statistic.metric;
|
||||
|
||||
import com.alibaba.csp.sentinel.cluster.flow.statistic.data.ClusterFlowEvent;
|
||||
import com.alibaba.csp.sentinel.cluster.test.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.cluster.server.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
public class ClusterMetricTest extends AbstractTimeBasedTest {
|
||||
|
||||
@Test
|
||||
public void testTryOccupyNext() {
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
ClusterMetric metric = new ClusterMetric(5, 25);
|
||||
metric.add(ClusterFlowEvent.PASS, 1);
|
||||
metric.add(ClusterFlowEvent.PASS, 2);
|
||||
|
|
@ -43,4 +46,5 @@ public class ClusterMetricTest extends AbstractTimeBasedTest {
|
|||
metric.add(ClusterFlowEvent.PASS, 1);
|
||||
Assert.assertEquals(0, metric.tryOccupyNext(ClusterFlowEvent.PASS, 333, 900));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,9 +15,11 @@
|
|||
*/
|
||||
package com.alibaba.csp.sentinel.cluster.flow.statistic.metric;
|
||||
|
||||
import com.alibaba.csp.sentinel.cluster.test.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.cluster.server.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
|
@ -26,7 +28,8 @@ public class ClusterParamMetricTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testClusterParamMetric() {
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
Map<Object, Double> topMap = new HashMap<Object, Double>();
|
||||
ClusterParamMetric metric = new ClusterParamMetric(5, 25, 100);
|
||||
metric.addValue("e1", -1);
|
||||
|
|
@ -47,6 +50,7 @@ public class ClusterParamMetricTest extends AbstractTimeBasedTest {
|
|||
Assert.assertEquals(246, metric.getSum("e2"));
|
||||
Assert.assertEquals(9840, metric.getAvg("e2"), 0.01);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIllegalArgument() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright 1999-2018 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.cluster.server;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* Mock support for {@link TimeUtil}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public abstract class AbstractTimeBasedTest {
|
||||
|
||||
private long currentMillis = 0;
|
||||
|
||||
public MockedStatic<TimeUtil> mockTimeUtil() {
|
||||
MockedStatic<TimeUtil> mocked = Mockito.mockStatic(TimeUtil.class);
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
return mocked;
|
||||
}
|
||||
|
||||
protected final void useActualTime(MockedStatic<TimeUtil> mocked) {
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenCallRealMethod();
|
||||
}
|
||||
|
||||
protected final void setCurrentMillis(MockedStatic<TimeUtil> mocked, long cur) {
|
||||
currentMillis = cur;
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleep(MockedStatic<TimeUtil> mocked, long t) {
|
||||
currentMillis += t;
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleepSecond(MockedStatic<TimeUtil> mocked, long timeSec) {
|
||||
sleep(mocked, timeSec * 1000);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
/*
|
||||
* Copyright 1999-2018 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.cluster.test;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
/**
|
||||
* Mock support for {@link TimeUtil}.
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({TimeUtil.class})
|
||||
public abstract class AbstractTimeBasedTest {
|
||||
|
||||
private long currentMillis = 0;
|
||||
|
||||
{
|
||||
PowerMockito.mockStatic(TimeUtil.class);
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void useActualTime() {
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenCallRealMethod();
|
||||
}
|
||||
|
||||
protected final void setCurrentMillis(long cur) {
|
||||
currentMillis = cur;
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleep(int t) {
|
||||
currentMillis += t;
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleepSecond(int timeSec) {
|
||||
sleep(timeSec * 1000);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
/*
|
||||
* Copyright 1999-2018 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.test;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
/**
|
||||
* Mock support for {@link TimeUtil}
|
||||
*
|
||||
* @author jason
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({TimeUtil.class})
|
||||
public abstract class AbstractTimeBasedTest {
|
||||
|
||||
private long currentMillis = 0;
|
||||
|
||||
{
|
||||
PowerMockito.mockStatic(TimeUtil.class);
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void useActualTime() {
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenCallRealMethod();
|
||||
}
|
||||
|
||||
protected final void setCurrentMillis(long cur) {
|
||||
currentMillis = cur;
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleep(int t) {
|
||||
currentMillis += t;
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleepSecond(int timeSec) {
|
||||
sleep(timeSec * 1000);
|
||||
}
|
||||
}
|
||||
|
|
@ -30,7 +30,7 @@
|
|||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<artifactId>mockito-inline</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
@ -43,16 +43,6 @@
|
|||
<artifactId>java-hamcrest</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
|
|
|||
|
|
@ -22,11 +22,14 @@ import com.alibaba.csp.sentinel.metric.extension.MetricExtensionProvider;
|
|||
import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper;
|
||||
import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import static org.powermock.api.mockito.PowerMockito.mock;
|
||||
import static org.powermock.api.mockito.PowerMockito.when;
|
||||
|
||||
/**
|
||||
* @author Carpenter Lee
|
||||
|
|
@ -35,6 +38,7 @@ public class MetricExitCallbackTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void onExit() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
FakeMetricExtension extension = new FakeMetricExtension();
|
||||
MetricExtensionProvider.addMetricExtension(extension);
|
||||
|
||||
|
|
@ -51,7 +55,7 @@ public class MetricExitCallbackTest extends AbstractTimeBasedTest {
|
|||
|
||||
// Mock current time
|
||||
long curMillis = System.currentTimeMillis();
|
||||
setCurrentMillis(curMillis);
|
||||
setCurrentMillis(mocked, curMillis);
|
||||
|
||||
int deltaMs = 100;
|
||||
when(entry.getError()).thenReturn(null);
|
||||
|
|
@ -62,12 +66,14 @@ public class MetricExitCallbackTest extends AbstractTimeBasedTest {
|
|||
Assert.assertEquals(extension.success, 6 + count);
|
||||
Assert.assertEquals(extension.thread, 10 - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author bill_yip
|
||||
*/
|
||||
@Test
|
||||
public void advancedExtensionOnExit() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
FakeAdvancedMetricExtension extension = new FakeAdvancedMetricExtension();
|
||||
MetricExtensionProvider.addMetricExtension(extension);
|
||||
|
||||
|
|
@ -84,7 +90,7 @@ public class MetricExitCallbackTest extends AbstractTimeBasedTest {
|
|||
|
||||
// Mock current time
|
||||
long curMillis = System.currentTimeMillis();
|
||||
setCurrentMillis(curMillis);
|
||||
setCurrentMillis(mocked, curMillis);
|
||||
|
||||
int deltaMs = 100;
|
||||
when(entry.getError()).thenReturn(null);
|
||||
|
|
@ -95,4 +101,5 @@ public class MetricExitCallbackTest extends AbstractTimeBasedTest {
|
|||
Assert.assertEquals(extension.complete, 6 + count);
|
||||
Assert.assertEquals(extension.concurrency, 10 - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,9 +22,11 @@ import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreake
|
|||
import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.EventObserverRegistry;
|
||||
import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;
|
||||
|
||||
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.Arrays;
|
||||
|
|
@ -55,8 +57,9 @@ public class CircuitBreakingIntegrationTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testSlowRequestMode() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
CircuitBreakerStateChangeObserver observer = mock(CircuitBreakerStateChangeObserver.class);
|
||||
setCurrentMillis(System.currentTimeMillis() / 1000 * 1000);
|
||||
setCurrentMillis(mocked, System.currentTimeMillis() / 1000 * 1000);
|
||||
int retryTimeoutSec = 5;
|
||||
int maxRt = 50;
|
||||
int statIntervalMs = 20000;
|
||||
|
|
@ -72,30 +75,30 @@ public class CircuitBreakingIntegrationTest extends AbstractTimeBasedTest {
|
|||
// Try first N requests where N = minRequestAmount.
|
||||
for (int i = 0; i < minRequestAmount; i++) {
|
||||
if (i < 7) {
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
} else {
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(-20, -10)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(-20, -10)));
|
||||
}
|
||||
}
|
||||
|
||||
// Till now slow ratio should be 70%.
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
// Circuit breaker has transformed to OPEN since here.
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.CLOSED), eq(State.OPEN), any(DegradeRule.class), anyDouble());
|
||||
assertEquals(State.OPEN, DegradeRuleManager.getCircuitBreakers(res).get(0).currentState());
|
||||
assertFalse(entryAndSleepFor(res, 1));
|
||||
assertFalse(entryAndSleepFor(mocked, res, 1));
|
||||
|
||||
sleepSecond(1);
|
||||
assertFalse(entryAndSleepFor(res, 1));
|
||||
sleepSecond(retryTimeoutSec);
|
||||
sleepSecond(mocked, 1);
|
||||
assertFalse(entryAndSleepFor(mocked, res, 1));
|
||||
sleepSecond(mocked, retryTimeoutSec);
|
||||
// Test HALF-OPEN to OPEN.
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.OPEN), eq(State.HALF_OPEN), any(DegradeRule.class), nullable(Double.class));
|
||||
|
|
@ -103,22 +106,24 @@ public class CircuitBreakingIntegrationTest extends AbstractTimeBasedTest {
|
|||
.onStateChange(eq(State.HALF_OPEN), eq(State.OPEN), any(DegradeRule.class), anyDouble());
|
||||
// Wait for next retry timeout;
|
||||
reset(observer);
|
||||
sleepSecond(retryTimeoutSec + 1);
|
||||
assertTrue(entryAndSleepFor(res, maxRt - ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
sleepSecond(mocked, retryTimeoutSec + 1);
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt - ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.OPEN), eq(State.HALF_OPEN), any(DegradeRule.class), nullable(Double.class));
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.HALF_OPEN), eq(State.CLOSED), any(DegradeRule.class), nullable(Double.class));
|
||||
// Now circuit breaker has been closed.
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
|
||||
EventObserverRegistry.getInstance().removeStateChangeObserver(res);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSlowRequestModeUseDefaultRule() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
CircuitBreakerStateChangeObserver observer = mock(CircuitBreakerStateChangeObserver.class);
|
||||
setCurrentMillis(System.currentTimeMillis() / 1000 * 1000);
|
||||
setCurrentMillis(mocked, System.currentTimeMillis() / 1000 * 1000);
|
||||
int retryTimeoutSec = 5;
|
||||
int maxRt = 50;
|
||||
int statIntervalMs = 20000;
|
||||
|
|
@ -134,30 +139,30 @@ public class CircuitBreakingIntegrationTest extends AbstractTimeBasedTest {
|
|||
// Try first N requests where N = minRequestAmount.
|
||||
for (int i = 0; i < minRequestAmount; i++) {
|
||||
if (i < 7) {
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
} else {
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(-20, -10)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(-20, -10)));
|
||||
}
|
||||
}
|
||||
|
||||
// Till now slow ratio should be 70%.
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
// Circuit breaker has transformed to OPEN since here.
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.CLOSED), eq(State.OPEN), any(DegradeRule.class), anyDouble());
|
||||
assertEquals(State.OPEN, DefaultCircuitBreakerRuleManager.getDefaultCircuitBreakers(res).get(0).currentState());
|
||||
assertFalse(entryAndSleepFor(res, 1));
|
||||
assertFalse(entryAndSleepFor(mocked, res, 1));
|
||||
|
||||
sleepSecond(1);
|
||||
assertFalse(entryAndSleepFor(res, 1));
|
||||
sleepSecond(retryTimeoutSec);
|
||||
sleepSecond(mocked, 1);
|
||||
assertFalse(entryAndSleepFor(mocked, res, 1));
|
||||
sleepSecond(mocked, retryTimeoutSec);
|
||||
// Test HALF-OPEN to OPEN.
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.OPEN), eq(State.HALF_OPEN), any(DegradeRule.class), nullable(Double.class));
|
||||
|
|
@ -165,22 +170,24 @@ public class CircuitBreakingIntegrationTest extends AbstractTimeBasedTest {
|
|||
.onStateChange(eq(State.HALF_OPEN), eq(State.OPEN), any(DegradeRule.class), anyDouble());
|
||||
// Wait for next retry timeout;
|
||||
reset(observer);
|
||||
sleepSecond(retryTimeoutSec + 1);
|
||||
assertTrue(entryAndSleepFor(res, maxRt - ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
sleepSecond(mocked, retryTimeoutSec + 1);
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt - ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.OPEN), eq(State.HALF_OPEN), any(DegradeRule.class), nullable(Double.class));
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.HALF_OPEN), eq(State.CLOSED), any(DegradeRule.class), nullable(Double.class));
|
||||
// Now circuit breaker has been closed.
|
||||
assertTrue(entryAndSleepFor(res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
assertTrue(entryAndSleepFor(mocked, res, maxRt + ThreadLocalRandom.current().nextInt(10, 20)));
|
||||
|
||||
EventObserverRegistry.getInstance().removeStateChangeObserver(res);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExceptionRatioMode() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
CircuitBreakerStateChangeObserver observer = mock(CircuitBreakerStateChangeObserver.class);
|
||||
setCurrentMillis(System.currentTimeMillis() / 1000 * 1000);
|
||||
setCurrentMillis(mocked, System.currentTimeMillis() / 1000 * 1000);
|
||||
int retryTimeoutSec = 5;
|
||||
double maxRatio = 0.5;
|
||||
int statIntervalMs = 25000;
|
||||
|
|
@ -196,40 +203,41 @@ public class CircuitBreakingIntegrationTest extends AbstractTimeBasedTest {
|
|||
// Try first N requests where N = minRequestAmount.
|
||||
for (int i = 0; i < minRequestAmount - 1; i++) {
|
||||
if (i < 6) {
|
||||
assertTrue(entryWithErrorIfPresent(res, new IllegalArgumentException()));
|
||||
assertTrue(entryWithErrorIfPresent(mocked, res, new IllegalArgumentException()));
|
||||
} else {
|
||||
assertTrue(entryWithErrorIfPresent(res, null));
|
||||
assertTrue(entryWithErrorIfPresent(mocked, res, null));
|
||||
}
|
||||
}
|
||||
|
||||
// Till now slow ratio should be 60%.
|
||||
assertTrue(entryWithErrorIfPresent(res, new IllegalArgumentException()));
|
||||
assertTrue(entryWithErrorIfPresent(mocked, res, new IllegalArgumentException()));
|
||||
// Circuit breaker has transformed to OPEN since here.
|
||||
assertEquals(State.OPEN, DegradeRuleManager.getCircuitBreakers(res).get(0).currentState());
|
||||
assertFalse(entryWithErrorIfPresent(res, null));
|
||||
assertFalse(entryWithErrorIfPresent(mocked, res, null));
|
||||
|
||||
sleepSecond(2);
|
||||
assertFalse(entryWithErrorIfPresent(res, null));
|
||||
sleepSecond(retryTimeoutSec);
|
||||
sleepSecond(mocked, 2);
|
||||
assertFalse(entryWithErrorIfPresent(mocked, res, null));
|
||||
sleepSecond(mocked, retryTimeoutSec);
|
||||
// Test HALF-OPEN to OPEN.
|
||||
assertTrue(entryWithErrorIfPresent(res, new IllegalArgumentException()));
|
||||
assertTrue(entryWithErrorIfPresent(mocked, res, new IllegalArgumentException()));
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.OPEN), eq(State.HALF_OPEN), any(DegradeRule.class), nullable(Double.class));
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.HALF_OPEN), eq(State.OPEN), any(DegradeRule.class), anyDouble());
|
||||
// Wait for next retry timeout;
|
||||
reset(observer);
|
||||
sleepSecond(retryTimeoutSec + 1);
|
||||
assertTrue(entryWithErrorIfPresent(res, null));
|
||||
sleepSecond(mocked, retryTimeoutSec + 1);
|
||||
assertTrue(entryWithErrorIfPresent(mocked, res, null));
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.OPEN), eq(State.HALF_OPEN), any(DegradeRule.class), nullable(Double.class));
|
||||
verify(observer)
|
||||
.onStateChange(eq(State.HALF_OPEN), eq(State.CLOSED), any(DegradeRule.class), nullable(Double.class));
|
||||
// Now circuit breaker has been closed.
|
||||
assertTrue(entryWithErrorIfPresent(res, new IllegalArgumentException()));
|
||||
assertTrue(entryWithErrorIfPresent(mocked, res, new IllegalArgumentException()));
|
||||
|
||||
EventObserverRegistry.getInstance().removeStateChangeObserver(res);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExceptionCountMode() {
|
||||
|
|
@ -252,8 +260,9 @@ public class CircuitBreakingIntegrationTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testMultipleHalfOpenedBreakers() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
CircuitBreakerStateChangeObserver observer = mock(CircuitBreakerStateChangeObserver.class);
|
||||
setCurrentMillis(System.currentTimeMillis() / 1000 * 1000);
|
||||
setCurrentMillis(mocked, System.currentTimeMillis() / 1000 * 1000);
|
||||
int retryTimeoutSec = 2;
|
||||
int maxRt = 50;
|
||||
int statIntervalMs = 20000;
|
||||
|
|
@ -269,28 +278,29 @@ public class CircuitBreakingIntegrationTest extends AbstractTimeBasedTest {
|
|||
.setStatIntervalMs(statIntervalMs).setMinRequestAmount(minRequestAmount)
|
||||
.setSlowRatioThreshold(0.8d).setGrade(0)
|
||||
));
|
||||
assertTrue(entryAndSleepFor(res, 100));
|
||||
assertTrue(entryAndSleepFor(mocked, res, 100));
|
||||
// they are open now
|
||||
for (CircuitBreaker breaker : DegradeRuleManager.getCircuitBreakers(res)) {
|
||||
assertEquals(CircuitBreaker.State.OPEN, breaker.currentState());
|
||||
}
|
||||
|
||||
sleepSecond(3);
|
||||
sleepSecond(mocked, 3);
|
||||
|
||||
for (int i = 0; i < 10; i ++) {
|
||||
assertFalse(entryAndSleepFor(res, 100));
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assertFalse(entryAndSleepFor(mocked, res, 100));
|
||||
}
|
||||
// Now one is in open state while the other experiences open -> half-open -> open
|
||||
verifyState(DegradeRuleManager.getCircuitBreakers(res), 2);
|
||||
|
||||
sleepSecond(3);
|
||||
sleepSecond(mocked, 3);
|
||||
|
||||
// They will all recover
|
||||
for (int i = 0; i < 10; i ++) {
|
||||
assertTrue(entryAndSleepFor(res, 1));
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assertTrue(entryAndSleepFor(mocked, res, 1));
|
||||
}
|
||||
|
||||
verifyState(DegradeRuleManager.getCircuitBreakers(res), -4);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import static org.junit.Assert.assertTrue;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
|
@ -30,6 +31,7 @@ import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
|||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||
import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
/**
|
||||
* @author Eric Zhao
|
||||
|
|
@ -48,6 +50,7 @@ public class ExceptionCircuitBreakerTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testRecordErrorOrSuccess() throws BlockException {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
String resource = "testRecordErrorOrSuccess";
|
||||
int retryTimeoutMillis = 10 * 1000;
|
||||
int retryTimeout = retryTimeoutMillis / 1000;
|
||||
|
|
@ -60,26 +63,27 @@ public class ExceptionCircuitBreakerTest extends AbstractTimeBasedTest {
|
|||
rule.setResource(resource);
|
||||
DegradeRuleManager.loadRules(Arrays.asList(rule));
|
||||
|
||||
assertTrue(entryAndSleepFor(resource, 10));
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 10));
|
||||
|
||||
assertTrue(entryWithErrorIfPresent(resource, new IllegalArgumentException())); // -> open
|
||||
assertFalse(entryWithErrorIfPresent(resource, new IllegalArgumentException()));
|
||||
assertFalse(entryAndSleepFor(resource, 100));
|
||||
sleep(retryTimeoutMillis / 2);
|
||||
assertFalse(entryAndSleepFor(resource, 100));
|
||||
sleep(retryTimeoutMillis / 2);
|
||||
assertTrue(entryWithErrorIfPresent(resource, new IllegalArgumentException())); // -> half -> open
|
||||
assertFalse(entryAndSleepFor(resource, 100));
|
||||
assertFalse(entryAndSleepFor(resource, 100));
|
||||
sleep(retryTimeoutMillis);
|
||||
assertTrue(entryAndSleepFor(resource, 100)); // -> half -> closed
|
||||
assertTrue(entryAndSleepFor(resource, 100));
|
||||
assertTrue(entryAndSleepFor(resource, 100));
|
||||
assertTrue(entryAndSleepFor(resource, 100));
|
||||
assertTrue(entryAndSleepFor(resource, 100));
|
||||
assertTrue(entryAndSleepFor(resource, 100));
|
||||
assertTrue(entryAndSleepFor(resource, 100));
|
||||
assertTrue(entryWithErrorIfPresent(resource, new IllegalArgumentException()));
|
||||
assertTrue(entryAndSleepFor(resource, 100));
|
||||
assertTrue(entryWithErrorIfPresent(mocked, resource, new IllegalArgumentException())); // -> open
|
||||
assertFalse(entryWithErrorIfPresent(mocked, resource, new IllegalArgumentException()));
|
||||
assertFalse(entryAndSleepFor(mocked, resource, 100));
|
||||
sleep(mocked, retryTimeoutMillis / 2);
|
||||
assertFalse(entryAndSleepFor(mocked, resource, 100));
|
||||
sleep(mocked, retryTimeoutMillis / 2);
|
||||
assertTrue(entryWithErrorIfPresent(mocked, resource, new IllegalArgumentException())); // -> half -> open
|
||||
assertFalse(entryAndSleepFor(mocked, resource, 100));
|
||||
assertFalse(entryAndSleepFor(mocked, resource, 100));
|
||||
sleep(mocked, retryTimeoutMillis);
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 100)); // -> half -> closed
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 100));
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 100));
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 100));
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 100));
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 100));
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 100));
|
||||
assertTrue(entryWithErrorIfPresent(mocked, resource, new IllegalArgumentException()));
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 100));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,9 +4,11 @@ import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
|||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||
import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;
|
||||
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;
|
||||
|
|
@ -31,6 +33,7 @@ public class ResponseTimeCircuitBreakerTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testMaxSlowRatioThreshold() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
String resource = "testMaxSlowRatioThreshold";
|
||||
DegradeRule rule = new DegradeRule("resource")
|
||||
.setCount(10)
|
||||
|
|
@ -42,17 +45,18 @@ public class ResponseTimeCircuitBreakerTest extends AbstractTimeBasedTest {
|
|||
rule.setResource(resource);
|
||||
DegradeRuleManager.loadRules(Collections.singletonList(rule));
|
||||
|
||||
assertTrue(entryAndSleepFor(resource, 20));
|
||||
assertTrue(entryAndSleepFor(resource, 20));
|
||||
assertTrue(entryAndSleepFor(resource, 20));
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 20));
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 20));
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 20));
|
||||
|
||||
// should be blocked, cause 3/3 requests' rt is bigger than max rt.
|
||||
assertFalse(entryAndSleepFor(resource,20));
|
||||
sleep(1000);
|
||||
assertFalse(entryAndSleepFor(resource,20));
|
||||
sleep(4000);
|
||||
assertFalse(entryAndSleepFor(mocked, resource, 20));
|
||||
sleep(mocked, 1000);
|
||||
assertFalse(entryAndSleepFor(mocked, resource, 20));
|
||||
sleep(mocked, 4000);
|
||||
|
||||
assertTrue(entryAndSleepFor(resource, 20));
|
||||
assertTrue(entryAndSleepFor(mocked, resource, 20));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,10 +20,12 @@ import static org.junit.Assert.assertTrue;
|
|||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.csp.sentinel.node.Node;
|
||||
import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
/**
|
||||
* @author jialiang.linjl
|
||||
|
|
@ -32,9 +34,10 @@ public class WarmUpControllerTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testWarmUp() throws InterruptedException {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
WarmUpController warmupController = new WarmUpController(10, 10, 3);
|
||||
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
|
||||
Node node = mock(Node.class);
|
||||
|
||||
|
|
@ -51,7 +54,7 @@ public class WarmUpControllerTest extends AbstractTimeBasedTest {
|
|||
when(node.previousPassQps()).thenReturn(10d);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
sleep(100);
|
||||
sleep(mocked, 100);
|
||||
warmupController.canPass(node, 1);
|
||||
}
|
||||
when(node.passQps()).thenReturn(8d);
|
||||
|
|
@ -60,4 +63,5 @@ public class WarmUpControllerTest extends AbstractTimeBasedTest {
|
|||
when(node.passQps()).thenReturn(10d);
|
||||
assertFalse(warmupController.canPass(node, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,9 +17,11 @@ package com.alibaba.csp.sentinel.slots.block.flow.tokenbucket;
|
|||
|
||||
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
|
||||
import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
|
@ -52,11 +54,12 @@ public class TokenBucketTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testForDefaultTokenBucket() throws InterruptedException {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
long unitProduceNum = 1;
|
||||
long maxTokenNum = 2;
|
||||
long intervalInMs = 1000;
|
||||
long testStart = System.currentTimeMillis();
|
||||
setCurrentMillis(testStart);
|
||||
setCurrentMillis(mocked, testStart);
|
||||
|
||||
DefaultTokenBucket defaultTokenBucket = new DefaultTokenBucket(unitProduceNum, maxTokenNum, intervalInMs);
|
||||
|
||||
|
|
@ -69,23 +72,25 @@ public class TokenBucketTest extends AbstractTimeBasedTest {
|
|||
assertTrue(defaultTokenBucketFullStart.tryConsume(2));
|
||||
assertFalse(defaultTokenBucketFullStart.tryConsume(1));
|
||||
|
||||
sleep(1000);
|
||||
sleep(mocked, 1000);
|
||||
assertTrue(defaultTokenBucket.tryConsume(1));
|
||||
assertFalse(defaultTokenBucket.tryConsume(1));
|
||||
|
||||
sleep(1000);
|
||||
sleep(mocked, 1000);
|
||||
assertTrue(defaultTokenBucketFullStart.tryConsume(2));
|
||||
assertFalse(defaultTokenBucketFullStart.tryConsume(1));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForStrictTokenBucket() throws InterruptedException {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
long unitProduceNum = 5;
|
||||
long maxTokenNum = 10;
|
||||
long intervalInMs = 1000;
|
||||
final int n = 64;
|
||||
long testStart = System.currentTimeMillis();
|
||||
setCurrentMillis(testStart);
|
||||
setCurrentMillis(mocked, testStart);
|
||||
|
||||
final AtomicLong passNum = new AtomicLong();
|
||||
final AtomicLong passNumFullStart = new AtomicLong();
|
||||
|
|
@ -121,5 +126,6 @@ public class TokenBucketTest extends AbstractTimeBasedTest {
|
|||
assertEquals(5, passNum.longValue());
|
||||
assertEquals(10, passNumFullStart.longValue());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,9 +17,11 @@ package com.alibaba.csp.sentinel.slots.statistic.base;
|
|||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
|
@ -30,6 +32,7 @@ public class LeapArrayTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testGetValidHead() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
int windowLengthInMs = 100;
|
||||
int intervalInMs = 1000;
|
||||
int sampleCount = intervalInMs / windowLengthInMs;
|
||||
|
|
@ -49,17 +52,18 @@ public class LeapArrayTest extends AbstractTimeBasedTest {
|
|||
|
||||
WindowWrap<AtomicInteger> expected1 = leapArray.currentWindow();
|
||||
expected1.value().addAndGet(1);
|
||||
sleep(windowLengthInMs);
|
||||
sleep(mocked, windowLengthInMs);
|
||||
WindowWrap<AtomicInteger> expected2 = leapArray.currentWindow();
|
||||
expected2.value().addAndGet(2);
|
||||
for (int i = 0; i < sampleCount - 2; i++) {
|
||||
sleep(windowLengthInMs);
|
||||
sleep(mocked, windowLengthInMs);
|
||||
leapArray.currentWindow().value().addAndGet(i + 3);
|
||||
}
|
||||
|
||||
assertSame(expected1, leapArray.getValidHead());
|
||||
sleep(windowLengthInMs);
|
||||
sleep(mocked, windowLengthInMs);
|
||||
assertSame(expected2, leapArray.getValidHead());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;
|
|||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
|
@ -27,8 +28,9 @@ public class OccupiableBucketLeapArrayTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testNewWindow() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
setCurrentMillis(currentTime);
|
||||
setCurrentMillis(mocked, currentTime);
|
||||
OccupiableBucketLeapArray leapArray = new OccupiableBucketLeapArray(sampleCount, intervalInMs);
|
||||
|
||||
WindowWrap<MetricBucket> currentWindow = leapArray.currentWindow(currentTime);
|
||||
|
|
@ -38,14 +40,15 @@ public class OccupiableBucketLeapArrayTest extends AbstractTimeBasedTest {
|
|||
leapArray.addWaiting(currentTime + windowLengthInMs, 1);
|
||||
assertEquals(leapArray.currentWaiting(), 1);
|
||||
assertEquals(currentWindow.value().pass(), 1L);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWindowInOneInterval() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
OccupiableBucketLeapArray leapArray = new OccupiableBucketLeapArray(sampleCount, intervalInMs);
|
||||
long currentTime = System.currentTimeMillis();
|
||||
setCurrentMillis(currentTime);
|
||||
setCurrentMillis(mocked, currentTime);
|
||||
|
||||
WindowWrap<MetricBucket> currentWindow = leapArray.currentWindow(currentTime);
|
||||
currentWindow.value().addPass(1);
|
||||
|
|
@ -65,11 +68,13 @@ public class OccupiableBucketLeapArrayTest extends AbstractTimeBasedTest {
|
|||
}
|
||||
assertEquals(sum, 3);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiThreadUpdateEmptyWindow() throws Exception {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
final long time = System.currentTimeMillis();
|
||||
setCurrentMillis(time);
|
||||
setCurrentMillis(mocked, time);
|
||||
final int nThreads = 16;
|
||||
final OccupiableBucketLeapArray leapArray = new OccupiableBucketLeapArray(sampleCount, intervalInMs);
|
||||
final CountDownLatch latch = new CountDownLatch(nThreads);
|
||||
|
|
@ -100,12 +105,14 @@ public class OccupiableBucketLeapArrayTest extends AbstractTimeBasedTest {
|
|||
assertEquals(values.size(), 2);
|
||||
assertEquals(sum, nThreads * 2);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWindowAfterOneInterval() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
OccupiableBucketLeapArray leapArray = new OccupiableBucketLeapArray(sampleCount, intervalInMs);
|
||||
long currentTime = System.currentTimeMillis();
|
||||
setCurrentMillis(currentTime);
|
||||
setCurrentMillis(mocked, currentTime);
|
||||
|
||||
System.out.println(currentTime);
|
||||
for (int i = 0; i < intervalInSec * 1000 / windowLengthInMs; i++) {
|
||||
|
|
@ -136,4 +143,5 @@ public class OccupiableBucketLeapArrayTest extends AbstractTimeBasedTest {
|
|||
*/
|
||||
assertEquals(leapArray.currentWaiting(), 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,58 +15,58 @@
|
|||
*/
|
||||
package com.alibaba.csp.sentinel.test;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import com.alibaba.csp.sentinel.Entry;
|
||||
import com.alibaba.csp.sentinel.SphU;
|
||||
import com.alibaba.csp.sentinel.Tracer;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
/**
|
||||
* Mock support for {@link TimeUtil}.
|
||||
*
|
||||
* @author jason
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ TimeUtil.class })
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public abstract class AbstractTimeBasedTest {
|
||||
|
||||
private long currentMillis = 0;
|
||||
|
||||
{
|
||||
PowerMockito.mockStatic(TimeUtil.class);
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
public MockedStatic<TimeUtil> mockTimeUtil() {
|
||||
MockedStatic<TimeUtil> mocked = Mockito.mockStatic(TimeUtil.class);
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
return mocked;
|
||||
}
|
||||
|
||||
protected final void useActualTime() {
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenCallRealMethod();
|
||||
protected final void useActualTime(MockedStatic<TimeUtil> mocked) {
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenCallRealMethod();
|
||||
}
|
||||
|
||||
protected final void setCurrentMillis(long cur) {
|
||||
protected final void setCurrentMillis(MockedStatic<TimeUtil> mocked, long cur) {
|
||||
currentMillis = cur;
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleep(int t) {
|
||||
protected final void sleep(MockedStatic<TimeUtil> mocked, long t) {
|
||||
currentMillis += t;
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleepSecond(int timeSec) {
|
||||
sleep(timeSec * 1000);
|
||||
protected final void sleepSecond(MockedStatic<TimeUtil> mocked, long timeSec) {
|
||||
sleep(mocked, timeSec * 1000);
|
||||
}
|
||||
|
||||
protected final boolean entryAndSleepFor(String res, int sleepMs) {
|
||||
protected final boolean entryAndSleepFor(MockedStatic<TimeUtil> mocked, String res, int sleepMs) {
|
||||
Entry entry = null;
|
||||
try {
|
||||
entry = SphU.entry(res);
|
||||
sleep(sleepMs);
|
||||
sleep(mocked, sleepMs);
|
||||
} catch (BlockException ex) {
|
||||
return false;
|
||||
} catch (Exception ex) {
|
||||
|
|
@ -79,14 +79,14 @@ public abstract class AbstractTimeBasedTest {
|
|||
return true;
|
||||
}
|
||||
|
||||
protected final boolean entryWithErrorIfPresent(String res, Exception ex) {
|
||||
protected final boolean entryWithErrorIfPresent(MockedStatic<TimeUtil> mocked, String res, Exception ex) {
|
||||
Entry entry = null;
|
||||
try {
|
||||
entry = SphU.entry(res);
|
||||
if (ex != null) {
|
||||
Tracer.traceEntry(ex, entry);
|
||||
}
|
||||
sleep(ThreadLocalRandom.current().nextInt(5, 10));
|
||||
sleep(mocked, ThreadLocalRandom.current().nextInt(5, 10));
|
||||
} catch (BlockException b) {
|
||||
return false;
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.fail;
|
|||
/**
|
||||
* @author sea
|
||||
*/
|
||||
@Ignore
|
||||
public class SentinelAnnotationInterceptorIntegrationTest {
|
||||
|
||||
static SeContainer container;
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@
|
|||
<artifactId>sentinel-datasource-eureka</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.cloud.version>2.1.2.RELEASE</spring.cloud.version>
|
||||
<spring.boot.version>2.6.1</spring.boot.version>
|
||||
<spring.cloud.version>3.1.0</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
|
||||
|
|
@ -27,6 +28,12 @@
|
|||
<artifactId>fastjson</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.13.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
|
@ -42,7 +49,7 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<version>${spring.boot.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,8 @@
|
|||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<spring.cloud.version>2.0.0.RELEASE</spring.cloud.version>
|
||||
<spring.boot.version>2.1.3.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>2.1.9.RELEASE</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
|
@ -44,7 +45,7 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<version>${spring.boot.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
|
@ -58,7 +59,7 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<version>${spring.boot.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
|
|
|||
|
|
@ -36,17 +36,7 @@
|
|||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<artifactId>mockito-inline</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright 1999-2018 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.block.flow.param;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
|
||||
/**
|
||||
* Mock support for {@link TimeUtil}.
|
||||
*
|
||||
* @author jason
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public abstract class AbstractTimeBasedTest {
|
||||
|
||||
private long currentMillis = 0;
|
||||
|
||||
public MockedStatic<TimeUtil> mockTimeUtil() {
|
||||
MockedStatic<TimeUtil> mocked = Mockito.mockStatic(TimeUtil.class);
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
return mocked;
|
||||
}
|
||||
|
||||
protected final void useActualTime(MockedStatic<TimeUtil> mocked) {
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenCallRealMethod();
|
||||
}
|
||||
|
||||
protected final void setCurrentMillis(MockedStatic<TimeUtil> mocked, long cur) {
|
||||
currentMillis = cur;
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleep(MockedStatic<TimeUtil> mocked, long t) {
|
||||
currentMillis += t;
|
||||
mocked.when(TimeUtil::currentTimeMillis).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleepSecond(MockedStatic<TimeUtil> mocked, long timeSec) {
|
||||
sleep(mocked, timeSec * 1000);
|
||||
}
|
||||
}
|
||||
|
|
@ -33,8 +33,9 @@ import com.alibaba.csp.sentinel.EntryType;
|
|||
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper;
|
||||
import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper;
|
||||
import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper;
|
||||
import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.block.flow.param.AbstractTimeBasedTest;
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
/**
|
||||
* @author jialiang.linjl
|
||||
|
|
@ -44,6 +45,7 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
|
||||
@Test
|
||||
public void testCheckQpsWithLongIntervalAndHighThreshold() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
// This test case is intended to avoid number overflow.
|
||||
final String resourceName = "testCheckQpsWithLongIntervalAndHighThreshold";
|
||||
final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
|
||||
|
|
@ -64,25 +66,27 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
|
||||
|
||||
// We mock the time directly to avoid unstable behaviour.
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
||||
// 24 hours passed.
|
||||
// This can make `toAddCount` larger that Integer.MAX_VALUE.
|
||||
sleep(1000 * 60 * 60 * 24);
|
||||
sleep(mocked, 1000 * 60 * 60 * 24);
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
||||
// 48 hours passed.
|
||||
sleep(1000 * 60 * 60 * 48);
|
||||
sleep(mocked, 1000 * 60 * 60 * 48);
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParamFlowDefaultCheckSingleQps() {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
final String resourceName = "testParamFlowDefaultCheckSingleQps";
|
||||
final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
|
||||
int paramIdx = 0;
|
||||
|
|
@ -102,7 +106,7 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
|
||||
|
||||
// We mock the time directly to avoid unstable behaviour.
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
|
@ -111,7 +115,7 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
||||
sleep(3000);
|
||||
sleep(mocked, 3000);
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
|
@ -119,9 +123,11 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParamFlowDefaultCheckSingleQpsWithBurst() throws InterruptedException {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
final String resourceName = "testParamFlowDefaultCheckSingleQpsWithBurst";
|
||||
final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
|
||||
int paramIdx = 0;
|
||||
|
|
@ -142,7 +148,7 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
|
||||
|
||||
// We mock the time directly to avoid unstable behaviour.
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
|
@ -154,7 +160,7 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
||||
sleep(1002);
|
||||
sleep(mocked, 1002);
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
|
@ -162,7 +168,7 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
||||
sleep(1002);
|
||||
sleep(mocked, 1002);
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
|
@ -170,7 +176,7 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
||||
sleep(2000);
|
||||
sleep(mocked, 2000);
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
|
@ -181,7 +187,7 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
||||
sleep(1002);
|
||||
sleep(mocked, 1002);
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
|
@ -189,9 +195,11 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParamFlowDefaultCheckQpsInDifferentDuration() throws InterruptedException {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
final String resourceName = "testParamFlowDefaultCheckQpsInDifferentDuration";
|
||||
final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
|
||||
int paramIdx = 0;
|
||||
|
|
@ -212,7 +220,7 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
|
||||
|
||||
// We mock the time directly to avoid unstable behaviour.
|
||||
setCurrentMillis(System.currentTimeMillis());
|
||||
setCurrentMillis(mocked, System.currentTimeMillis());
|
||||
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
|
@ -222,16 +230,16 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
||||
sleepSecond(1);
|
||||
sleepSecond(mocked, 1);
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
||||
sleepSecond(10);
|
||||
sleepSecond(mocked, 10);
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
||||
sleepSecond(30);
|
||||
sleepSecond(mocked, 30);
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
||||
sleepSecond(30);
|
||||
sleepSecond(mocked, 30);
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
|
|
@ -240,11 +248,13 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
|
||||
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParamFlowDefaultCheckSingleValueCheckQpsMultipleThreads() throws Exception {
|
||||
try (MockedStatic<TimeUtil> mocked = super.mockTimeUtil()) {
|
||||
// In this test case we use the actual time.
|
||||
useActualTime();
|
||||
useActualTime(mocked);
|
||||
|
||||
final String resourceName = "testParamFlowDefaultCheckSingleValueCheckQpsMultipleThreads";
|
||||
final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
|
||||
|
|
@ -322,6 +332,7 @@ public class ParamFlowDefaultCheckerTest extends AbstractTimeBasedTest {
|
|||
|
||||
assertEquals(successCount.get(), threshold);
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
|
|
|||
|
|
@ -1,58 +0,0 @@
|
|||
/*
|
||||
* Copyright 1999-2018 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.test;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
/**
|
||||
* Mock support for {@link TimeUtil}
|
||||
*
|
||||
* @author jason
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({TimeUtil.class})
|
||||
public abstract class AbstractTimeBasedTest {
|
||||
|
||||
private long currentMillis = 0;
|
||||
|
||||
{
|
||||
PowerMockito.mockStatic(TimeUtil.class);
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void useActualTime() {
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenCallRealMethod();
|
||||
}
|
||||
|
||||
protected final void setCurrentMillis(long cur) {
|
||||
currentMillis = cur;
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleep(int t) {
|
||||
currentMillis += t;
|
||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||
}
|
||||
|
||||
protected final void sleepSecond(int timeSec) {
|
||||
sleep(timeSec * 1000);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue