/[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 454 - (show annotations) (download)
Tue Oct 20 11:08:53 2009 UTC (14 years, 7 months ago) by torben
Original Path: CircuitBreaker/test/dk/thoerup/circuitbreaker/TestCircuitBreaker.java
File size: 3787 byte(s)
No need for the notifier
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 forcedResetTest() throws Exception {
89 try{
90 cb.invoke( new FailingInvocation() );
91 } catch (IOException e) {}
92
93 try{
94 cb.invoke( new FailingInvocation() );
95 } catch (IOException e) {}
96
97 cb.reset();
98
99 assertTrue( cb.isClosed() );
100 assertTrue( cb.getFailureCount() == 0 ); //TODO: currently an externally triggered reset doesn't reset failure count - should a forced reset be possible at all ?
101 }
102
103
104 @Test(expected=CircuitBreakerException.class) public void openAndFailingTest1() throws Exception {
105 cb.tripBreaker();
106 assertTrue(cb.isOpen());
107 cb.invoke( new FailingInvocation() );
108 }
109
110 @Test(expected=CircuitBreakerException.class) public void openAndFailingTest2() throws Exception {
111 cb.tripBreaker();
112 assertTrue(cb.isOpen());
113 cb.invoke( new SucceedingInvocation() );
114 }
115
116 @Test public void halfOpen1() throws Exception {
117 cb.tripBreaker();
118
119 assertTrue( cb.isOpen() );
120 Thread.sleep(DELAY*2);
121
122 cb.invoke( new SucceedingInvocation() );
123 assertTrue( cb.isClosed() );
124 }
125
126 @Test(expected=CircuitBreakerException.class) public void halfOpen2() throws Exception {
127 cb.tripBreaker();
128
129 assertTrue( cb.isOpen() );
130 Thread.sleep(DELAY*2);
131
132 cb.invoke( new FailingInvocation() ); //in half open this will cause a CircuitBreakerException
133 }
134
135 @Test public void halfOpen3() throws Exception {
136 cb.tripBreaker();
137
138 assertTrue( cb.isOpen() );
139 Thread.sleep(DELAY*2);
140
141 try{
142 cb.invoke( new FailingInvocation() );
143 } catch (CircuitBreakerException e) {}
144
145 assertTrue( cb.isOpen() );
146 }
147
148
149 }

  ViewVC Help
Powered by ViewVC 1.1.20