Java Code Examples for software.amazon.awssdk.core.async.AsyncResponseTransformer#prepare()

The following examples show how to use software.amazon.awssdk.core.async.AsyncResponseTransformer#prepare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EventStreamAsyncResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void prepareReturnsNewFuture() {
    AsyncResponseTransformer<SdkResponse, Void> transformer =
            EventStreamAsyncResponseTransformer.builder()
                    .eventStreamResponseHandler(
                            onEventStream(p -> {}))
                    .eventResponseHandler((r, e) -> null)
                    .executor(Executors.newFixedThreadPool(2))
                    .future(new CompletableFuture<>())
                    .build();

    CompletableFuture<?> cf1 = transformer.prepare();

    transformer.exceptionOccurred(new RuntimeException("Boom!"));

    assertThat(cf1.isCompletedExceptionally()).isTrue();
    assertThat(transformer.prepare()).isNotEqualTo(cf1);
}
 
Example 2
Source File: EventStreamAsyncResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 2000)
public void prepareResetsSubscriberRef() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(2);
    AtomicBoolean exceptionThrown = new AtomicBoolean(false);

    AsyncResponseTransformer<SdkResponse, Void> transformer =
            EventStreamAsyncResponseTransformer.builder()
                    .eventStreamResponseHandler(
                            onEventStream(p -> {
                                try {
                                    p.subscribe(e -> {});
                                } catch (Throwable t) {
                                    exceptionThrown.set(true);
                                } finally {
                                    latch.countDown();
                                }
                            }))
                    .eventResponseHandler((r, e) -> null)
                    .executor(Executors.newFixedThreadPool(2))
                    .future(new CompletableFuture<>())
                    .build();

    Flowable<ByteBuffer> bytePublisher = Flowable.empty();

    CompletableFuture<Void> transformFuture = transformer.prepare();
    transformer.onStream(SdkPublisher.adapt(bytePublisher));
    transformFuture.join();

    transformFuture = transformer.prepare();
    transformer.onStream(SdkPublisher.adapt(bytePublisher));
    transformFuture.join();

    latch.await();
    assertThat(exceptionThrown).isFalse();
}
 
Example 3
Source File: EventStreamAsyncResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void erroneousExtraExceptionOccurredDoesNotSurfaceException() {
    AtomicLong numExceptions = new AtomicLong(0);
    AsyncResponseTransformer<SdkResponse, Void> transformer =
            EventStreamAsyncResponseTransformer.builder()
                    .eventStreamResponseHandler(new EventStreamResponseHandler<Object, Object>() {
                        @Override
                        public void responseReceived(Object response) {
                        }

                        @Override
                        public void onEventStream(SdkPublisher<Object> publisher) {
                        }

                        @Override
                        public void exceptionOccurred(Throwable throwable) {
                            numExceptions.incrementAndGet();
                        }

                        @Override
                        public void complete() {
                        }
                    })
                    .eventResponseHandler((r, e) -> null)
                    .executor(Executors.newFixedThreadPool(2))
                    .future(new CompletableFuture<>())
                    .build();

    transformer.prepare();
    transformer.exceptionOccurred(new RuntimeException("Boom!"));
    transformer.exceptionOccurred(new RuntimeException("Boom again!"));

    assertThat(numExceptions).hasValue(1);
}
 
Example 4
Source File: EventStreamAsyncResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void verifyExceptionThrown(Map<String, HeaderValue> headers) {
    SdkServiceException exception = SdkServiceException.builder().build();

    Message exceptionMessage = new Message(headers, new byte[0]);

    Flowable<ByteBuffer> bytePublisher = Flowable.just(exceptionMessage.toByteBuffer());

    AsyncResponseTransformer<SdkResponse, Void> transformer =
        EventStreamAsyncResponseTransformer.builder()
                                           .eventStreamResponseHandler(new SubscribingResponseHandler())
                                           .exceptionResponseHandler((response, executionAttributes) -> exception)
                                           .executor(Executors.newSingleThreadExecutor())
                                           .future(new CompletableFuture<>())
                                           .build();
    CompletableFuture<Void> cf = transformer.prepare();
    transformer.onResponse(null);
    transformer.onStream(SdkPublisher.adapt(bytePublisher));

    assertThatThrownBy(() -> {
        try {
            cf.join();
        } catch (CompletionException e) {
            if (e.getCause() instanceof SdkServiceException) {
                throw ((SdkServiceException) e.getCause());
            }
        }
    }).isSameAs(exception);
}