Example usage for io.netty.util ReferenceCountUtil releaseLater

List of usage examples for io.netty.util ReferenceCountUtil releaseLater

Introduction

In this page you can find the example usage for io.netty.util ReferenceCountUtil releaseLater.

Prototype

@Deprecated
public static <T> T releaseLater(T msg) 

Source Link

Document

Schedules the specified object to be released when the caller thread terminates.

Usage

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

License:Apache License

@Test
public void shouldEncodeNegativeCounterRequest() {
    CounterRequest request = new CounterRequest("key", 5, -10, 15, "bucket");
    request.partition((short) 1);

    channel.writeOutbound(request);/*from   w  w w.  j  a va 2 s.c o  m*/
    BinaryMemcacheRequest outbound = (BinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals("key".length() + 20, outbound.getTotalBodyLength());
    assertEquals(20, outbound.getExtrasLength());
    assertEquals(10, outbound.getExtras().readLong());
    assertEquals(5, outbound.getExtras().readLong());
    assertEquals(15, outbound.getExtras().readInt());
    assertEquals(KeyValueHandler.OP_COUNTER_DECR, outbound.getOpcode());
    ReferenceCountUtil.releaseLater(outbound);
}

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

License:Apache License

@Test
public void shouldEncodeTouchRequest() {
    TouchRequest request = new TouchRequest("key", 30, "bucket");
    request.partition((short) 1);

    channel.writeOutbound(request);//from w  ww  .ja va2 s  .  c o  m
    BinaryMemcacheRequest outbound = (BinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals("key".length() + 4, outbound.getTotalBodyLength());
    assertEquals(1, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_TOUCH, outbound.getOpcode());
    assertEquals(4, outbound.getExtrasLength());
    assertEquals(30, outbound.getExtras().readInt());
    assertEquals(0, outbound.getCAS());
    ReferenceCountUtil.releaseLater(outbound);
}

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

License:Apache License

@Test
public void shouldEncodeUnlockRequest() {
    UnlockRequest request = new UnlockRequest("key", 234234234L, "bucket");
    request.partition((short) 1);

    channel.writeOutbound(request);//from w  ww. ja va 2  s .  c  o  m
    BinaryMemcacheRequest outbound = (BinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals("key".length(), outbound.getTotalBodyLength());
    assertEquals(1, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_UNLOCK, outbound.getOpcode());
    assertEquals(0, outbound.getExtrasLength());
    assertEquals(234234234L, outbound.getCAS());
    ReferenceCountUtil.releaseLater(outbound);
}

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

License:Apache License

@Test
public void shouldEncodeObserveRequest() {
    ObserveRequest request = new ObserveRequest("key", 234234234L, true, (short) 0, "bucket");
    request.partition((short) 1);

    channel.writeOutbound(request);/*from w  w  w.  j  a v  a 2 s .co m*/
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals(0, outbound.getKeyLength());
    assertEquals(0, outbound.getExtrasLength());
    assertEquals(7, outbound.getTotalBodyLength());
    assertEquals(KeyValueHandler.OP_OBSERVE, outbound.getOpcode());
    assertEquals(1, outbound.content().readShort());
    assertEquals("key".length(), outbound.content().readShort());
    assertEquals("key",
            outbound.content().readBytes(outbound.content().readableBytes()).toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);
}

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

License:Apache License

@Test
public void shouldRetainContentOnStore() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("foo", CharsetUtil.UTF_8);
    UpsertRequest request = new UpsertRequest("key", content, "bucket");
    request.partition((short) 1);

    assertEquals(1, content.refCnt());/*from w ww  .  j a va  2 s  .c o m*/
    channel.writeOutbound(request);
    assertEquals(2, content.refCnt());
    ReferenceCountUtil.releaseLater(content); //release content first time
    ReferenceCountUtil.releaseLater(channel.readOutbound()); //releases extra once + content second time
}

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

License:Apache License

@Test
public void shouldNotRetainContentOnObserve() throws Exception {
    ObserveRequest request = new ObserveRequest("key", 0, false, (short) 1, "bucket");
    request.partition((short) 1);

    channel.writeOutbound(request);/*from  w w w .j  ava 2 s  .  com*/
    FullBinaryMemcacheRequest written = (FullBinaryMemcacheRequest) channel.readOutbound();

    assertEquals(1, written.content().refCnt());
    ReferenceCountUtil.releaseLater(written);
}

From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java

License:Apache License

/**
 *
 * @param inbound//w  w  w. j a  va2  s  .  com
 * @param expectedSuccess
 * @param expectedStatus
 * @param expectedRequestId
 * @param expectedClientId
 * @param expectedFinalStatus
 * @param expectedSignature
 * @param assertRows
 * @param assertErrors
 * @param metricsToCheck null to expect no metrics
 */
private void assertResponse(GenericQueryResponse inbound, boolean expectedSuccess,
        ResponseStatus expectedStatus, String expectedRequestId, String expectedClientId,
        String expectedFinalStatus, String expectedSignature, Action1<ByteBuf> assertRows,
        Action1<ByteBuf> assertErrors, Map<String, Object> metricsToCheck) {
    assertEquals(expectedSuccess, inbound.status().isSuccess());
    assertEquals(expectedStatus, inbound.status());
    assertEquals(expectedRequestId, inbound.requestId());
    assertEquals(expectedClientId, inbound.clientRequestId());

    assertEquals(expectedFinalStatus, inbound.queryStatus().timeout(1, TimeUnit.SECONDS).toBlocking().single());

    inbound.rows().timeout(5, TimeUnit.SECONDS).toBlocking().forEach(assertRows);

    List<ByteBuf> metricList = inbound.info().timeout(1, TimeUnit.SECONDS).toList().toBlocking().single();
    if (metricsToCheck == null) {
        assertEquals(0, metricList.size());
    } else {
        assertEquals(1, metricList.size());
        String metricsJson = metricList.get(0).toString(CharsetUtil.UTF_8);
        metricList.get(0).release();
        try {
            Map metrics = mapper.readValue(metricsJson, Map.class);
            assertEquals(7, metrics.size());

            for (Map.Entry<String, Object> entry : metricsToCheck.entrySet()) {
                assertNotNull(metrics.get(entry.getKey()));
                assertEquals(entry.getKey(), entry.getValue(), metrics.get(entry.getKey()));
            }
        } catch (IOException e) {
            fail(e.toString());
        }
    }

    inbound.errors().timeout(1, TimeUnit.SECONDS).toBlocking().forEach(assertErrors);

    List<ByteBuf> signatureList = inbound.signature().timeout(1, TimeUnit.SECONDS).toList().toBlocking()
            .single();
    if (expectedSignature != null) {
        assertEquals(1, signatureList.size());
        String signatureJson = signatureList.get(0).toString(CharsetUtil.UTF_8);
        assertNotNull(signatureJson);
        assertEquals(expectedSignature, signatureJson.replaceAll("\\s", ""));
        ReferenceCountUtil.releaseLater(signatureList.get(0));
    } else {
        assertEquals(0, signatureList.size());
    }
}

From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java

License:Apache License

@Test
public void shouldDecodeErrorResponse() throws Exception {
    String response = Resources.read("parse_error.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8));

    GenericQueryRequest requestMock = mock(GenericQueryRequest.class);
    queue.add(requestMock);//from w  w  w .  ja v a2 s .  c  o  m
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    assertResponse(inbound, false, ResponseStatus.FAILURE, FAKE_REQUESTID, FAKE_CLIENTID, "fatal", null,
            new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf byteBuf) {
                    fail("no result expected");
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    String response = buf.toString(CharsetUtil.UTF_8);
                    try {
                        Map error = mapper.readValue(response, Map.class);
                        assertEquals(5, error.size());
                        assertEquals(new Integer(4100), error.get("code"));
                        assertEquals(Boolean.FALSE, error.get("temp"));
                        assertEquals("Parse Error", error.get("msg"));
                    } catch (IOException e) {
                        fail();
                    }
                    ReferenceCountUtil.releaseLater(buf);
                }
            }, expectedMetricsCounts(1, 0));
}

From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java

License:Apache License

@Test
public void shouldDecodeChunkedErrorResponse() throws Exception {
    String response = Resources.read("parse_error.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk1 = new DefaultHttpContent(
            Unpooled.copiedBuffer(response.substring(0, 223), CharsetUtil.UTF_8));
    HttpContent responseChunk2 = new DefaultLastHttpContent(
            Unpooled.copiedBuffer(response.substring(223), CharsetUtil.UTF_8));

    GenericQueryRequest requestMock = mock(GenericQueryRequest.class);
    queue.add(requestMock);/*from  w  w w . j a  v  a2  s  . co m*/
    channel.writeInbound(responseHeader, responseChunk1, responseChunk2);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    assertResponse(inbound, false, ResponseStatus.FAILURE, FAKE_REQUESTID, FAKE_CLIENTID, "fatal", null,
            new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf byteBuf) {
                    ReferenceCountUtil.releaseLater(byteBuf);
                    fail("no result expected");
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    String response = buf.toString(CharsetUtil.UTF_8);
                    ReferenceCountUtil.releaseLater(buf);
                    try {
                        Map error = mapper.readValue(response, Map.class);
                        assertEquals(5, error.size());
                        assertEquals(new Integer(4100), error.get("code"));
                        assertEquals(Boolean.FALSE, error.get("temp"));
                        assertEquals("Parse Error", error.get("msg"));
                    } catch (IOException e) {
                        fail();
                    }
                }
            }, expectedMetricsCounts(1, 0));
}

