List of usage examples for io.netty.util ReferenceCountUtil release
public static boolean release(Object msg)
From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java
License:Open Source License
@Test public void shouldDecodeGetNotFoundResponse() throws Exception { ByteBuf content = Unpooled.copiedBuffer("Not Found", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse("key", Unpooled.EMPTY_BUFFER, content.copy());//from w w w. ja va 2s . com response.setStatus(BinaryMemcacheResponseStatus.KEY_ENOENT); 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.NOT_EXISTS, inbound.status()); assertEquals("bucket", inbound.bucket()); assertEquals(0, inbound.cas()); assertEquals(0, inbound.flags()); assertEquals("Not Found", 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());// w ww . j av a2 s. co 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.binary.BinaryHandlerTest.java
License:Open Source License
@Test public void shouldDecodeGetBucketConfigResponse() throws Exception { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse("key", Unpooled.EMPTY_BUFFER, content.copy());/*from w ww. j av a 2 s.c o m*/ GetBucketConfigRequest requestMock = mock(GetBucketConfigRequest.class); when(requestMock.bucket()).thenReturn("bucket"); when(requestMock.hostname()).thenReturn(InetAddress.getLocalHost()); queue.add(requestMock); channel.writeInbound(response); channel.readInbound(); latch.await(1, TimeUnit.SECONDS); assertEquals(1, firedEvents.size()); GetBucketConfigResponse inbound = (GetBucketConfigResponse) firedEvents.get(0); assertEquals(ResponseStatus.SUCCESS, inbound.status()); assertEquals("bucket", inbound.bucket()); assertEquals(InetAddress.getLocalHost(), inbound.hostname()); 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 shouldEncodeInsertRequest() { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); InsertRequest request = new InsertRequest("key", content.copy(), "bucket"); request.partition((short) 512); channel.writeOutbound(request);/* w w w . j a v a 2s . com*/ FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_INSERT, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals(0, outbound.getCAS()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); request = new InsertRequest("key", content.copy(), 10, 0, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_INSERT, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(10, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); request = new InsertRequest("key", content.copy(), 0, 5, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_INSERT, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(5, outbound.getExtras().readInt()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); request = new InsertRequest("key", content.copy(), 30, 99, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_INSERT, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(99, outbound.getExtras().readInt()); assertEquals(30, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); ReferenceCountUtil.release(content); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldEncodeUpsertRequest() { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); UpsertRequest request = new UpsertRequest("key", content.copy(), "bucket"); request.partition((short) 512); channel.writeOutbound(request);/* w ww. j a v a 2s .c om*/ FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_UPSERT, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals(0, outbound.getCAS()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); request = new UpsertRequest("key", content.copy(), 10, 0, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_UPSERT, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(10, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); request = new UpsertRequest("key", content.copy(), 0, 5, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_UPSERT, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(5, outbound.getExtras().readInt()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); request = new UpsertRequest("key", content.copy(), 30, 99, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_UPSERT, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(99, outbound.getExtras().readInt()); assertEquals(30, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); ReferenceCountUtil.release(content); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldEncodeReplaceRequest() { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); ReplaceRequest request = new ReplaceRequest("key", content.copy(), "bucket"); request.partition((short) 512); channel.writeOutbound(request);/*from ww w . j a v a 2s.c o m*/ FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals(0, outbound.getCAS()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); request = new ReplaceRequest("key", content.copy(), 0, 10, 0, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(10, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); request = new ReplaceRequest("key", content.copy(), 0, 0, 5, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(5, outbound.getExtras().readInt()); assertEquals(0, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); request = new ReplaceRequest("key", content.copy(), 0, 30, 99, "bucket"); request.partition((short) 512); channel.writeOutbound(request); outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertNotNull(outbound); assertEquals("key", new String(outbound.getKey(), CHARSET)); assertEquals("key".length(), outbound.getKeyLength()); assertEquals(512, outbound.getReserved()); assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode()); assertEquals(8, outbound.getExtrasLength()); assertEquals(99, outbound.getExtras().readInt()); assertEquals(30, outbound.getExtras().readInt()); assertEquals(18, outbound.getTotalBodyLength()); assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8)); ReferenceCountUtil.releaseLater(outbound); ReferenceCountUtil.release(content); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldEncodeReplaceWithCASRequest() { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); ReplaceRequest request = new ReplaceRequest("key", content.copy(), 4234234234L, 30, 99, "bucket"); request.partition((short) 512); channel.writeOutbound(request);//from ww w .j a v a2s . c om FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound(); assertEquals(4234234234L, outbound.getCAS()); ReferenceCountUtil.releaseLater(outbound); ReferenceCountUtil.release(content); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldDecodeGetBucketConfigResponse() throws Exception { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER, content.copy());/*from w w w . j a v a2 s .c om*/ GetBucketConfigRequest requestMock = mock(GetBucketConfigRequest.class); when(requestMock.bucket()).thenReturn("bucket"); when(requestMock.hostname()).thenReturn(InetAddress.getLocalHost()); requestQueue.add(requestMock); channel.writeInbound(response); assertEquals(1, eventSink.responseEvents().size()); GetBucketConfigResponse event = (GetBucketConfigResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(BUCKET, event.bucket()); assertEquals(ResponseStatus.SUCCESS, event.status()); assertEquals(InetAddress.getLocalHost(), event.hostname()); assertEquals("content", event.content().toString(CHARSET)); ReferenceCountUtil.release(content); }
From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java
License:Apache License
@Test public void testBigChunkedResponseWithEscapedBackslashInRowObject() throws Exception { String response = Resources.read("chunkedResponseWithDoubleBackslashes.txt", this.getClass()); String[] chunks = response.split("(?m)^[0-9a-f]+"); HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(200, "OK")); responseHeader.headers().add("Transfer-Encoding", "chunked"); responseHeader.headers().add("Content-Type", "application/json; version=1.0.0"); Object[] httpChunks = new Object[chunks.length]; httpChunks[0] = responseHeader;/*from w ww. j ava2 s . c o m*/ for (int i = 1; i < chunks.length; i++) { String chunk = chunks[i]; if (i == chunks.length - 1) { httpChunks[i] = new DefaultLastHttpContent(Unpooled.copiedBuffer(chunk, CharsetUtil.UTF_8)); } else { httpChunks[i] = new DefaultHttpContent(Unpooled.copiedBuffer(chunk, CharsetUtil.UTF_8)); } } Subject<CouchbaseResponse, CouchbaseResponse> obs = AsyncSubject.create(); GenericQueryRequest requestMock = mock(GenericQueryRequest.class); when(requestMock.observable()).thenReturn(obs); queue.add(requestMock); channel.writeInbound(httpChunks); GenericQueryResponse inbound = (GenericQueryResponse) obs.timeout(1, TimeUnit.SECONDS).toBlocking().last(); final AtomicInteger found = new AtomicInteger(0); final AtomicInteger errors = new AtomicInteger(0); inbound.rows().timeout(5, TimeUnit.SECONDS).toBlocking().forEach(new Action1<ByteBuf>() { @Override public void call(ByteBuf byteBuf) { int rowNumber = found.incrementAndGet(); String content = byteBuf.toString(CharsetUtil.UTF_8); byteBuf.release(); assertNotNull(content); assertFalse(content.isEmpty()); } }); inbound.errors().timeout(5, TimeUnit.SECONDS).toBlocking().forEach(new Action1<ByteBuf>() { @Override public void call(ByteBuf buf) { buf.release(); errors.incrementAndGet(); } }); //ignore signature ReferenceCountUtil.release(inbound.signature().timeout(5, TimeUnit.SECONDS).toBlocking().single()); String status = inbound.queryStatus().timeout(5, TimeUnit.SECONDS).toBlocking().single(); List<ByteBuf> metricList = inbound.info().timeout(1, TimeUnit.SECONDS).toList().toBlocking().single(); assertEquals(1, metricList.size()); ByteBuf metricsBuf = metricList.get(0); ReferenceCountUtil.releaseLater(metricsBuf); Map metrics = mapper.readValue(metricsBuf.toString(CharsetUtil.UTF_8), Map.class); assertEquals("success", status); assertEquals(5, found.get()); assertEquals(0, errors.get()); assertEquals(found.get(), metrics.get("resultCount")); }
From source file:com.couchbase.client.core.utils.Buffers.java
License:Apache License
/** * Wrap an observable and free a reference counted item if unsubscribed in the meantime. * * This can and should be used if a hot observable is used as the source but it is not guaranteed that * there will always be a subscriber that consumes the reference counted item. If an item is emitted * by the source observable and no subscriber is attached (because it unsubscribed) the item will * be freed.//ww w . jav a2s .c o m * * Note that also non reference counted items can be passed in, but there is no impact other than * making it cold (in which case defer could be used). * * It is very important that if subscribed, the caller needs to release the reference counted item. * It wil only be released on behalf of the caller when unsubscribed. * * @param source the source observable to wrap. * @return the wrapped cold observable with refcnt release logic. */ public static <T> Observable<T> wrapColdWithAutoRelease(final Observable<T> source) { return Observable.create(new Observable.OnSubscribe<T>() { @Override public void call(final Subscriber<? super T> subscriber) { source.subscribe(new Subscriber<T>() { @Override public void onCompleted() { if (!subscriber.isUnsubscribed()) { subscriber.onCompleted(); } } @Override public void onError(Throwable e) { if (!subscriber.isUnsubscribed()) { subscriber.onError(e); } } @Override public void onNext(T t) { if (!subscriber.isUnsubscribed()) { subscriber.onNext(t); } else { ReferenceCountUtil.release(t); } } }); } }); }