/[projects]/miscJava/CircuitBreaker/src/test/java/dk/thoerup/circuitbreaker/TestCircuitBreaker.java
ViewVC logotype

Contents of /miscJava/CircuitBreaker/src/test/java/dk/thoerup/circuitbreaker/TestCircuitBreaker.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 452 - (show annotations) (download)
Tue Oct 20 10:47:36 2009 UTC (14 years, 7 months ago) by torben
Original Path: CircuitBreaker/test/dk/thoerup/circuitbreaker/TestCircuitBreaker.java
File size: 3888 byte(s)
More tests
1 package dk.thoerup.circuitbreaker;
2
3
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.IOException;
8
9 import org.junit.Before;
10 import org.junit.Test;
11
12 import dk.thoerup.curcuitbreaker.CircuitBreaker;
13 import dk.thoerup.curcuitbreaker.CircuitBreakerException;
14 import dk.thoerup.curcuitbreaker.CircuitInvocation;
15 import dk.thoerup.curcuitbreaker.notification.SystemOutNotifier;
16
17
18 public class TestCircuitBreaker {
19 public static final int DELAY = 50;
20 public static final int THRESHOLD = 2;
21
22 class SucceedingInvocation implements CircuitInvocation {
23 public Object proceed() throws Exception {
24 return "OK";
25 }
26 }
27
28 class FailingInvocation implements CircuitInvocation {
29 public Object proceed() throws Exception {
30 throw new IOException("Error");
31 }
32 }
33
34
35 CircuitBreaker cb;
36
37 @Before public void setup() {
38 cb = new CircuitBreaker("test", THRESHOLD, DELAY);
39 cb.setNotifier( new SystemOutNotifier() );
40 }
41
42 @Test public void defaultState() {
43 assertTrue(cb.isClosed());
44 assertTrue(cb.getFailureCount() == 0);
45 }
46
47 @Test public void simpleTest() throws Exception {
48 String retval = (String) cb.invoke( new SucceedingInvocation() );
49 assertEquals(retval, "OK");
50 assertTrue( cb.isClosed() );
51 assertTrue(cb.getFailureCount() == 0);
52 }
53
54 @Test(expected=IOException.class) public void simpleFailingTest() throws Exception {
55 cb.invoke( new FailingInvocation() );
56 }
57
58 @Test public void failingTest() {
59 try {
60 cb.invoke( new FailingInvocation() );
61 }catch (Exception e) {}
62 assertTrue(cb.getFailureCount() == 1);
63 assertTrue(cb.isClosed());
64 }
65
66 @Test public void failAndResetTest() throws Exception {
67 try {
68 cb.invoke( new FailingInvocation() );
69 }catch (Exception e) {}
70
71 cb.invoke(new SucceedingInvocation() ); //after one good it should reset back to closed
72
73 assertTrue(cb.isClosed());
74 assertTrue(cb.getFailureCount() == 0);
75 }
76
77 @Test public void normalOpenTest() throws Exception {
78 try{
79 cb.invoke( new FailingInvocation() );
80 } catch (IOException e) {}
81
82 try{
83 cb.invoke( new FailingInvocation() );
84 } catch (IOException e) {}
85
86 assertTrue( cb.isOpen() );
87 }
88
89 @Test public void forcedResetTest() throws Exception {
90 try{
91 cb.invoke( new FailingInvocation() );
92 } catch (IOException e) {}
93
94 try{
95 cb.invoke( new FailingInvocation() );
96 } catch (IOException e) {}
97
98 cb.reset();
99
100 assertTrue( cb.isClosed() );
101 assertTrue( cb.getFailureCount() == 0 ); //currently an externally triggered reset doesn't reset failure count - should a forced reset be possible at all ?
102 }
103
104
105 @Test(expected=CircuitBreakerException.class) public void openAndFailingTest1() throws Exception {
106 cb.tripBreaker();
107 assertTrue(cb.isOpen());
108 cb.invoke( new FailingInvocation() );
109 }
110
111 @Test(expected=CircuitBreakerException.class) public void openAndFailingTest2() throws Exception {
112 cb.tripBreaker();
113 assertTrue(cb.isOpen());
114 cb.invoke( new SucceedingInvocation() );
115 }
116
117 @Test public void halfOpen1() throws Exception {
118 cb.tripBreaker();
119
120 assertTrue( cb.isOpen() );
121 Thread.sleep(DELAY*2);
122
123 cb.invoke( new SucceedingInvocation() );
124 assertTrue( cb.isClosed() );
125 }
126
127 @Test(expected=CircuitBreakerException.class) public void halfOpen2() throws Exception {
128 cb.tripBreaker();
129
130 assertTrue( cb.isOpen() );
131 Thread.sleep(DELAY*2);
132
133 cb.invoke( new FailingInvocation() ); //in half open this will cause a CircuitBreakerException
134 }
135
136 @Test public void halfOpen3() throws Exception {
137 cb.tripBreaker();
138
139 assertTrue( cb.isOpen() );
140 Thread.sleep(DELAY*2);
141
142 try{
143 cb.invoke( new FailingInvocation() );
144 } catch (CircuitBreakerException e) {}
145
146 assertTrue( cb.isOpen() );
147 }
148
149
150 }

  ViewVC Help
Powered by ViewVC 1.1.20