diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/degrade/circuitbreaker/ExceptionCircuitBreakerTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/degrade/circuitbreaker/ExceptionCircuitBreakerTest.java index e159353ba1..c6f1439735 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/degrade/circuitbreaker/ExceptionCircuitBreakerTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/degrade/circuitbreaker/ExceptionCircuitBreakerTest.java @@ -90,26 +90,28 @@ public void testRecordErrorOrSuccess() throws BlockException { @Test public void testMaxErrorRatioThreshold() { - String resource = "testMaxErrorRatioThreshold"; - DegradeRule rule = new DegradeRule("resource") - .setCount(1) - .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) - .setMinRequestAmount(3) - .setStatIntervalMs(5000) - .setTimeWindow(5); - rule.setResource(resource); - DegradeRuleManager.loadRules(Collections.singletonList(rule)); + try (MockedStatic mocked = super.mockTimeUtil()) { + String resource = "testMaxErrorRatioThreshold"; + DegradeRule rule = new DegradeRule("resource") + .setCount(1) + .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) + .setMinRequestAmount(3) + .setStatIntervalMs(5000) + .setTimeWindow(5); + rule.setResource(resource); + DegradeRuleManager.loadRules(Collections.singletonList(rule)); - assertTrue(entryWithErrorIfPresent(resource, new RuntimeException())); - assertTrue(entryWithErrorIfPresent(resource, new RuntimeException())); - assertTrue(entryWithErrorIfPresent(resource, new RuntimeException())); + assertTrue(entryWithErrorIfPresent(mocked, resource, new RuntimeException())); + assertTrue(entryWithErrorIfPresent(mocked, resource, new RuntimeException())); + assertTrue(entryWithErrorIfPresent(mocked, resource, new RuntimeException())); - // should be blocked, cause 3/3 requests' rt is bigger than max rt. - assertFalse(entryWithErrorIfPresent(resource, new RuntimeException())); - assertFalse(entryWithErrorIfPresent(resource, new RuntimeException())); + // should be blocked, cause 3/3 requests' rt is bigger than max rt. + assertFalse(entryWithErrorIfPresent(mocked, resource, new RuntimeException())); + assertFalse(entryWithErrorIfPresent(mocked, resource, new RuntimeException())); - sleep(5000); + sleep(mocked, 5000); - assertTrue(entryWithErrorIfPresent(resource, new RuntimeException())); + assertTrue(entryWithErrorIfPresent(mocked, resource, new RuntimeException())); + } } } \ No newline at end of file diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java index a48be564ea..acb4e5e1d7 100755 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.concurrent.CountDownLatch; import org.junit.After; import org.junit.Before; @@ -78,7 +79,7 @@ public void testThreadGrade() throws InterruptedException, BlockException { flowRule.setCount(1); FlowRuleManager.loadRules(Arrays.asList(flowRule)); - final Object sequence = new Object(); + final CountDownLatch latch = new CountDownLatch(1); Runnable runnable = new Runnable() { @Override @@ -86,10 +87,7 @@ public void run() { Entry e = null; try { e = SphU.entry("testThreadGrade"); - synchronized (sequence) { - System.out.println("notify up"); - sequence.notify(); - } + latch.countDown(); Thread.sleep(100); } catch (BlockException e1) { fail("Should had failed"); @@ -103,11 +101,7 @@ public void run() { Thread thread = new Thread(runnable); thread.start(); - synchronized (sequence) { - System.out.println("sleep"); - sequence.wait(); - System.out.println("wake up"); - } + latch.await(); SphU.entry("testThreadGrade"); System.out.println("done");