Example usage for io.netty.buffer EmptyByteBuf EmptyByteBuf

List of usage examples for io.netty.buffer EmptyByteBuf EmptyByteBuf

Introduction

In this page you can find the example usage for io.netty.buffer EmptyByteBuf EmptyByteBuf.

Prototype

public EmptyByteBuf(ByteBufAllocator alloc) 

Source Link

Usage

From source file:io.grpc.netty.NettyServerStreamTest.java

License:Apache License

@Test
public void closeAfterClientHalfCloseShouldSucceed() throws Exception {
    ListMultimap<CharSequence, CharSequence> expectedHeaders = ImmutableListMultimap
            .copyOf(new DefaultHttp2Headers().status(new AsciiString("200"))
                    .set(new AsciiString("content-type"), new AsciiString("application/grpc"))
                    .set(new AsciiString("grpc-status"), new AsciiString("0")));

    // Client half-closes. Listener gets halfClosed()
    stream().transportState().inboundDataReceived(new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT), true);

    verify(serverListener).halfClosed();

    // Server closes. Status sent
    stream().close(Status.OK, trailers);
    assertNull("no message expected", listenerMessageQueue.poll());

    ArgumentCaptor<SendResponseHeadersCommand> cmdCap = ArgumentCaptor
            .forClass(SendResponseHeadersCommand.class);
    verify(writeQueue).enqueue(cmdCap.capture(), eq(true));
    SendResponseHeadersCommand cmd = cmdCap.getValue();
    assertThat(cmd.stream()).isSameInstanceAs(stream.transportState());
    assertThat(ImmutableListMultimap.copyOf(cmd.headers())).containsExactlyEntriesIn(expectedHeaders);
    assertThat(cmd.endOfStream()).isTrue();

    // Sending and receiving complete. Listener gets closed()
    stream().transportState().complete();
    verify(serverListener).closed(Status.OK);
    assertNull("no message expected", listenerMessageQueue.poll());
}

From source file:io.grpc.netty.NettyServerStreamTest.java

License:Apache License

@Test
public void abortStreamAfterClientHalfCloseShouldCallClose() {
    Status status = Status.INTERNAL.withCause(new Throwable());
    // Client half-closes. Listener gets halfClosed()
    stream().transportState().inboundDataReceived(new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT), true);
    verify(serverListener).halfClosed();
    // Abort from the transport layer
    stream().transportState().transportReportStatus(status);
    verify(serverListener).closed(same(status));
    assertNull("no message expected", listenerMessageQueue.poll());
}

From source file:io.netty.buffer.EmptyByteBufTest.java

License:Apache License

@Test
public void testIsWritable() {
    EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
    Assert.assertFalse(empty.isWritable());
    Assert.assertFalse(empty.isWritable(1));
}

From source file:io.netty.buffer.EmptyByteBufTest.java

License:Apache License

@Test
public void testIsReadable() {
    EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
    Assert.assertFalse(empty.isReadable());
    Assert.assertFalse(empty.isReadable(1));
}

From source file:io.netty.buffer.EmptyByteBufTest.java

License:Apache License

@Test
public void testArray() {
    EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
    assertThat(empty.hasArray(), is(true));
    assertThat(empty.array().length, is(0));
    assertThat(empty.arrayOffset(), is(0));
}

From source file:io.netty.buffer.EmptyByteBufTest.java

License:Apache License

@Test
public void testNioBuffer() {
    EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
    assertThat(empty.nioBufferCount(), is(1));
    assertThat(empty.nioBuffer().position(), is(0));
    assertThat(empty.nioBuffer().limit(), is(0));
    assertThat(empty.nioBuffer(), is(sameInstance(empty.nioBuffer())));
    assertThat(empty.nioBuffer(), is(sameInstance(empty.internalNioBuffer(0, 0))));
}

From source file:io.netty.buffer.EmptyByteBufTest.java

License:Apache License

@Test
public void testMemoryAddress() {
    EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
    if (empty.hasMemoryAddress()) {
        assertThat(empty.memoryAddress(), is(not(0L)));
    } else {//from   www.ja  v  a2s . c o  m
        try {
            empty.memoryAddress();
            fail();
        } catch (UnsupportedOperationException ignored) {
            // Ignore.
        }
    }
}

