List of usage examples for io.netty.buffer Unpooled copiedBuffer
private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset)
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);/*from www.j av a2 s. c om*/ 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);/*ww w .j a va 2 s .co 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 ww w . ja va 2 s. c o 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()); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldNotReleasePrependRequestContentOnRetry() 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 v a 2 s . c om*/ response.setStatus(KeyValueStatus.ERR_NOT_MY_VBUCKET.code()); 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(1, requestContent.refCnt()); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test(expected = CouchbaseException.class) public void shouldFailWhenOpaqueDoesNotMatch() throws Exception { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER, content);/* www.ja v a 2 s .c om*/ response.setStatus(BinaryMemcacheResponseStatus.SUCCESS); response.setOpaque(1); PrependRequest requestMock = mock(PrependRequest.class); ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); when(requestMock.bucket()).thenReturn("bucket"); AsyncSubject<CouchbaseResponse> responseSubject = AsyncSubject.<CouchbaseResponse>create(); when(requestMock.observable()).thenReturn(responseSubject); when(requestMock.content()).thenReturn(requestContent); when(requestMock.opaque()).thenReturn(3); requestQueue.add(requestMock); channel.writeInbound(response); assertEquals(0, content.refCnt()); responseSubject.toBlocking().single(); }
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 www . j a va 2 s . c om 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);//w ww . ja va 2s . c o 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 shouldDecodeEmptySuccessResponse() throws Exception { String response = Resources.read("success_0.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 va 2s . com*/ channel.writeInbound(responseHeader, responseChunk); latch.await(1, TimeUnit.SECONDS); assertEquals(1, firedEvents.size()); GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0); assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, FAKE_CLIENTID, "success", FAKE_SIGNATURE, new Action1<ByteBuf>() { @Override public void call(ByteBuf byteBuf) { fail("no result expected"); } }, new Action1<ByteBuf>() { @Override public void call(ByteBuf buf) { fail("no error expected"); } }, expectedMetricsCounts(0, 0)); }
From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java
License:Apache License
@Test public void shouldDecodeOneRowResponse() throws Exception { String response = Resources.read("success_1.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 av a 2s. co 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, FAKE_CLIENTID, "success", FAKE_SIGNATURE, new Action1<ByteBuf>() { @Override public void call(ByteBuf buf) { invokeCounter1.incrementAndGet(); String response = buf.toString(CharsetUtil.UTF_8); buf.release(); 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")); } catch (IOException e) { fail("no result expected"); } } }, new Action1<ByteBuf>() { @Override public void call(ByteBuf buf) { String error = buf.toString(CharsetUtil.UTF_8); buf.release(); fail("no error expected, got " + error); } }, expectedMetricsCounts(0, 1)); assertEquals(1, invokeCounter1.get()); }
From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java
License:Apache License
@Test public void shouldDecodeNRowResponse() throws Exception { String response = Resources.read("success_5.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 ww .jav a 2 s. c om*/ channel.writeInbound(responseHeader, responseChunk); latch.await(1, TimeUnit.SECONDS); assertEquals(1, firedEvents.size()); GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0); final AtomicInteger found = new AtomicInteger(0); assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, FAKE_CLIENTID, "success", FAKE_SIGNATURE, new Action1<ByteBuf>() { @Override public void call(ByteBuf row) { found.incrementAndGet(); String content = row.toString(CharsetUtil.UTF_8); row.release(); assertNotNull(content); assertTrue(!content.isEmpty()); try { Map decoded = mapper.readValue(content, Map.class); assertTrue(decoded.size() > 0); assertTrue(decoded.containsKey("name")); } catch (Exception e) { assertTrue(false); } } }, new Action1<ByteBuf>() { @Override public void call(ByteBuf buf) { fail("no error expected"); } }, expectedMetricsCounts(0, 5)); assertEquals(5, found.get()); }