/[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 459 - (show annotations) (download)
Wed Oct 21 07:52:08 2009 UTC (14 years, 7 months ago) by torben
Original Path: CircuitBreaker/test/dk/thoerup/circuitbreaker/TestCircuitBreaker.java
File size: 3865 byte(s)
Minor tweaks and comments
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
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 }
40
41 @Test public void defaultState() {
42 assertTrue(cb.isClosed());
43 assertTrue(cb.getFailureCount() == 0);
44 }
45
46 @Test public void simpleTest() throws Exception {
47 String retval = (String) cb.invoke( new SucceedingInvocation() );
48 assertEquals(retval, "OK");
49 assertTrue( cb.isClosed() );
50 assertTrue(cb.getFailureCount() == 0);
51 }
52
53 @Test(expected=IOException.class) public void simpleFailingTest() throws Exception {
54 cb.invoke( new FailingInvocation() );
55 }
56
57 @Test public void failingTest() {
58 try {
59 cb.invoke( new FailingInvocation() );
60 }catch (Exception e) {}
61 assertTrue(cb.getFailureCount() == 1);
62 assertTrue(cb.isClosed());
63 }
64
65 @Test public void failAndResetTest() throws Exception {
66 try {
67 cb.invoke( new FailingInvocation() );
68 }catch (Exception e) {}
69
70 cb.invoke(new SucceedingInvocation() ); //after one good it should reset back to closed
71
72 assertTrue(cb.isClosed());
73 assertTrue(cb.getFailureCount() == 0);
74 }
75
76 @Test public void normalOpenTest() throws Exception {
77 try{
78 cb.invoke( new FailingInvocation() );
79 } catch (IOException e) {}
80
81 try{
82 cb.invoke( new FailingInvocation() );
83 } catch (IOException e) {}
84
85 assertTrue( cb.isOpen() );
86 }
87
88 @Test public void forcedTrip() {
89 assertTrue( cb.isClosed() );
90 cb.tripBreaker();
91 assertTrue( cb.isOpen() );
92 }
93
94 @Test public void forcedResetTest() throws Exception {
95 try{
96 cb.invoke( new FailingInvocation() );
97 } catch (IOException e) {}
98
99 try{
100 cb.invoke( new FailingInvocation() );
101 } catch (IOException e) {}
102
103 cb.reset();
104
105 assertTrue( cb.isClosed() );
106 assertTrue( cb.getFailureCount() == 0 );
107 }
108
109
110 @Test(expected=CircuitBreakerException.class) public void openAndFailingTest1() throws Exception {
111 cb.tripBreaker();
112 assertTrue(cb.isOpen());
113 cb.invoke( new FailingInvocation() );
114 }
115
116 @Test(expected=CircuitBreakerException.class) public void openAndFailingTest2() throws Exception {
117 cb.tripBreaker();
118 assertTrue(cb.isOpen());
119 cb.invoke( new SucceedingInvocation() );
120 }
121
122 @Test public void halfOpen1() throws Exception {
123 cb.tripBreaker();
124
125 assertTrue( cb.isOpen() );
126 Thread.sleep(DELAY*2);
127
128 cb.invoke( new SucceedingInvocation() );
129 assertTrue( cb.isClosed() );
130 }
131
132 @Test(expected=CircuitBreakerException.class) public void halfOpen2() throws Exception {
133 cb.tripBreaker();
134
135 assertTrue( cb.isOpen() );
136 Thread.sleep(DELAY*2);
137
138 cb.invoke( new FailingInvocation() ); //in half-open this will cause a CircuitBreakerException, as if it was in open mode
139 }
140
141 @Test public void halfOpen3() throws Exception {
142 cb.tripBreaker();
143
144 assertTrue( cb.isOpen() );
145 Thread.sleep(DELAY*2);
146
147 try{
148 cb.invoke( new FailingInvocation() );
149 } catch (CircuitBreakerException e) {}
150
151 assertTrue( cb.isOpen() ); //after failing in half-open go back to open
152 }
153
154
155 }

  ViewVC Help
Powered by ViewVC 1.1.20