18
18
*/
19
19
package io .github .resilience4j .decorators ;
20
20
21
+ import io .github .resilience4j .bulkhead .Bulkhead ;
21
22
import io .github .resilience4j .cache .Cache ;
22
23
import io .github .resilience4j .circuitbreaker .CircuitBreaker ;
23
24
import io .github .resilience4j .ratelimiter .RateLimiter ;
24
25
import io .github .resilience4j .ratelimiter .RateLimiterConfig ;
25
26
import io .github .resilience4j .ratelimiter .RequestNotPermitted ;
27
+ import io .github .resilience4j .retry .AsyncRetry ;
26
28
import io .github .resilience4j .retry .Retry ;
27
29
import io .github .resilience4j .test .HelloWorldService ;
28
30
import io .vavr .CheckedFunction0 ;
35
37
36
38
import java .io .IOException ;
37
39
import java .time .Duration ;
40
+ import java .util .concurrent .CompletableFuture ;
41
+ import java .util .concurrent .CompletionStage ;
42
+ import java .util .concurrent .ExecutionException ;
43
+ import java .util .concurrent .Executors ;
38
44
import java .util .function .Function ;
39
45
import java .util .function .Supplier ;
40
46
@@ -62,6 +68,7 @@ public void testDecorateSupplier() {
62
68
.withCircuitBreaker (circuitBreaker )
63
69
.withRetry (Retry .ofDefaults ("id" ))
64
70
.withRateLimiter (RateLimiter .ofDefaults ("testName" ))
71
+ .withBulkhead (Bulkhead .ofDefaults ("testName" ))
65
72
.decorate ();
66
73
67
74
String result = decoratedSupplier .get ();
@@ -84,6 +91,7 @@ public void testDecorateCheckedSupplier() throws IOException {
84
91
.withCircuitBreaker (circuitBreaker )
85
92
.withRetry (Retry .ofDefaults ("id" ))
86
93
.withRateLimiter (RateLimiter .ofDefaults ("testName" ))
94
+ .withBulkhead (Bulkhead .ofDefaults ("testName" ))
87
95
.decorate ();
88
96
89
97
String result = Try .of (decoratedSupplier ).get ();
@@ -104,6 +112,7 @@ public void testDecorateRunnable() {
104
112
.withCircuitBreaker (circuitBreaker )
105
113
.withRetry (Retry .ofDefaults ("id" ))
106
114
.withRateLimiter (RateLimiter .ofDefaults ("testName" ))
115
+ .withBulkhead (Bulkhead .ofDefaults ("testName" ))
107
116
.decorate ();
108
117
109
118
decoratedRunnable .run ();
@@ -124,6 +133,7 @@ public void testDecorateCheckedRunnable() throws IOException {
124
133
.withCircuitBreaker (circuitBreaker )
125
134
.withRetry (Retry .ofDefaults ("id" ))
126
135
.withRateLimiter (RateLimiter .ofDefaults ("testName" ))
136
+ .withBulkhead (Bulkhead .ofDefaults ("testName" ))
127
137
.decorate ();
128
138
129
139
Try .run (decoratedRunnable );
@@ -135,6 +145,53 @@ public void testDecorateCheckedRunnable() throws IOException {
135
145
BDDMockito .then (helloWorldService ).should (times (1 )).sayHelloWorldWithException ();
136
146
}
137
147
148
+
149
+ @ Test
150
+ public void testDecorateCompletionStage () throws ExecutionException , InterruptedException {
151
+ // Given the HelloWorldService returns Hello world
152
+ given (helloWorldService .returnHelloWorld ()).willReturn ("Hello world" );
153
+ CircuitBreaker circuitBreaker = CircuitBreaker .ofDefaults ("helloBackend" );
154
+
155
+ Supplier <CompletionStage <String >> completionStageSupplier =
156
+ () -> CompletableFuture .supplyAsync (helloWorldService ::returnHelloWorld );
157
+
158
+ CompletionStage <String > completionStage = Decorators .ofCompletionStage (completionStageSupplier )
159
+ .withCircuitBreaker (circuitBreaker )
160
+ .withRetry (AsyncRetry .ofDefaults ("id" ), Executors .newSingleThreadScheduledExecutor ())
161
+ .withBulkhead (Bulkhead .ofDefaults ("testName" ))
162
+ .get ();
163
+
164
+ String value = completionStage .toCompletableFuture ().get ();
165
+ assertThat (value ).isEqualTo ("Hello world" );
166
+
167
+ CircuitBreaker .Metrics metrics = circuitBreaker .getMetrics ();
168
+ assertThat (metrics .getNumberOfBufferedCalls ()).isEqualTo (1 );
169
+ assertThat (metrics .getNumberOfSuccessfulCalls ()).isEqualTo (1 );
170
+
171
+ // Then the helloWorldService should be invoked 1 time
172
+ BDDMockito .then (helloWorldService ).should (times (1 )).returnHelloWorld ();
173
+ }
174
+
175
+ @ Test
176
+ public void testExecuteConsumer () throws ExecutionException , InterruptedException {
177
+ // Given the HelloWorldService returns Hello world
178
+ CircuitBreaker circuitBreaker = CircuitBreaker .ofDefaults ("helloBackend" );
179
+
180
+
181
+ Decorators .ofConsumer ((String input ) -> helloWorldService .sayHelloWorldWithName (input ))
182
+ .withCircuitBreaker (circuitBreaker )
183
+ .withBulkhead (Bulkhead .ofDefaults ("testName" ))
184
+ .withRateLimiter (RateLimiter .ofDefaults ("testName" ))
185
+ .accept ("test" );
186
+
187
+ CircuitBreaker .Metrics metrics = circuitBreaker .getMetrics ();
188
+ assertThat (metrics .getNumberOfBufferedCalls ()).isEqualTo (1 );
189
+ assertThat (metrics .getNumberOfSuccessfulCalls ()).isEqualTo (1 );
190
+
191
+ // Then the helloWorldService should be invoked 1 time
192
+ BDDMockito .then (helloWorldService ).should (times (1 )).sayHelloWorldWithName ("test" );
193
+ }
194
+
138
195
@ Test
139
196
public void testDecorateFunction () {
140
197
// Given the HelloWorldService returns Hello world
@@ -145,6 +202,7 @@ public void testDecorateFunction() {
145
202
.withCircuitBreaker (circuitBreaker )
146
203
.withRetry (Retry .ofDefaults ("id" ))
147
204
.withRateLimiter (RateLimiter .ofDefaults ("testName" ))
205
+ .withBulkhead (Bulkhead .ofDefaults ("testName" ))
148
206
.decorate ();
149
207
150
208
String result = decoratedFunction .apply ("Name" );
@@ -165,6 +223,7 @@ public void testDecorateCheckedFunction() throws IOException {
165
223
.withCircuitBreaker (circuitBreaker )
166
224
.withRetry (Retry .ofDefaults ("id" ))
167
225
.withRateLimiter (RateLimiter .ofDefaults ("testName" ))
226
+ .withBulkhead (Bulkhead .ofDefaults ("testName" ))
168
227
.decorate ();
169
228
170
229
String result = Try .of (() -> decoratedFunction .apply ("Name" )).get ();
@@ -185,6 +244,7 @@ public void testDecoratorBuilderWithRetry() {
185
244
Supplier <String > decoratedSupplier = Decorators .ofSupplier (() -> helloWorldService .returnHelloWorld ())
186
245
.withCircuitBreaker (circuitBreaker )
187
246
.withRetry (Retry .ofDefaults ("id" ))
247
+ .withBulkhead (Bulkhead .ofDefaults ("testName" ))
188
248
.decorate ();
189
249
190
250
Try .of (decoratedSupplier ::get );
0 commit comments