Example usage for io.netty.channel.embedded EmbeddedChannel newPromise

List of usage examples for io.netty.channel.embedded EmbeddedChannel newPromise

Introduction

In this page you can find the example usage for io.netty.channel.embedded EmbeddedChannel newPromise.

Prototype

@Override
    public ChannelPromise newPromise() 

Source Link

Usage

From source file:com.google.devtools.build.lib.remote.blobstore.http.AbstractHttpHandlerTest.java

License:Open Source License

@Test
public void basicAuthShouldWork() throws Exception {
    URI uri = new URI("http://user:password@does.not.exist/foo");
    EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null));
    ByteArrayOutputStream out = Mockito.spy(new ByteArrayOutputStream());
    DownloadCommand cmd = new DownloadCommand(uri, true, "abcdef", new ByteArrayOutputStream());
    ChannelPromise writePromise = ch.newPromise();
    ch.writeOneOutbound(cmd, writePromise);

    HttpRequest request = ch.readOutbound();
    assertThat(request.headers().get(HttpHeaderNames.AUTHORIZATION)).isEqualTo("Basic dXNlcjpwYXNzd29yZA==");
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.AbstractHttpHandlerTest.java

License:Open Source License

@Test
public void basicAuthShouldNotEnabled() throws Exception {
    URI uri = new URI("http://does.not.exist/foo");
    EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null));
    ByteArrayOutputStream out = Mockito.spy(new ByteArrayOutputStream());
    DownloadCommand cmd = new DownloadCommand(uri, true, "abcdef", new ByteArrayOutputStream());
    ChannelPromise writePromise = ch.newPromise();
    ch.writeOneOutbound(cmd, writePromise);

    HttpRequest request = ch.readOutbound();
    assertThat(request.headers().contains(HttpHeaderNames.AUTHORIZATION)).isFalse();
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpDownloadHandlerTest.java

License:Open Source License

private void downloadShouldWork(boolean casDownload, EmbeddedChannel ch) throws IOException {
    ByteArrayOutputStream out = Mockito.spy(new ByteArrayOutputStream());
    DownloadCommand cmd = new DownloadCommand(CACHE_URI, casDownload, "abcdef", out);
    ChannelPromise writePromise = ch.newPromise();
    ch.writeOneOutbound(cmd, writePromise);

    HttpRequest request = ch.readOutbound();
    assertThat(request.method()).isEqualTo(HttpMethod.GET);
    assertThat(request.headers().get(HttpHeaderNames.HOST))
            .isEqualTo(CACHE_URI.getHost() + ":" + CACHE_URI.getPort());
    if (casDownload) {
        assertThat(request.uri()).isEqualTo("/cache-bucket/cas/abcdef");
    } else {//from  ww w .  j a v  a  2 s  .c om
        assertThat(request.uri()).isEqualTo("/cache-bucket/ac/abcdef");
    }

    assertThat(writePromise.isDone()).isFalse();

    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.headers().set(HttpHeaders.CONTENT_LENGTH, 5);
    response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    ch.writeInbound(response);
    ByteBuf content = Unpooled.buffer();
    content.writeBytes(new byte[] { 1, 2, 3, 4, 5 });
    ch.writeInbound(new DefaultLastHttpContent(content));

    assertThat(writePromise.isDone()).isTrue();
    assertThat(out.toByteArray()).isEqualTo(new byte[] { 1, 2, 3, 4, 5 });
    verify(out, never()).close();
    assertThat(ch.isActive()).isTrue();
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpDownloadHandlerTest.java

License:Open Source License

/** Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND). */
@Test/*from   w  ww.  j  a  va  2 s .  c  o m*/
public void httpErrorsAreSupported() throws IOException {
    EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null));
    ByteArrayOutputStream out = Mockito.spy(new ByteArrayOutputStream());
    DownloadCommand cmd = new DownloadCommand(CACHE_URI, true, "abcdef", out);
    ChannelPromise writePromise = ch.newPromise();
    ch.writeOneOutbound(cmd, writePromise);

    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
    response.headers().set(HttpHeaders.HOST, "localhost");
    response.headers().set(HttpHeaders.CONTENT_LENGTH, 0);
    response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    ch.writeInbound(response);
    ch.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
    assertThat(writePromise.isDone()).isTrue();
    assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
    assertThat(((HttpException) writePromise.cause()).response().status())
            .isEqualTo(HttpResponseStatus.NOT_FOUND);
    // No data should have been written to the OutputStream and it should have been closed.
    assertThat(out.size()).isEqualTo(0);
    // The caller is responsible for closing the stream.
    verify(out, never()).close();
    assertThat(ch.isOpen()).isTrue();
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpDownloadHandlerTest.java

