List of usage examples for io.netty.buffer ByteBuf refCnt
int refCnt();
From source file:org.apache.bookkeeper.util.ByteBufListTest.java
License:Apache License
@Test public void testRetain() throws Exception { ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b1.writerIndex(b1.capacity());/* www . j a v a 2 s . c o m*/ ByteBufList buf = ByteBufList.get(b1); assertEquals(1, buf.size()); assertEquals(128, buf.readableBytes()); assertEquals(b1, buf.getBuffer(0)); assertEquals(buf.refCnt(), 1); assertEquals(b1.refCnt(), 1); buf.retain(); assertEquals(buf.refCnt(), 2); assertEquals(b1.refCnt(), 1); buf.release(); assertEquals(buf.refCnt(), 1); assertEquals(b1.refCnt(), 1); buf.release(); assertEquals(buf.refCnt(), 0); assertEquals(b1.refCnt(), 0); }
From source file:org.apache.bookkeeper.util.ByteBufListTest.java
License:Apache License
@Test public void testEncoder() throws Exception { ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b1.writerIndex(b1.capacity());/*from ww w .j a v a2s. c om*/ ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b2.writerIndex(b2.capacity()); ByteBufList buf = ByteBufList.get(b1, b2); ChannelHandlerContext ctx = new MockChannelHandlerContext(); ByteBufList.ENCODER.write(ctx, buf, null); assertEquals(buf.refCnt(), 0); assertEquals(b1.refCnt(), 0); assertEquals(b2.refCnt(), 0); }
From source file:org.apache.flink.runtime.io.network.netty.InboundEnvelopeDecoderTest.java
License:Apache License
@Test public void testEncodeDecode() throws Exception { final EmbeddedChannel ch = new EmbeddedChannel(new OutboundEnvelopeEncoder(), new InboundEnvelopeDecoder(this.bufferProviderBroker)); when(this.bufferProviderBroker.getBufferProvider(anyJobId(), anyChannelId())) .thenReturn(this.bufferProvider); when(this.bufferProvider.requestBuffer(anyInt())).thenAnswer(new Answer<Object>() { @Override//from w w w .j av a 2s .c o m public Object answer(InvocationOnMock invocation) throws Throwable { // fulfill the buffer request return allocBuffer((Integer) invocation.getArguments()[0]); } }); // -------------------------------------------------------------------- Envelope[] envelopes = new Envelope[] { nextEnvelope(0), nextEnvelope(2), nextEnvelope(32768), nextEnvelope(3782, new TestEvent1(34872527)), nextEnvelope(88, new TestEvent1(8749653), new TestEvent1(365345)), nextEnvelope(0, new TestEvent2(34563456), new TestEvent1(598432), new TestEvent2(976293845)), nextEnvelope(23) }; ByteBuf buf = encode(ch, envelopes); // 1. complete ByteBuf as input int refCount = buf.retain().refCnt(); decodeAndVerify(ch, buf, envelopes); Assert.assertEquals(refCount - 1, buf.refCnt()); // 2. random slices buf.readerIndex(0); ByteBuf[] slices = randomSlices(buf); ch.writeInbound((Object[]) slices); for (ByteBuf slice : slices) { Assert.assertEquals(1, slice.refCnt()); } decodeAndVerify(ch, envelopes); buf.release(); }
From source file:org.apache.flink.runtime.io.network.netty.NettyMessageSerializationTest.java
License:Apache License
@Test public void testEncodeDecode() { {/*from w ww. j ava 2s.co m*/ Buffer buffer = spy( new Buffer(MemorySegmentFactory.allocateUnpooledSegment(1024), mock(BufferRecycler.class))); ByteBuffer nioBuffer = buffer.getNioBuffer(); for (int i = 0; i < 1024; i += 4) { nioBuffer.putInt(i); } NettyMessage.BufferResponse expected = new NettyMessage.BufferResponse(buffer, random.nextInt(), new InputChannelID()); NettyMessage.BufferResponse actual = encodeAndDecode(expected); // Verify recycle has been called on buffer instance verify(buffer, times(1)).recycle(); final ByteBuf retainedSlice = actual.getNettyBuffer(); // Ensure not recycled and same size as original buffer assertEquals(1, retainedSlice.refCnt()); assertEquals(1024, retainedSlice.readableBytes()); nioBuffer = retainedSlice.nioBuffer(); for (int i = 0; i < 1024; i += 4) { assertEquals(i, nioBuffer.getInt()); } // Release the retained slice actual.releaseBuffer(); assertEquals(0, retainedSlice.refCnt()); assertEquals(expected.sequenceNumber, actual.sequenceNumber); assertEquals(expected.receiverId, actual.receiverId); } { { IllegalStateException expectedError = new IllegalStateException(); InputChannelID receiverId = new InputChannelID(); NettyMessage.ErrorResponse expected = new NettyMessage.ErrorResponse(expectedError, receiverId); NettyMessage.ErrorResponse actual = encodeAndDecode(expected); assertEquals(expected.cause.getClass(), actual.cause.getClass()); assertEquals(expected.cause.getMessage(), actual.cause.getMessage()); assertEquals(receiverId, actual.receiverId); } { IllegalStateException expectedError = new IllegalStateException("Illegal illegal illegal"); InputChannelID receiverId = new InputChannelID(); NettyMessage.ErrorResponse expected = new NettyMessage.ErrorResponse(expectedError, receiverId); NettyMessage.ErrorResponse actual = encodeAndDecode(expected); assertEquals(expected.cause.getClass(), actual.cause.getClass()); assertEquals(expected.cause.getMessage(), actual.cause.getMessage()); assertEquals(receiverId, actual.receiverId); } { IllegalStateException expectedError = new IllegalStateException("Illegal illegal illegal"); NettyMessage.ErrorResponse expected = new NettyMessage.ErrorResponse(expectedError); NettyMessage.ErrorResponse actual = encodeAndDecode(expected); assertEquals(expected.cause.getClass(), actual.cause.getClass()); assertEquals(expected.cause.getMessage(), actual.cause.getMessage()); assertNull(actual.receiverId); assertTrue(actual.isFatalError()); } } { NettyMessage.PartitionRequest expected = new NettyMessage.PartitionRequest( new ResultPartitionID(new IntermediateResultPartitionID(), new ExecutionAttemptID()), random.nextInt(), new InputChannelID()); NettyMessage.PartitionRequest actual = encodeAndDecode(expected); assertEquals(expected.partitionId, actual.partitionId); assertEquals(expected.queueIndex, actual.queueIndex); assertEquals(expected.receiverId, actual.receiverId); } { NettyMessage.TaskEventRequest expected = new NettyMessage.TaskEventRequest( new IntegerTaskEvent(random.nextInt()), new ResultPartitionID(new IntermediateResultPartitionID(), new ExecutionAttemptID()), new InputChannelID()); NettyMessage.TaskEventRequest actual = encodeAndDecode(expected); assertEquals(expected.event, actual.event); assertEquals(expected.partitionId, actual.partitionId); assertEquals(expected.receiverId, actual.receiverId); } { NettyMessage.CancelPartitionRequest expected = new NettyMessage.CancelPartitionRequest( new InputChannelID()); NettyMessage.CancelPartitionRequest actual = encodeAndDecode(expected); assertEquals(expected.receiverId, actual.receiverId); } { NettyMessage.CloseRequest expected = new NettyMessage.CloseRequest(); NettyMessage.CloseRequest actual = encodeAndDecode(expected); assertEquals(expected.getClass(), actual.getClass()); } }
From source file:org.apache.flink.runtime.query.netty.KvStateClientHandlerTest.java
License:Apache License
/** * Tests that on reads the expected callback methods are called and read * buffers are recycled.//from ww w . jav a 2 s . c o m */ @Test public void testReadCallbacksAndBufferRecycling() throws Exception { KvStateClientHandlerCallback callback = mock(KvStateClientHandlerCallback.class); EmbeddedChannel channel = new EmbeddedChannel(new KvStateClientHandler(callback)); // // Request success // ByteBuf buf = KvStateRequestSerializer.serializeKvStateRequestResult(channel.alloc(), 1222112277, new byte[0]); buf.skipBytes(4); // skip frame length // Verify callback channel.writeInbound(buf); verify(callback, times(1)).onRequestResult(eq(1222112277L), any(byte[].class)); assertEquals("Buffer not recycled", 0, buf.refCnt()); // // Request failure // buf = KvStateRequestSerializer.serializeKvStateRequestFailure(channel.alloc(), 1222112278, new RuntimeException("Expected test Exception")); buf.skipBytes(4); // skip frame length // Verify callback channel.writeInbound(buf); verify(callback, times(1)).onRequestFailure(eq(1222112278L), any(RuntimeException.class)); assertEquals("Buffer not recycled", 0, buf.refCnt()); // // Server failure // buf = KvStateRequestSerializer.serializeServerFailure(channel.alloc(), new RuntimeException("Expected test Exception")); buf.skipBytes(4); // skip frame length // Verify callback channel.writeInbound(buf); verify(callback, times(1)).onFailure(any(RuntimeException.class)); // // Unexpected messages // buf = channel.alloc().buffer(4).writeInt(1223823); // Verify callback channel.writeInbound(buf); verify(callback, times(2)).onFailure(any(IllegalStateException.class)); assertEquals("Buffer not recycled", 0, buf.refCnt()); // // Exception caught // channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception")); verify(callback, times(3)).onFailure(any(RuntimeException.class)); // // Channel inactive // channel.pipeline().fireChannelInactive(); verify(callback, times(4)).onFailure(any(ClosedChannelException.class)); }
From source file:org.apache.flink.runtime.query.netty.KvStateServerHandlerTest.java
License:Apache License
/** * Tests that incoming buffer instances are recycled. *///from w w w . j a v a2s. c om @Test public void testIncomingBufferIsRecycled() throws Exception { KvStateRegistry registry = new KvStateRegistry(); AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats(); KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats); EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler); ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), 282872, new KvStateID(), new byte[0]); assertEquals(1, request.refCnt()); // Write regular request channel.writeInbound(request); assertEquals("Buffer not recycled", 0, request.refCnt()); // Write unexpected msg ByteBuf unexpected = channel.alloc().buffer(8); unexpected.writeInt(4); unexpected.writeInt(4); assertEquals(1, unexpected.refCnt()); channel.writeInbound(unexpected); assertEquals("Buffer not recycled", 0, unexpected.refCnt()); }
From source file:org.apache.pulsar.common.api.ByteBufPairTest.java
License:Apache License
@Test public void testDoubleByteBuf() throws Exception { ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b1.writerIndex(b1.capacity());//from w ww. jav a 2 s. c o m ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b2.writerIndex(b2.capacity()); ByteBufPair buf = ByteBufPair.get(b1, b2); assertEquals(buf.readableBytes(), 256); assertEquals(buf.getFirst(), b1); assertEquals(buf.getSecond(), b2); assertEquals(buf.refCnt(), 1); assertEquals(b1.refCnt(), 1); assertEquals(b2.refCnt(), 1); buf.release(); assertEquals(buf.refCnt(), 0); assertEquals(b1.refCnt(), 0); assertEquals(b2.refCnt(), 0); }
From source file:org.apache.pulsar.common.api.ByteBufPairTest.java
License:Apache License
@Test public void testEncoder() throws Exception { ByteBuf b1 = Unpooled.wrappedBuffer("hello".getBytes()); ByteBuf b2 = Unpooled.wrappedBuffer("world".getBytes()); ByteBufPair buf = ByteBufPair.get(b1, b2); assertEquals(buf.readableBytes(), 10); assertEquals(buf.getFirst(), b1);// w w w. j av a2 s. c o m assertEquals(buf.getSecond(), b2); assertEquals(buf.refCnt(), 1); assertEquals(b1.refCnt(), 1); assertEquals(b2.refCnt(), 1); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); when(ctx.write(any(), any())).then(invocation -> { // Simulate a write on the context which releases the buffer ((ByteBuf) invocation.getArguments()[0]).release(); return null; }); ByteBufPair.ENCODER.write(ctx, buf, null); assertEquals(buf.refCnt(), 0); assertEquals(b1.refCnt(), 0); assertEquals(b2.refCnt(), 0); }
From source file:org.apache.spark.network.protocol.MessageWithHeaderSuite.java
License:Apache License
@Test public void testByteBufBody() throws Exception { ByteBuf header = Unpooled.copyLong(42); ByteBuf bodyPassedToNettyManagedBuffer = Unpooled.copyLong(84); assertEquals(1, header.refCnt()); assertEquals(1, bodyPassedToNettyManagedBuffer.refCnt()); ManagedBuffer managedBuf = new NettyManagedBuffer(bodyPassedToNettyManagedBuffer); Object body = managedBuf.convertToNetty(); assertEquals(2, bodyPassedToNettyManagedBuffer.refCnt()); assertEquals(1, header.refCnt());//ww w . ja v a 2s . c o m MessageWithHeader msg = new MessageWithHeader(managedBuf, header, body, managedBuf.size()); ByteBuf result = doWrite(msg, 1); assertEquals(msg.count(), result.readableBytes()); assertEquals(42, result.readLong()); assertEquals(84, result.readLong()); assertTrue(msg.release()); assertEquals(0, bodyPassedToNettyManagedBuffer.refCnt()); assertEquals(0, header.refCnt()); }
From source file:org.apache.spark.network.protocol.MessageWithHeaderSuite.java
License:Apache License
@Test public void testDeallocateReleasesManagedBuffer() throws Exception { ByteBuf header = Unpooled.copyLong(42); ManagedBuffer managedBuf = Mockito.spy(new TestManagedBuffer(84)); ByteBuf body = (ByteBuf) managedBuf.convertToNetty(); assertEquals(2, body.refCnt()); MessageWithHeader msg = new MessageWithHeader(managedBuf, header, body, body.readableBytes()); assertTrue(msg.release());//from ww w . j a v a 2 s. co m Mockito.verify(managedBuf, Mockito.times(1)).release(); assertEquals(0, body.refCnt()); }