Java Code Examples for io.netty.util.concurrent.Promise#get()

The following examples show how to use io.netty.util.concurrent.Promise#get() . 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: MultiplexedChannelRecordTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * IOException is expected if acquire stream from closed channel.
 */
@Test
public void acquireClaimedConnectionOnClosedChannelShouldThrowIOException() {
  loopGroup.register(channel).awaitUninterruptibly();
  Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());

  MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 1, 10000L, streamChannelInitializer);

  record.closeChildChannels();

  record.acquireClaimedStream(channelPromise);

  try {
    channelPromise.get();
  } catch (InterruptedException | ExecutionException e) {
    assertTrue(e.getCause() instanceof IOException);
  }
}
 
Example 2
Source File: MultiplexedChannelRecordTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void closeChildChannels_shouldDeliverException() throws ExecutionException, InterruptedException {
    EmbeddedChannel channel = newHttp2Channel();
    loopGroup.register(channel).awaitUninterruptibly();
    Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());
    channelPromise.setSuccess(channel);

    MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 2, Duration.ofSeconds(10));

    Promise<Channel> streamPromise = channel.eventLoop().newPromise();
    record.acquireStream(streamPromise);

    channel.runPendingTasks();
    Channel childChannel = streamPromise.get();
    VerifyExceptionHandler verifyExceptionHandler = new VerifyExceptionHandler();
    childChannel.pipeline().addFirst(verifyExceptionHandler);

    IOException ioException = new IOException("foobar");
    record.closeChildChannels(ioException);

    assertThat(childChannel.pipeline().get(UnusedChannelExceptionHandler.class)).isNotNull();

    assertThat(verifyExceptionHandler.exceptionCaught).hasStackTraceContaining("foobar")
                                                      .hasRootCauseInstanceOf(IOException.class);

    // should be closed by UnusedChannelExceptionHandler
    assertThat(childChannel.isOpen()).isFalse();
}
 
Example 3
Source File: HttpBlobStore.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
private Channel acquireUploadChannel() throws InterruptedException {
  Promise<Channel> channelReady = eventLoop.next().newPromise();
  channelPool
      .acquire()
      .addListener(
          (Future<Channel> channelAcquired) -> {
            if (!channelAcquired.isSuccess()) {
              channelReady.setFailure(channelAcquired.cause());
              return;
            }

            try {
              Channel ch = channelAcquired.getNow();
              ChannelPipeline p = ch.pipeline();

              if (!isChannelPipelineEmpty(p)) {
                channelReady.setFailure(
                    new IllegalStateException("Channel pipeline is not empty."));
                return;
              }

              p.addLast(new HttpResponseDecoder());
              // The 10KiB limit was chosen at random. We only expect HTTP servers to respond with
              // an error message in the body and that should always be less than 10KiB.
              p.addLast(new HttpObjectAggregator(10 * 1024));
              p.addLast(new HttpRequestEncoder());
              p.addLast(new ChunkedWriteHandler());
              synchronized (credentialsLock) {
                p.addLast(new HttpUploadHandler(creds));
              }

              channelReady.setSuccess(ch);
            } catch (Throwable t) {
              channelReady.setFailure(t);
            }
          });

  try {
    return channelReady.get();
  } catch (ExecutionException e) {
    PlatformDependent.throwException(e.getCause());
    return null;
  }
}
 
Example 4
Source File: HttpCacheClient.java    From bazel with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
private Channel acquireUploadChannel() throws InterruptedException {
  Promise<Channel> channelReady = eventLoop.next().newPromise();
  channelPool
      .acquire()
      .addListener(
          (Future<Channel> channelAcquired) -> {
            if (!channelAcquired.isSuccess()) {
              channelReady.setFailure(channelAcquired.cause());
              return;
            }

            try {
              Channel ch = channelAcquired.getNow();
              ChannelPipeline p = ch.pipeline();

              if (!isChannelPipelineEmpty(p)) {
                channelReady.setFailure(
                    new IllegalStateException("Channel pipeline is not empty."));
                return;
              }

              p.addFirst(
                  "timeout-handler",
                  new IdleTimeoutHandler(timeoutSeconds, WriteTimeoutException.INSTANCE));
              p.addLast(new HttpResponseDecoder());
              // The 10KiB limit was chosen arbitrarily. We only expect HTTP servers to respond
              // with an error message in the body, and that should always be less than 10KiB. If
              // the response is larger than 10KiB, HttpUploadHandler will catch the
              // TooLongFrameException that HttpObjectAggregator throws and convert it to an
              // IOException.
              p.addLast(new HttpObjectAggregator(10 * 1024));
              p.addLast(new HttpRequestEncoder());
              p.addLast(new ChunkedWriteHandler());
              synchronized (credentialsLock) {
                p.addLast(new HttpUploadHandler(creds, extraHttpHeaders));
              }

              if (!ch.eventLoop().inEventLoop()) {
                // If addLast is called outside an event loop, then it doesn't complete until the
                // event loop is run again. In that case, a message sent to the last handler gets
                // delivered to the last non-pending handler, which will most likely end up
                // throwing UnsupportedMessageTypeException. Therefore, we only complete the
                // promise in the event loop.
                ch.eventLoop().execute(() -> channelReady.setSuccess(ch));
              } else {
                channelReady.setSuccess(ch);
              }
            } catch (Throwable t) {
              channelReady.setFailure(t);
            }
          });

  try {
    return channelReady.get();
  } catch (ExecutionException e) {
    PlatformDependent.throwException(e.getCause());
    return null;
  }
}