Example usage for io.netty.buffer ByteBuf refCnt

List of usage examples for io.netty.buffer ByteBuf refCnt

Introduction

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

Prototype

int refCnt();

Source Link

Document

Returns the reference count of this object.

Usage

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldIgnoreNodeWithoutKVServiceEnabled() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    BucketConfig config = mock(BucketConfig.class);

    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.provider(provider);//from ww w.j ava 2 s  .  c  o  m

    when(config.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();

    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "1.2.3.4:8091", ports));
    nodeInfos.add(new DefaultNodeInfo(null, "6.7.8.9:8091", new HashMap<String, Integer>()));
    nodeInfos.add(new DefaultNodeInfo(null, "2.3.4.5:8091", ports));
    when(config.nodes()).thenReturn(nodeInfos);

    ByteBuf content = Unpooled.copiedBuffer("{\"config\": true}", CharsetUtil.UTF_8);
    Observable<CouchbaseResponse> goodResponse = Observable
            .just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                    KeyValueStatus.SUCCESS.code(), "bucket", content, InetAddress.getByName("1.2.3.4")));
    Observable<CouchbaseResponse> badResponse = Observable.error(new CouchbaseException("Failure"));
    when(cluster.send(any(GetBucketConfigRequest.class))).thenReturn(badResponse, goodResponse);
    refresher.markTainted(config);

    Thread.sleep(1500);

    verify(provider, times(1)).proposeBucketConfig("bucket", "{\"config\": true}");
    assertEquals(0, content.refCnt());
}

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

License:Apache License

/**
 * Helper method to safely release the content.
 *
 * @param content the content to safely release if needed.
 *///from   w ww . j a  va 2 s .  c  om
private static void releaseContent(ByteBuf content) {
    if (content != null && content.refCnt() > 0) {
        content.release();
    }
}

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());
    channel.writeOutbound(request);//from www .  jav a  2 s.  co m
    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 shouldRetainContentOnAppend() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("foo", CharsetUtil.UTF_8);
    AppendRequest request = new AppendRequest("key", 0, content, "bucket");
    request.partition((short) 1);

    assertEquals(1, content.refCnt());
    channel.writeOutbound(request);//from   www  .  ja  va  2  s  .  com
    assertEquals(2, content.refCnt());
}

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

License:Apache License

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

    assertEquals(1, content.refCnt());
    channel.writeOutbound(request);/*from  w ww.  java 2  s .c o m*/
    assertEquals(2, content.refCnt());
}

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

License:Apache License

@Test
public void shouldReleaseStoreRequestContentOnSuccess() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);//  www . j  av  a  2s .  c  o m
    response.setStatus(BinaryMemcacheResponseStatus.SUCCESS);

    UpsertRequest requestMock = mock(UpsertRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create());
    when(requestMock.content()).thenReturn(requestContent);
    requestQueue.add(requestMock);

    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
    channel.writeInbound(response);
    assertEquals(1, content.refCnt());
    assertEquals(0, requestContent.refCnt());
}

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

License:Apache License

@Test
public void shouldNotReleaseStoreRequestContentOnRetry() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);//  w  ww. j a v  a  2  s  .  com
    response.setStatus(KeyValueStatus.ERR_NOT_MY_VBUCKET.code());

    UpsertRequest requestMock = mock(UpsertRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create());
    when(requestMock.content()).thenReturn(requestContent);
    requestQueue.add(requestMock);

    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
    channel.writeInbound(response);
    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
}

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

License:Apache License

@Test
public void shouldReleaseAppendRequestContentOnSuccess() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);/*w w  w .j ava2  s.co m*/
    response.setStatus(BinaryMemcacheResponseStatus.SUCCESS);

    AppendRequest requestMock = mock(AppendRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create());
    when(requestMock.content()).thenReturn(requestContent);
    requestQueue.add(requestMock);

    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
    channel.writeInbound(response);
    assertEquals(1, content.refCnt());
    assertEquals(0, requestContent.refCnt());
}

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

License:Apache License

@Test
public void shouldNotReleaseAppendRequestContentOnRetry() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);//from   w  w w . j  a va2s  .c o m
    response.setStatus(KeyValueStatus.ERR_NOT_MY_VBUCKET.code());

    AppendRequest requestMock = mock(AppendRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create());
    requestQueue.add(requestMock);

    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
    channel.writeInbound(response);
    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
}

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

License:Apache License

@Test
public void shouldReleasePrependRequestContentOnSuccess() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);/*from   w  w  w .j a  va  2  s  .  co  m*/
    response.setStatus(BinaryMemcacheResponseStatus.SUCCESS);

    PrependRequest requestMock = mock(PrependRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create());
    when(requestMock.content()).thenReturn(requestContent);
    requestQueue.add(requestMock);

    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
    channel.writeInbound(response);
    assertEquals(1, content.refCnt());
    assertEquals(0, requestContent.refCnt());
}