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

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

Introduction

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

Prototype

@Override
    public boolean isOpen() 

Source Link

Usage

From source file:com.github.ambry.rest.HealthCheckHandlerTest.java

License:Open Source License

/**
 * Does a test to see that a health check request results in expected response from the health check handler
 * @param httpMethod the {@link HttpMethod} for the request.
 * @param keepAlive true if keep alive has to be set in the request, false otherwise
 * @throws IOException/* ww  w  .ja va2  s.  c o m*/
 */
private void testHealthCheckRequest(HttpMethod httpMethod, boolean isServiceUp, boolean keepAlive)
        throws IOException {
    EmbeddedChannel channel = createChannel();
    for (int i = 0; i < 2; i++) {
        if (isServiceUp) {
            restServerState.markServiceUp();
        }
        HttpRequest request = RestTestUtils.createRequest(httpMethod, healthCheckUri, null);
        HttpHeaders.setKeepAlive(request, keepAlive);
        FullHttpResponse response = sendRequestAndGetResponse(channel, request);
        HttpResponseStatus httpResponseStatus = (isServiceUp) ? HttpResponseStatus.OK
                : HttpResponseStatus.SERVICE_UNAVAILABLE;
        assertEquals("Unexpected response status", httpResponseStatus, response.getStatus());
        String expectedStr = (isServiceUp) ? goodStr : badStr;
        assertEquals("Unexpected content", expectedStr, RestTestUtils.getContentString(response));
        restServerState.markServiceDown();
        if (keepAlive && isServiceUp) {
            Assert.assertTrue("Channel should not be closed ", channel.isOpen());
        } else {
            Assert.assertFalse("Channel should have been closed by now ", channel.isOpen());
            channel = createChannel();
        }
    }
    channel.close();
}

From source file:com.github.ambry.rest.NettyMessageProcessorTest.java

License:Open Source License

/**
 * Does a test to see that request handling with good input succeeds when channel is not keep alive.
 * @param httpMethod the {@link HttpMethod} for the request.
 * @param restMethod the equivalent {@link RestMethod} for {@code httpMethod}. Used to check for correctness of
 *                   response.//w  w  w  .j  a  v  a 2 s. c o  m
 * @throws IOException
 */
private void doRequestHandleWithoutKeepAlive(HttpMethod httpMethod, RestMethod restMethod) throws IOException {
    EmbeddedChannel channel = createChannel();
    sendRequestCheckResponse(channel, httpMethod, restMethod, false);
    assertFalse("Channel not closed", channel.isOpen());
}

From source file:com.github.ambry.rest.NettyMessageProcessorTest.java

License:Open Source License

/**
 * Does a test to see that request handling with good input succeeds when channel is keep alive.
 * @param channel the {@link EmbeddedChannel} to use.
 * @param httpMethod the {@link HttpMethod} for the request.
 * @param restMethod the equivalent {@link RestMethod} for {@code httpMethod}. Used to check for correctness of
 *                   response./*w w  w .ja va  2  s . co  m*/
 * @throws IOException
 */