From source file:org.ebayopensource.scc.cache.NettyResponseSerializerTest.java

License:Apache License

@Test
public void testSerialize() {
    NettyResponseSerializer serializer = new NettyResponseSerializer();

    DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK);
    CacheResponse cacheRes = serializer.serialize(response);
    assertNotNull(cacheRes);//from  w  w  w  .  j  av a 2  s.c  o  m
    assertEquals(HttpVersion.HTTP_1_0.toString(), cacheRes.getProtocalVersion());
    assertEquals(HttpResponseStatus.OK.code(), cacheRes.getCode());
    assertEquals(0, cacheRes.getContent().length);
    assertTrue(cacheRes.getHeaders().get(0).getKey().equals("Content-Length"));
    assertTrue(cacheRes.getTrailingHeaders().isEmpty());

    ByteBuf content = new EmptyByteBuf(ByteBufAllocator.DEFAULT);
    response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, content);
    cacheRes = serializer.serialize(response);
    assertEquals(0, cacheRes.getContent().length);

    content = UnpooledByteBufAllocator.DEFAULT.buffer();
    content.writeBytes("Hello, World!".getBytes());
    response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, content);
    cacheRes = serializer.serialize(response);
    assertEquals("Hello, World!", new String(cacheRes.getContent()));

    HttpHeaders headers = response.headers();
    headers.add("header1", "value1");

    HttpHeaders trailingHeaders = response.trailingHeaders();
    trailingHeaders.add("tHeader2", "value2");

    cacheRes = serializer.serialize(response);
    Entry<String, String> header = cacheRes.getHeaders().get(0);
    assertEquals("header1", header.getKey());
    assertEquals("value1", header.getValue());

    Entry<String, String> tHeader = cacheRes.getTrailingHeaders().get(0);
    assertEquals("tHeader2", tHeader.getKey());
    assertEquals("value2", tHeader.getValue());
}

From source file:org.ebayopensource.scc.filter.NettyRequestProxyFilterTest.java

License:Apache License

@Test
public void testFilterRequest() throws IOException {
    AppConfiguration appConfig = new AppConfiguration(new ConfigLoader(), null);
    appConfig.init();//from ww w . j  a va2  s  .c  om

    PolicyManager policyManager = mock(PolicyManager.class);
    NettyRequestProxyFilter filter = new NettyRequestProxyFilter(policyManager, appConfig);

    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.attr(any(AttributeKey.class))).thenReturn(mock(Attribute.class));
    assertNull(filter.filterRequest(mock(HttpRequest.class), ctx));

    DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
            "http://test.ebay.com/s/");
    when(policyManager.cacheIsNeededFor(any(CacheDecisionObject.class))).thenReturn(false);
    assertNull(filter.filterRequest(req, ctx));

    when(policyManager.cacheIsNeededFor(any(CacheDecisionObject.class))).thenReturn(true);
    CacheManager cm = mock(CacheManager.class);
    when(policyManager.getCacheManager()).thenReturn(cm);
    assertNull(filter.filterRequest(req, ctx));

    FullHttpResponse resp = mock(FullHttpResponse.class);
    HttpHeaders respHeaders = mock(HttpHeaders.class);
    when(resp.headers()).thenReturn(respHeaders);
    when(respHeaders.get(any(CharSequence.class))).thenReturn("100");
    when(cm.get(anyString())).thenReturn(resp);
    Channel channel = mock(Channel.class);
    SocketChannelConfig config = mock(SocketChannelConfig.class);
    when(channel.config()).thenReturn(config);
    when(ctx.channel()).thenReturn(channel);
    req.headers().add("h1", "v1");

    when(resp.content()).thenReturn(new EmptyByteBuf(new PooledByteBufAllocator()))
            .thenReturn(Unpooled.copiedBuffer("Hello".getBytes()));
    assertEquals(resp, filter.filterRequest(req, ctx));
    assertEquals(resp, filter.filterRequest(req, ctx));
}