License:Open Source License

/**
 * Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND) with a
 * Content-Length header.//from  w  w w.  j  a  va 2  s .c om
 */
@Test
public void httpErrorsWithContentAreSupported() throws IOException {
    EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null));
    ByteArrayOutputStream out = Mockito.spy(new ByteArrayOutputStream());
    DownloadCommand cmd = new DownloadCommand(CACHE_URI, true, "abcdef", out);
    ChannelPromise writePromise = ch.newPromise();
    ch.writeOneOutbound(cmd, writePromise);

    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
    ByteBuf errorMessage = ByteBufUtil.writeAscii(ch.alloc(), "Error message");
    response.headers().set(HttpHeaders.HOST, "localhost");
    response.headers().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(errorMessage.readableBytes()));
    response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.CLOSE);

    ch.writeInbound(response);
    // The promise must not be done because we haven't received the error message yet.
    assertThat(writePromise.isDone()).isFalse();

    ch.writeInbound(new DefaultHttpContent(errorMessage));
    ch.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
    assertThat(writePromise.isDone()).isTrue();
    assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
    assertThat(((HttpException) writePromise.cause()).response().status())
            .isEqualTo(HttpResponseStatus.NOT_FOUND);
    // No data should have been written to the OutputStream and it should have been closed.
    assertThat(out.size()).isEqualTo(0);
    // The caller is responsible for closing the stream.
    verify(out, never()).close();
    assertThat(ch.isOpen()).isFalse();
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpUploadHandlerTest.java

License:Open Source License

private void uploadsShouldWork(boolean casUpload, EmbeddedChannel ch, HttpResponseStatus status)
        throws Exception {
    ByteArrayInputStream data = new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, 5 });
    ChannelPromise writePromise = ch.newPromise();
    ch.writeOneOutbound(new UploadCommand(CACHE_URI, casUpload, "abcdef", data, 5), writePromise);

    HttpRequest request = ch.readOutbound();
    assertThat(request.method()).isEqualTo(HttpMethod.PUT);
    assertThat(request.headers().get(HttpHeaders.CONNECTION)).isEqualTo(HttpHeaderValues.KEEP_ALIVE.toString());

    HttpChunkedInput content = ch.readOutbound();
    assertThat(content.readChunk(ByteBufAllocator.DEFAULT).content().readableBytes()).isEqualTo(5);

    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
    response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

    ch.writeInbound(response);//from  w  ww .ja  v  a  2s. c o m

    assertThat(writePromise.isDone()).isTrue();
    assertThat(ch.isOpen()).isTrue();
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpUploadHandlerTest.java

License:Open Source License

/** Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND). */
@Test//from   w w w.j  ava 2s  . co m
public void httpErrorsAreSupported() {
    EmbeddedChannel ch = new EmbeddedChannel(new HttpUploadHandler(null));
    ByteArrayInputStream data = new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, 5 });
    ChannelPromise writePromise = ch.newPromise();
    ch.writeOneOutbound(new UploadCommand(CACHE_URI, true, "abcdef", data, 5), writePromise);

    HttpRequest request = ch.readOutbound();
    assertThat(request).isInstanceOf(HttpRequest.class);
    HttpChunkedInput content = ch.readOutbound();
    assertThat(content).isInstanceOf(HttpChunkedInput.class);

    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN);
    response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.CLOSE);

    ch.writeInbound(response);

    assertThat(writePromise.isDone()).isTrue();
    assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
    assertThat(((HttpException) writePromise.cause()).response().status())
            .isEqualTo(HttpResponseStatus.FORBIDDEN);
    assertThat(ch.isOpen()).isFalse();
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpUploadHandlerTest.java