From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java

License:Apache License

@Test
public void shouldDecodeOneRowResponseWithQuotesInClientIdAndResults() throws Exception {
    String expectedClientIdWithQuotes = "ThisIsA\\\"Client\\\"Id";

    String response = Resources.read("with_escaped_quotes.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8));

    GenericQueryRequest requestMock = mock(GenericQueryRequest.class);
    queue.add(requestMock);/*from www  .j ava 2  s.c o  m*/
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    final AtomicInteger invokeCounter1 = new AtomicInteger();
    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, expectedClientIdWithQuotes, "success",
            FAKE_SIGNATURE, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    invokeCounter1.incrementAndGet();
                    String response = buf.toString(CharsetUtil.UTF_8);
                    ReferenceCountUtil.releaseLater(buf);
                    try {
                        Map found = mapper.readValue(response, Map.class);
                        assertEquals(12, found.size());
                        assertEquals("San Francisco", found.get("city"));
                        assertEquals("United States", found.get("country"));
                        Map geo = (Map) found.get("geo");
                        assertNotNull(geo);
                        assertEquals(3, geo.size());
                        assertEquals("ROOFTOP", geo.get("accuracy"));
                        //TODO check the quote in the result
                    } catch (IOException e) {
                        assertFalse(true);
                    }
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    fail("no error expected");
                }
            }, expectedMetricsCounts(0, 1));
    assertEquals(1, invokeCounter1.get());
}