Example usage for io.netty.buffer Unpooled buffer

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

Introduction

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

Prototype

public static ByteBuf buffer() 

Source Link

Document

Creates a new big-endian Java heap buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

Usage

From source file:com.corundumstudio.socketio.parser.EncoderMessagePacketTest.java

License:Apache License

@Test
public void testEncodeWithIdAndEndpoint() throws IOException {
    Packet packet = new Packet(PacketType.MESSAGE);
    //        packet.setId(5L);
    //        packet.setAck(true);
    packet.setNsp("/tobi");
    ByteBuf result = Unpooled.buffer();
    //        encoder.encodePacket(packet, result);
    Assert.assertEquals("3:5:/tobi", result.toString(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.parser.PayloadTest.java

License:Apache License

@Test
public void testPayloadEncode() throws IOException {
    Queue<Packet> packets = new ConcurrentLinkedQueue<Packet>();
    Packet packet1 = new Packet(PacketType.MESSAGE);
    packet1.setData("5");
    packets.add(packet1);/*from   w  ww  .j  a va  2s .  c om*/

    Packet packet2 = new Packet(PacketType.MESSAGE);
    packet2.setData("53d");
    packets.add(packet2);

    ByteBuf result = Unpooled.buffer();
    //        encoder.encodePackets(packets, result, UnpooledByteBufAllocator.DEFAULT);
    Assert.assertEquals("\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d", result.toString(CharsetUtil.UTF_8));
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldDecodeGetSuccessResponse() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse("key", Unpooled.EMPTY_BUFFER,
            content.copy());//  ww w  .  j  a  v  a2 s. c  o  m
    response.setCAS(123456789L);
    response.setExtras(Unpooled.buffer().writeInt(123));
    response.setExtrasLength((byte) 4);

    GetRequest requestMock = mock(GetRequest.class);
    when(requestMock.bucket()).thenReturn("bucket");
    queue.add(requestMock);
    channel.writeInbound(response);
    channel.readInbound();

    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GetResponse inbound = (GetResponse) firedEvents.get(0);
    assertEquals(ResponseStatus.SUCCESS, inbound.status());
    assertEquals("bucket", inbound.bucket());
    assertEquals(123456789L, inbound.cas());
    assertEquals(123, inbound.flags());
    assertEquals("content", inbound.content().toString(CharsetUtil.UTF_8));
    assertTrue(queue.isEmpty());

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldDecodeReplicaGetResponse() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse("key", Unpooled.EMPTY_BUFFER,
            content.copy());/*from w  w w . ja v  a2 s . c o m*/
    response.setCAS(123456789L);
    response.setExtras(Unpooled.buffer().writeInt(123));
    response.setExtrasLength((byte) 4);

    ReplicaGetRequest requestMock = mock(ReplicaGetRequest.class);
    when(requestMock.bucket()).thenReturn("bucket");
    queue.add(requestMock);
    channel.writeInbound(response);
    channel.readInbound();

    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GetResponse inbound = (GetResponse) firedEvents.get(0);
    assertEquals(ResponseStatus.SUCCESS, inbound.status());
    assertEquals("bucket", inbound.bucket());
    assertEquals(123456789L, inbound.cas());
    assertEquals(123, inbound.flags());
    assertEquals("content", inbound.content().toString(CharsetUtil.UTF_8));
    assertTrue(queue.isEmpty());

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldDecodeSuccessfulGet() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content.copy());/*from  w  w  w  .ja v a  2  s . c o  m*/
    response.setCAS(123456789L);
    response.setExtras(Unpooled.buffer().writeInt(123));
    response.setExtrasLength((byte) 4);

    GetRequest requestMock = mock(GetRequest.class);
    when(requestMock.bucket()).thenReturn(BUCKET);
    requestQueue.add(requestMock);
    channel.writeInbound(response);

    assertEquals(1, eventSink.responseEvents().size());
    GetResponse event = (GetResponse) eventSink.responseEvents().get(0).getMessage();
    assertEquals(123456789L, event.cas());
    assertEquals(123, event.flags());
    assertEquals("content", event.content().toString(CHARSET));
    assertEquals(BUCKET, event.bucket());
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldDecodeReplicaGetResponse() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content.copy());/*from  www. j  a v  a2 s  . c o m*/
    response.setCAS(123456789L);
    response.setExtras(Unpooled.buffer().writeInt(123));
    response.setExtrasLength((byte) 4);

    ReplicaGetRequest requestMock = mock(ReplicaGetRequest.class);
    when(requestMock.bucket()).thenReturn(BUCKET);
    requestQueue.add(requestMock);
    channel.writeInbound(response);

    assertEquals(1, eventSink.responseEvents().size());
    GetResponse event = (GetResponse) eventSink.responseEvents().get(0).getMessage();
    assertEquals(123456789L, event.cas());
    assertEquals(123, event.flags());
    assertEquals("content", event.content().toString(CHARSET));
    assertEquals(BUCKET, event.bucket());
}

From source file:com.couchbase.client.core.utils.BuffersTest.java

License:Apache License

@Test
public void shouldReleaseIfUnsubscribed() {
    ReplaySubject<ByteBuf> source = ReplaySubject.create();
    Observable<ByteBuf> wrapped = Buffers.wrapColdWithAutoRelease(source);

    for (int i = 0; i < 5; i++) {
        source.onNext(Unpooled.buffer());
    }// w w  w.  ja  v  a2 s  .co m
    source.onCompleted();

    source.toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            assertEquals(1, byteBuf.refCnt());
        }
    });

    wrapped.take(2).toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            byteBuf.release();
        }
    });

    source.toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            assertEquals(0, byteBuf.refCnt());
        }
    });

}

