public static LiveHttpRequest doFinally(LiveHttpRequest request, Consumer<Optional<Throwable>> action) { return request.newBuilder() .body(it -> it.doOnEnd(action)) .build(); }
public static LiveHttpResponse doFinally(LiveHttpResponse response, Consumer<Optional<Throwable>> action) { return response.newBuilder() .body(it -> it.doOnEnd(action::accept)) .build(); }
public static LiveHttpResponse doOnComplete(LiveHttpResponse response, Runnable action) { return response.newBuilder() .body(it -> it.doOnEnd(ifSuccessful(action))) .build(); }
public static LiveHttpRequest doOnError(LiveHttpRequest request, Consumer<Throwable> action) { return request.newBuilder() .body(it -> it.doOnEnd(ifError(action))) .build(); }
public static LiveHttpRequest doOnComplete(LiveHttpRequest request, Runnable action) { return request.newBuilder() .body(it -> it.doOnEnd(ifSuccessful(action))) .build(); }
public static LiveHttpResponse doOnError(LiveHttpResponse response, Consumer<Throwable> action) { return response.newBuilder() .body(it -> it.doOnEnd(ifError(action))) .build(); }
@Test public void deliversAtEndOfStreamNotification() { AtomicBoolean terminated = new AtomicBoolean(); ByteStream stream = new ByteStream(Flux.just(buf1, buf2)) .doOnEnd(maybeCause -> terminated.set(maybeCause == Optional.<Throwable>empty())); StepVerifier.create(new ByteStream(stream), 0) .thenRequest(1) .expectNext(buf1) .then(() -> assertThat(terminated.get(), is(false))) .thenRequest(1) .expectNext(buf2) .then(() -> assertThat(terminated.get(), is(true))) .expectComplete() .verify(); }
@Test public void deliversAtEndOfStreamNotificationWhenTerminated() { AtomicBoolean terminated = new AtomicBoolean(); ByteStream stream = new ByteStream(Flux.just(buf1, buf2).concatWith(Flux.error(new RuntimeException("bang!")))) .doOnEnd(maybeCause -> terminated.set(maybeCause.isPresent())); StepVerifier.create(new ByteStream(stream), 0) .thenRequest(1) .expectNext(buf1) .then(() -> assertThat(terminated.get(), is(false))) .thenRequest(1) .expectNext(buf2) .then(() -> assertThat(terminated.get(), is(true))) .expectError() .verify(); }