-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
CircuitBreakerLoadBalancerTest.java
231 lines (192 loc) · 8.99 KB
/
CircuitBreakerLoadBalancerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.processor;
import java.util.concurrent.RejectedExecutionException;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import static org.apache.camel.component.mock.MockEndpoint.expectsMessageCount;
public class CircuitBreakerLoadBalancerTest extends ContextTestSupport {
private static class MyCustomException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
private MockEndpoint result;
@Override
protected void setUp() throws Exception {
super.setUp();
result = getMockEndpoint("mock:result");
}
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:start").loadBalance()
.circuitBreaker(2, 1000L, MyCustomException.class)
.to("mock:result");
from("direct:start-async").loadBalance()
.circuitBreaker(2, 1000L, MyCustomException.class)
.threads(1).to("mock:result");
}
};
}
public void testClosedCircuitPassesMessagesSync() throws Exception {
String endpoint = "direct:start";
closedCircuitPassesMessages(endpoint);
}
public void testClosedCircuitPassesMessagesAsync() throws Exception {
String endpoint = "direct:start-async";
closedCircuitPassesMessages(endpoint);
}
private void closedCircuitPassesMessages(String endpoint) throws InterruptedException, Exception {
expectsMessageCount(3, result);
sendMessage(endpoint, "message one");
sendMessage(endpoint, "message two");
sendMessage(endpoint, "message three");
assertMockEndpointsSatisfied();
}
public void testFailedMessagesOpenCircuitToPreventMessageThreeSync() throws Exception {
String endpoint = "direct:start";
failedMessagesOpenCircuitToPreventMessageThree(endpoint);
}
public void testFailedMessagesOpenCircuitToPreventMessageThreeAsync() throws Exception {
String endpoint = "direct:start-async";
failedMessagesOpenCircuitToPreventMessageThree(endpoint);
}
private void failedMessagesOpenCircuitToPreventMessageThree(String endpoint) throws InterruptedException, Exception {
expectsMessageCount(2, result);
result.whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.setException(new MyCustomException());
}
});
Exchange exchangeOne = sendMessage(endpoint, "message one");
Exchange exchangeTwo = sendMessage(endpoint, "message two");
Exchange exchangeThree = sendMessage(endpoint, "message three");
assertMockEndpointsSatisfied();
assertTrue(exchangeOne.getException() instanceof MyCustomException);
assertTrue(exchangeTwo.getException() instanceof MyCustomException);
assertTrue(exchangeThree.getException() instanceof RejectedExecutionException);
}
public void testHalfOpenAfterTimeoutSync() throws Exception {
String endpoint = "direct:start";
halfOpenAfterTimeout(endpoint);
}
public void testHalfOpenAfterTimeoutAsync() throws Exception {
String endpoint = "direct:start-async";
halfOpenAfterTimeout(endpoint);
}
private void halfOpenAfterTimeout(String endpoint) throws InterruptedException, Exception {
expectsMessageCount(2, result);
result.whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.setException(new MyCustomException());
}
});
Exchange exchangeOne = sendMessage(endpoint, "message one");
Exchange exchangeTwo = sendMessage(endpoint, "message two");
Exchange exchangeThree = sendMessage(endpoint, "message three");
Exchange exchangeFour = sendMessage(endpoint, "message four");
assertMockEndpointsSatisfied();
Thread.sleep(1000);
result.reset();
result.whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.setException(new MyCustomException());
}
});
expectsMessageCount(1, result);
Exchange exchangeFive = sendMessage(endpoint, "message five");
Exchange exchangeSix = sendMessage(endpoint, "message six");
assertMockEndpointsSatisfied();
assertTrue(exchangeOne.getException() instanceof MyCustomException);
assertTrue(exchangeTwo.getException() instanceof MyCustomException);
assertTrue(exchangeThree.getException() instanceof RejectedExecutionException);
assertTrue(exchangeFour.getException() instanceof RejectedExecutionException);
assertTrue(exchangeFive.getException() instanceof MyCustomException);
assertTrue(exchangeSix.getException() instanceof RejectedExecutionException);
}
public void testHalfOpenToCloseTransitionSync() throws Exception {
String endpoint = "direct:start";
halfOpenToCloseTransition(endpoint);
}
public void testHalfOpenToCloseTransitionAsync() throws Exception {
String endpoint = "direct:start-async";
halfOpenToCloseTransition(endpoint);
}
private void halfOpenToCloseTransition(String endpoint) throws Exception {
expectsMessageCount(2, result);
result.whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.setException(new MyCustomException());
}
});
Exchange exchangeOne = sendMessage(endpoint, "message one");
Exchange exchangeTwo = sendMessage(endpoint, "message two");
Exchange exchangeThree = sendMessage(endpoint, "message three");
assertMockEndpointsSatisfied();
Thread.sleep(1000);
result.reset();
expectsMessageCount(2, result);
Exchange exchangeFour = sendMessage(endpoint, "message four");
Exchange exchangeFive = sendMessage(endpoint, "message five");
assertMockEndpointsSatisfied();
assertTrue(exchangeOne.getException() instanceof MyCustomException);
assertTrue(exchangeTwo.getException() instanceof MyCustomException);
assertTrue(exchangeThree.getException() instanceof RejectedExecutionException);
assertTrue(exchangeFour.getException() == null);
assertTrue(exchangeFive.getException() == null);
}
public void testHalfOpenCircuitClosesAfterTimeoutSync() throws Exception {
String endpoint = "direct:start";
halfOpenCircuitClosesAfterTimeout(endpoint);
}
public void testHalfOpenCircuitClosesAfterTimeoutAsync() throws Exception {
String endpoint = "direct:start-async";
halfOpenCircuitClosesAfterTimeout(endpoint);
}
private void halfOpenCircuitClosesAfterTimeout(String endpoint) throws InterruptedException, Exception {
expectsMessageCount(2, result);
result.whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.setException(new MyCustomException());
}
});
sendMessage(endpoint, "message one");
sendMessage(endpoint, "message two");
sendMessage(endpoint, "message three");
assertMockEndpointsSatisfied();
result.reset();
expectsMessageCount(1, result);
Thread.sleep(1000);
sendMessage(endpoint, "message four");
assertMockEndpointsSatisfied();
}
protected Exchange sendMessage(final String endpoint, final Object body) throws Exception {
return template.send(endpoint, new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(body);
}
});
}
}