use of tech.pegasys.teku.infrastructure.async.SafeFuture.Interruptor in project teku by ConsenSys.
the class SafeFutureTest method notInterrupted_shouldThrowIfInterrupted.
@Test
public void notInterrupted_shouldThrowIfInterrupted() {
SafeFuture<Void> interruptorFut = new SafeFuture<>();
Interruptor interruptor = SafeFuture.createInterruptor(interruptorFut, () -> new RuntimeException("test"));
interruptorFut.complete(null);
SafeFuture<String> future = SafeFuture.notInterrupted(interruptor).thenApply(__ -> "aaa");
assertThatThrownBy(future::get).hasMessageContaining("test");
}
use of tech.pegasys.teku.infrastructure.async.SafeFuture.Interruptor in project teku by ConsenSys.
the class RpcHandler method sendRequest.
public SafeFuture<RpcStreamController<TOutgoingHandler>> sendRequest(Connection connection, TRequest request, TRespHandler responseHandler) {
final Bytes initialPayload;
try {
initialPayload = rpcMethod.encodeRequest(request);
} catch (Exception e) {
return SafeFuture.failedFuture(e);
}
Interruptor closeInterruptor = SafeFuture.createInterruptor(connection.closeFuture(), PeerDisconnectedException::new);
Interruptor timeoutInterruptor = SafeFuture.createInterruptor(asyncRunner.getDelayedFuture(TIMEOUT), () -> new StreamTimeoutException("Timed out waiting to initialize stream for protocol(s): " + String.join(",", rpcMethod.getIds())));
return SafeFuture.notInterrupted(closeInterruptor).thenApply(__ -> connection.muxerSession().createStream(this)).thenWaitFor(StreamPromise::getStream).orInterrupt(closeInterruptor, timeoutInterruptor).thenCompose(streamPromise -> {
final SafeFuture<String> protocolIdFuture = SafeFuture.of(streamPromise.getStream().thenCompose(Stream::getProtocol));
// waiting for controller, writing initial payload or interrupt
return SafeFuture.of(streamPromise.getController()).<String, RpcStreamController<TOutgoingHandler>>thenCombine(protocolIdFuture, (controller, protocolId) -> {
final TOutgoingHandler handler = rpcMethod.createOutgoingRequestHandler(protocolId, request, responseHandler);
controller.setOutgoingRequestHandler(handler);
return controller;
}).orInterrupt(closeInterruptor, timeoutInterruptor).thenWaitFor(controller -> controller.getRpcStream().writeBytes(initialPayload)).orInterrupt(closeInterruptor, timeoutInterruptor).whenException(err -> closeStreamAbruptly(streamPromise.getStream().join()));
}).catchAndRethrow(err -> {
if (ExceptionUtil.getCause(err, ConnectionClosedException.class).isPresent()) {
throw new PeerDisconnectedException(err);
}
});
}
use of tech.pegasys.teku.infrastructure.async.SafeFuture.Interruptor in project teku by ConsenSys.
the class SafeFutureTest method orInterrupt_simpleCompleteWithoutInterruption.
@Test
public void orInterrupt_simpleCompleteWithoutInterruption() throws Exception {
SafeFuture<Integer> interruptorFut = new SafeFuture<>();
Interruptor interruptor = SafeFuture.createInterruptor(interruptorFut, IllegalStateException::new);
SafeFuture<Integer> fut0 = new SafeFuture<>();
assertThat(hasDependents(interruptorFut)).isFalse();
assertThat(hasDependents(fut0)).isFalse();
SafeFuture<Integer> intFut = fut0.orInterrupt(interruptor);
assertThat(hasDependents(interruptorFut)).isTrue();
assertThat(hasDependents(fut0)).isTrue();
fut0.complete(12);
assertThat(hasDependents(interruptorFut)).isFalse();
assertThat(hasDependents(fut0)).isFalse();
assertThat(intFut.get()).isEqualTo(12);
}
use of tech.pegasys.teku.infrastructure.async.SafeFuture.Interruptor in project teku by ConsenSys.
the class SafeFutureTest method orInterrupt_triggerOneOfTwoInterruptors.
@Test
public void orInterrupt_triggerOneOfTwoInterruptors() {
SafeFuture<Integer> interruptorFut1 = new SafeFuture<>();
SafeFuture<Integer> interruptorFut2 = new SafeFuture<>();
Interruptor interruptor1 = SafeFuture.createInterruptor(interruptorFut1, IllegalStateException::new);
Interruptor interruptor2 = SafeFuture.createInterruptor(interruptorFut2, IllegalStateException::new);
SafeFuture<Integer> fut0 = new SafeFuture<>();
assertThat(hasDependents(interruptorFut1)).isFalse();
assertThat(hasDependents(interruptorFut2)).isFalse();
assertThat(hasDependents(fut0)).isFalse();
SafeFuture<Integer> intFut = fut0.orInterrupt(interruptor1, interruptor2);
assertThat(hasDependents(interruptorFut1)).isTrue();
assertThat(hasDependents(interruptorFut2)).isTrue();
assertThat(hasDependents(fut0)).isTrue();
interruptorFut2.complete(0);
assertThat(hasDependents(interruptorFut1)).isFalse();
assertThat(hasDependents(interruptorFut2)).isFalse();
assertThat(hasDependents(fut0)).isFalse();
assertThat(intFut).isCompletedExceptionally();
}
Aggregations