License:Open Source License

/**
 * Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND) with a
 * Content-Length header.//from w  ww  .  ja  v a 2 s .c om
 */
@Test
public void httpErrorsWithContentAreSupported() {
    EmbeddedChannel ch = new EmbeddedChannel(new HttpUploadHandler(null));
    ByteArrayInputStream data = new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, 5 });
    ChannelPromise writePromise = ch.newPromise();
    ch.writeOneOutbound(new UploadCommand(CACHE_URI, true, "abcdef", data, 5), writePromise);

    HttpRequest request = ch.readOutbound();
    assertThat(request).isInstanceOf(HttpRequest.class);
    HttpChunkedInput content = ch.readOutbound();
    assertThat(content).isInstanceOf(HttpChunkedInput.class);

    ByteBuf errorMsg = ByteBufUtil.writeAscii(ch.alloc(), "error message");
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND,
            errorMsg);
    response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

    ch.writeInbound(response);

    assertThat(writePromise.isDone()).isTrue();
    assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
    assertThat(((HttpException) writePromise.cause()).response().status())
            .isEqualTo(HttpResponseStatus.NOT_FOUND);
    assertThat(ch.isOpen()).isTrue();
}

From source file:net.epsilony.utils.codec.modbus.ModbusMasterCodecTest.java

License:Open Source License

@Test
public void test() throws InterruptedException, ExecutionException {
    SimpModbusMasterChannelInitializer init = new SimpModbusMasterChannelInitializer();
    EmbeddedChannel channel = new EmbeddedChannel(init);
    ChannelPromise newPromise = channel.newPromise();
    System.out.println(channel.isRegistered());

    ModbusClientMaster master = new ModbusClientMaster() {
        @Override/*from  w w w  .  j  a  v  a2 s  .  co m*/
        protected ChannelFuture genConnectFuture() {
            this.initializer = init;
            newPromise.setSuccess();
            return newPromise;
        }
    };

    CompletableFuture<ModbusResponse> future = master
            .request(new ModbusRequest(0, 0, MF.readRegisters(ModbusRegisterType.COIL, 0, 10)));
    future.get();
}

From source file:org.elasticsearch.http.netty4.Netty4HttpPipeliningHandlerTests.java

License:Apache License

public void testPipeliningRequestsAreReleased() throws InterruptedException {
    final int numberOfRequests = 10;
    final EmbeddedChannel embeddedChannel = new EmbeddedChannel(
            new Netty4HttpPipeliningHandler(logger, numberOfRequests + 1));

    for (int i = 0; i < numberOfRequests; i++) {
        embeddedChannel.writeInbound(createHttpRequest("/" + i));
    }/* ww  w .j a  va2 s.c o m*/

    HttpPipelinedRequest<FullHttpRequest> inbound;
    ArrayList<HttpPipelinedRequest<FullHttpRequest>> requests = new ArrayList<>();
    while ((inbound = embeddedChannel.readInbound()) != null) {
        requests.add(inbound);
    }

    ArrayList<ChannelPromise> promises = new ArrayList<>();
    for (int i = 1; i < requests.size(); ++i) {
        ChannelPromise promise = embeddedChannel.newPromise();
        promises.add(promise);
        HttpPipelinedRequest<FullHttpRequest> pipelinedRequest = requests.get(i);
        Netty4HttpRequest nioHttpRequest = new Netty4HttpRequest(pipelinedRequest.getRequest(),
                pipelinedRequest.getSequence());
        Netty4HttpResponse resp = nioHttpRequest.createResponse(RestStatus.OK, BytesArray.EMPTY);
        embeddedChannel.writeAndFlush(resp, promise);
    }

    for (ChannelPromise promise : promises) {
        assertFalse(promise.isDone());
    }
    embeddedChannel.close().syncUninterruptibly();
    for (ChannelPromise promise : promises) {
        assertTrue(promise.isDone());
        assertTrue(promise.cause() instanceof ClosedChannelException);
    }
}