private void doRequestHandleWithKeepAlive(EmbeddedChannel channel, HttpMethod httpMethod, RestMethod restMethod)
        throws IOException {
    for (int i = 0; i < 5; i++) {
        sendRequestCheckResponse(channel, httpMethod, restMethod, true);
        assertTrue("Channel is closed", channel.isOpen());
    }
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Tests that the underlying network channel is closed when {@link NettyResponseChannel#close()} is called.
 *//*  ww w  .jav a2  s  .  co  m*/
@Test
public void closeTest() {
    // request is keep-alive by default.
    HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.Close.toString());
    EmbeddedChannel channel = createEmbeddedChannel();
    channel.writeInbound(request);

    // drain the channel of content.
    while (channel.readOutbound() != null) {
    }
    assertFalse("Channel should be closed", channel.isOpen());
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Tests for the request handling flow with transfer encoding chunked
 *//*from w  w  w .ja va 2s .c  o  m*/
@Test
public void doRequestHandleWithChunkedResponse() throws IOException {
    EmbeddedChannel channel = createChannel();
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add(EchoMethodHandler.IS_CHUNKED, "true");
    HttpRequest request = RestTestUtils.createRequest(HttpMethod.POST, "POST", headers);
    HttpHeaders.setKeepAlive(request, true);
    sendRequestCheckResponse(channel, request, "POST", headers, false, true);
    Assert.assertTrue("Channel should not be closed ", channel.isOpen());
    channel.close();
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Does a test to see that request handling results in expected entries in public access log
 * @param httpMethod the {@link HttpMethod} for the request.
 * @param uri Uri to be used during the request
 * @param testErrorCase true if error case has to be tested, false otherwise
 * @throws IOException/*from   w  ww . j  a  va 2 s  .co m*/
 */
private void doRequestHandleTest(HttpMethod httpMethod, String uri, boolean testErrorCase) throws IOException {
    EmbeddedChannel channel = createChannel();
    List<HttpHeaders> httpHeadersList = getHeadersList();
    for (HttpHeaders headers : httpHeadersList) {
        HttpRequest request = RestTestUtils.createRequest(httpMethod, uri, headers);
        HttpHeaders.setKeepAlive(request, true);
        sendRequestCheckResponse(channel, request, uri, headers, testErrorCase, false);
        if (!testErrorCase) {
            Assert.assertTrue("Channel should not be closed ", channel.isOpen());
        } else {
            Assert.assertFalse("Channel should have been closed ", channel.isOpen());
            channel = createChannel();
        }
    }
    channel.close();
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Does a test to see that two consecutive request handling results in expected entries in public access log
 * with keep alive// ww  w.  j av  a2 s  . com
 * @param httpMethod the {@link HttpMethod} for the request.
 * @param uri Uri to be used during the request
 * @throws IOException
 */
private void doRequestHandleWithKeepAliveTest(HttpMethod httpMethod, String uri) throws IOException {
    EmbeddedChannel channel = createChannel();
    // contains one logged request header
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add(HttpHeaders.Names.CONTENT_LENGTH, new Random().nextLong());

    HttpRequest request = RestTestUtils.createRequest(httpMethod, uri, headers);
    HttpHeaders.setKeepAlive(request, true);
    sendRequestCheckResponse(channel, request, uri, headers, false, false);
    Assert.assertTrue("Channel should not be closed ", channel.isOpen());

    // contains one logged and not logged header
    headers = new DefaultHttpHeaders();
    headers.add(NOT_LOGGED_HEADER_KEY + "1", "headerValue1");
    headers.add(HttpHeaders.Names.CONTENT_LENGTH, new Random().nextLong());

    request = RestTestUtils.createRequest(httpMethod, uri, headers);
    HttpHeaders.setKeepAlive(request, true);
    sendRequestCheckResponse(channel, request, uri, headers, false, false);
    Assert.assertTrue("Channel should not be closed ", channel.isOpen());
    channel.close();
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Does a test to see that two consecutive requests without sending last http content for first request fails
 * @param httpMethod the {@link HttpMethod} for the request.
 * @param uri Uri to be used during the request
 * @throws IOException/*from  w  ww.  ja  va2 s. c  o m*/
 */
private void doRequestHandleWithMultipleRequest(HttpMethod httpMethod, String uri) throws IOException {
    EmbeddedChannel channel = createChannel();
    // contains one logged request header
    HttpHeaders headers1 = new DefaultHttpHeaders();
    headers1.add(HttpHeaders.Names.CONTENT_LENGTH, new Random().nextLong());

    HttpRequest request = RestTestUtils.createRequest(httpMethod, uri, headers1);
    HttpHeaders.setKeepAlive(request, true);
    channel.writeInbound(request);

    // contains one logged and not logged header
    HttpHeaders headers2 = new DefaultHttpHeaders();
    headers2.add(NOT_LOGGED_HEADER_KEY + "1", "headerValue1");
    headers2.add(HttpHeaders.Names.CONTENT_LENGTH, new Random().nextLong());
    // sending another request w/o sending last http content
    request = RestTestUtils.createRequest(httpMethod, uri, headers2);
    HttpHeaders.setKeepAlive(request, true);
    sendRequestCheckResponse(channel, request, uri, headers2, false, false);
    Assert.assertTrue("Channel should not be closed ", channel.isOpen());

    // verify that headers from first request is not found in public access log
    String lastLogEntry = publicAccessLogger.getLastPublicAccessLogEntry();
    // verify request headers
    verifyPublicAccessLogEntryForRequestHeaders(lastLogEntry, headers1, request.getMethod(), false);

    channel.close();
}

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  w  w  .  j a  va2 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./*  ww  w  .j  a v  a 2  s.  co m*/
 */
@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();
}