28
28
import java .util .ArrayList ;
29
29
import java .util .Arrays ;
30
30
import java .util .List ;
31
- import java .util .concurrent .CompletableFuture ;
32
- import java .util .concurrent .TimeUnit ;
31
+ import java .util .concurrent .*;
33
32
import java .util .concurrent .atomic .AtomicReference ;
34
33
import java .util .function .Predicate ;
35
34
import java .util .function .Supplier ;
@@ -287,8 +286,9 @@ void putMetricDataShouldBeCalledOnPublish() {
287
286
@ Test
288
287
void shouldHandleAbortedExceptionDuringPutMetricDataCallWithoutFailing () {
289
288
CloudWatchAsyncClient client = mock (CloudWatchAsyncClient .class );
290
- when (client .putMetricData (isA (PutMetricDataRequest .class )))
291
- .thenReturn (CompletableFuture .failedFuture (AbortedException .create ("simulated" )));
289
+ CompletableFuture <PutMetricDataResponse > future = new CompletableFuture <>();
290
+ future .completeExceptionally (AbortedException .create ("simulated" ));
291
+ when (client .putMetricData (isA (PutMetricDataRequest .class ))).thenReturn (future );
292
292
293
293
CloudWatchMeterRegistry registry = new CloudWatchMeterRegistry (config , clock , client );
294
294
registry .counter ("test" ).increment ();
@@ -300,8 +300,9 @@ void shouldHandleAbortedExceptionDuringPutMetricDataCallWithoutFailing() {
300
300
@ Test
301
301
void shouldHandleExceptionsDuringPutMetricDataCallWithoutFailing () {
302
302
CloudWatchAsyncClient client = mock (CloudWatchAsyncClient .class );
303
- when (client .putMetricData (isA (PutMetricDataRequest .class )))
304
- .thenReturn (CompletableFuture .failedFuture (new SocketTimeoutException ("simulated" )));
303
+ CompletableFuture <PutMetricDataResponse > future = new CompletableFuture <>();
304
+ future .completeExceptionally (new SocketTimeoutException ("simulated" ));
305
+ when (client .putMetricData (isA (PutMetricDataRequest .class ))).thenReturn (future );
305
306
306
307
CloudWatchMeterRegistry registry = new CloudWatchMeterRegistry (config , clock , client );
307
308
registry .counter ("test" ).increment ();
@@ -311,11 +312,16 @@ void shouldHandleExceptionsDuringPutMetricDataCallWithoutFailing() {
311
312
}
312
313
313
314
@ Test
314
- @ SuppressWarnings ("deprecation" )
315
315
void shouldHandleTimeoutsDuringPutMetricDataCallWithoutFailing () {
316
316
CloudWatchAsyncClient client = mock (CloudWatchAsyncClient .class );
317
- when (client .putMetricData (isA (PutMetricDataRequest .class ))).thenReturn (CompletableFuture .supplyAsync (() -> null ,
318
- CompletableFuture .delayedExecutor (config .readTimeout ().toMillis () + 1_000 , TimeUnit .MILLISECONDS )));
317
+ Executor nonExecutingExecutor = new Executor () {
318
+ @ Override
319
+ public void execute (Runnable command ) {
320
+ // intentionally noop
321
+ }
322
+ };
323
+ when (client .putMetricData (isA (PutMetricDataRequest .class )))
324
+ .thenReturn (CompletableFuture .supplyAsync (() -> null , nonExecutingExecutor ));
319
325
320
326
CloudWatchMeterRegistry registry = new CloudWatchMeterRegistry (config , clock , client );
321
327
registry .counter ("test" ).increment ();
0 commit comments