Skip to content

Commit 729d28b

Browse files
committed
New tests
1 parent f250f53 commit 729d28b

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.geckotechnology.simpleCircuitBreaker;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class DisabledStateTest {
8+
9+
private static final int LOOP_COUNT = 100;
10+
private static final long SLEEP_TIME = 200;
11+
private static final int THREAD_COUNT = 5;
12+
13+
@Test
14+
public void test() {
15+
CircuitBreakerConfig config = new CircuitBreakerConfig();
16+
config.setSlidingWindowSize(0);
17+
final CircuitBreaker circuitBreaker = new CircuitBreaker(config);
18+
Thread threads[] = new Thread[THREAD_COUNT];
19+
for(int t = 0; t<THREAD_COUNT; t++) {
20+
threads[t] = new Thread() {
21+
public void run() {
22+
for(int i = 0; i<LOOP_COUNT; i++) {
23+
assertTrue(circuitBreaker.getBreakerState() instanceof BreakerDisabledState);
24+
if(circuitBreaker.isClosedForThisCall()) {
25+
assertTrue(circuitBreaker.getBreakerState() instanceof BreakerDisabledState);
26+
TestUtils.sleep(SLEEP_TIME);
27+
circuitBreaker.callSucceeded(SLEEP_TIME);
28+
}
29+
else
30+
fail("circuitBreaker should always be closed");
31+
}
32+
}
33+
};
34+
threads[t].start();
35+
}
36+
for(int t = 0; t<THREAD_COUNT; t++)
37+
try {
38+
threads[t].join();
39+
} catch (InterruptedException e) {
40+
fail("Exception captured " + e);
41+
}
42+
}
43+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.geckotechnology.simpleCircuitBreaker;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class ForceOpenStateTest {
8+
9+
private static final int LOOP_COUNT = 100;
10+
private static final long SLEEP_TIME = 200;
11+
private static final int THREAD_COUNT = 5;
12+
13+
@Test
14+
public void test() {
15+
CircuitBreakerConfig config = new CircuitBreakerConfig();
16+
config.setSlidingWindowSize(-1);
17+
final CircuitBreaker circuitBreaker = new CircuitBreaker(config);
18+
Thread threads[] = new Thread[THREAD_COUNT];
19+
for(int t = 0; t<THREAD_COUNT; t++) {
20+
threads[t] = new Thread() {
21+
public void run() {
22+
for(int i = 0; i<LOOP_COUNT; i++) {
23+
assertTrue(circuitBreaker.getBreakerState() instanceof BreakerForcedOpenState);
24+
if(circuitBreaker.isClosedForThisCall())
25+
fail("circuitBreaker should always be closed");
26+
else {
27+
assertTrue(circuitBreaker.getBreakerState() instanceof BreakerForcedOpenState);
28+
TestUtils.sleep(SLEEP_TIME);
29+
}
30+
}
31+
}
32+
};
33+
threads[t].start();
34+
}
35+
for(int t = 0; t<THREAD_COUNT; t++)
36+
try {
37+
threads[t].join();
38+
} catch (InterruptedException e) {
39+
fail("Exception captured " + e);
40+
}
41+
}
42+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.geckotechnology.simpleCircuitBreaker;
2+
3+
public class OverheadTest {
4+
5+
CircuitBreaker breaker;
6+
private static final int ARRAY_SIZE = 5017;
7+
private static final int LOOP_CALC_COUNT = 500000;
8+
private static final int LOOP_TEST = 1000;
9+
private static final int LOOP_TEST_WARMUP = 10000;
10+
11+
public static void main(String[] args) {
12+
new OverheadTest().go();
13+
14+
}
15+
16+
public void go() {
17+
CircuitBreakerConfig config = new CircuitBreakerConfig();
18+
config.setSlidingWindowSize(10);
19+
breaker = new CircuitBreaker(config);
20+
21+
int array[] = null;
22+
long start, end = 0;
23+
24+
//warm-up
25+
array = createArray();
26+
for(int i = 0; i<LOOP_TEST_WARMUP; i++)
27+
oneCall(array);
28+
29+
for(int j = 0; j<5; j++) {
30+
//no breaker
31+
array = createArray();
32+
start = System.currentTimeMillis();
33+
for(int i = 0; i<LOOP_TEST; i++)
34+
oneCall(array);
35+
end = System.currentTimeMillis();
36+
double durationPerCallNoBreaker = (double)(end-start)/(double)LOOP_TEST;
37+
System.out.println("duration (ms) per oneCall - NO BREAKER: " + durationPerCallNoBreaker);
38+
39+
//breaker
40+
array = createArray();
41+
start = System.currentTimeMillis();
42+
for(int i = 0; i<LOOP_TEST; i++) {
43+
if(breaker.isClosedForThisCall()) {
44+
long startCall = System.currentTimeMillis();
45+
oneCall(array);
46+
breaker.callSucceeded(System.currentTimeMillis() - startCall);
47+
}
48+
}
49+
end = System.currentTimeMillis();
50+
double durationPerCallWithBreaker = (double)(end-start)/(double)LOOP_TEST;
51+
System.out.println("duration (ms) per oneCall - WITH BREAKER: " + durationPerCallWithBreaker);
52+
System.out.println("Overhead (ms) per oneCall of breaker: " + (durationPerCallWithBreaker - durationPerCallNoBreaker));
53+
}
54+
System.out.println("Done");
55+
}
56+
57+
private int[] createArray() {
58+
int array[] = new int[ARRAY_SIZE];
59+
for(int i = 0; i<ARRAY_SIZE; i++)
60+
array[i] = i;
61+
return array;
62+
}
63+
64+
private void oneCall(int array[]) {
65+
// long start = System.currentTimeMillis();
66+
int s = array.length;
67+
for(int i = 0; i<LOOP_CALC_COUNT; i++)
68+
array[(7 * i) % s] = array[(3 * i) % s] * array [i % s] + array [(5 * i) % s];
69+
// long end = System.currentTimeMillis();
70+
// System.out.println("duration: " + (end-start));
71+
}
72+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.geckotechnology.simpleCircuitBreaker;
2+
3+
public class TestUtils {
4+
5+
public static void sleep(long ms) {
6+
try {
7+
Thread.sleep(ms);
8+
} catch (InterruptedException e) {
9+
e.printStackTrace();
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)