From source file:com.couchbase.client.core.utils.UnicastAutoReleaseSubjectTest.java

License:Apache License

@Test
public void testByteBufReleaseWithNoTimeout() throws Exception {
    UnicastAutoReleaseSubject<ByteBuf> subject = UnicastAutoReleaseSubject.createWithoutNoSubscriptionTimeout();
    ByteBuf buffer = Unpooled.buffer();
    Assert.assertEquals("Created byte buffer not retained.", 1, buffer.refCnt());
    subject.onNext(buffer);//from   ww  w  .  j  av a  2  s  .  co m
    subject.onCompleted();
    final AtomicInteger byteBufRefCnt = new AtomicInteger(-1);

    ByteBuf last = subject.doOnNext(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            byteBufRefCnt.set(byteBuf.refCnt());
            byteBuf.release();// Simulate consumption as ByteBuf refcount is 1 when created.
        }
    }).toBlocking().last();

    Assert.assertEquals("Unexpected ByteBuf ref count when received.", 1, byteBufRefCnt.get());
    Assert.assertSame("Unexpected byte buffer received.", buffer, last);
    Assert.assertEquals("Byte buffer not released.", 0, last.refCnt());
}

From source file:com.couchbase.client.core.utils.UnicastAutoReleaseSubjectTest.java

License:Apache License

@Test
public void testByteBufReleaseWithTimeout() throws Exception {
    TestScheduler testScheduler = Schedulers.test();
    UnicastAutoReleaseSubject<ByteBuf> subject = UnicastAutoReleaseSubject.create(100, TimeUnit.MILLISECONDS,
            testScheduler);/*from   w w  w  .j a  v  a2 s.co m*/
    ByteBuf buffer = Unpooled.buffer();

    subject.onNext(buffer);
    Assert.assertEquals("Byte buffer not retained on buffering by subject.", 1, buffer.refCnt());

    subject.onCompleted();

    testScheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
    Assert.assertEquals("Byte buffer not fully released", 0, buffer.refCnt());
}

From source file:com.digisky.outerproxy.testclient.HttpSnoopClient.java

License:Apache License

public static void testWithEncode(Channel ch, String uri, String sjson) {
    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
    request.headers().set(HttpHeaders.Names.HOST, "127.0.0.1");
    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

    request.setMethod(HttpMethod.POST);//ww  w. j  a v  a  2 s.c o  m
    request.setUri("/" + uri);
    HttpPostRequestEncoder bodyRequestEncoder = null;
    try {
        bodyRequestEncoder = new HttpPostRequestEncoder(request, false);
    } catch (ErrorDataEncoderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // false => not multipart
      //***********************************************************************
    ByteBuf b = Unpooled.buffer();
    b.writeBytes("{}".getBytes());
    try {
        bodyRequestEncoder.addBodyAttribute("val", sjson);
    } catch (ErrorDataEncoderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //***********************************************************************
    try {
        request = bodyRequestEncoder.finalizeRequest();
    } catch (ErrorDataEncoderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ch.writeAndFlush(request);
    try {
        ch.closeFuture().sync();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}