Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<TimeUtil> 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()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,18 +79,15 @@ 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
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");
Expand All @@ -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");
Expand Down
Loading