List of usage examples for io.netty.buffer ByteBuf refCnt
int refCnt();
From source file:com.heliosapm.streams.metrics.StreamValueTest.java
License:Open Source License
protected void testStreamedMetricValueIterator(final boolean single, final boolean release) { ByteBuf inBuffer = null; try {/*w ww.jav a2 s.co m*/ final int metricCount = 10000; inBuffer = BufferManager.getInstance().buffer(metricCount * 128); final Set<StreamedMetricValue> originals = new LinkedHashSet<StreamedMetricValue>(metricCount); for (int i = 0; i < metricCount; i++) { StreamedMetricValue smv = new StreamedMetricValue(System.currentTimeMillis(), nextPosDouble(), getRandomFragment(), randomTags(3)); originals.add(smv); smv.intoByteBuf(inBuffer); } Assert.assertEquals("Invalid number of samples", metricCount, originals.size()); final Iterator<StreamedMetricValue> originalsIter = originals.iterator(); final Iterator<StreamedMetricValue> iter = StreamedMetricValue .streamedMetricValues(single, inBuffer, release).iterator(); int loops = 0; while (originalsIter.hasNext()) { final StreamedMetricValue smv1 = originalsIter.next(); Assert.assertTrue("Buffered iterator had no next metric", iter.hasNext()); final StreamedMetricValue smv2 = iter.next(); assertEquals(smv1, smv2); // log(smv1); loops++; } Assert.assertFalse("Buffer iter should have no more metrics", iter.hasNext()); Assert.assertEquals("Invalid number of loops", metricCount, loops); if (release) { Assert.assertEquals("Invalid refCount on released buffer", 0, inBuffer.refCnt()); } else { Assert.assertEquals("Invalid refCount on released buffer", 1, inBuffer.refCnt()); } } finally { if (!release) inBuffer.release(); } }
From source file:com.ibm.crail.datanode.netty.NettyDataNode.java
License:Apache License
@Override public void run() throws Exception { int entries = (int) (NettyConstants.DATANODE_NETTY_STORAGE_LIMIT / NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE); map = new ConcurrentHashMap<Integer, ByteBuf>(entries); datanode = null;//w w w . j a va 2 s .com /* we start with stag 1 and increment it constantly */ stag = 1; LOG.info("Booting with " + entries + " nums of " + NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE + " byte buffers"); /* this manages the netty datanode which processes the client requests */ datanode = new NettyServer(getAddress(), this); datanode.start(); /* now the Namenode Processor communication part */ long allocated = 0; double perc; LOG.info("Allocation started for the target of : " + NettyConstants.DATANODE_NETTY_STORAGE_LIMIT); while (allocated < NettyConstants.DATANODE_NETTY_STORAGE_LIMIT) { /* allocate a new buffer */ ByteBuf buf = directBuffer((int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE, (int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE); /* retain this buffer */ buf.retain(); Long address = ((DirectBuffer) buf.nioBuffer()).address(); /* update entries */ map.put(this.stag, buf); this.setBlock(address, (int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE, this.stag); LOG.info("MAP entry : " + Long.toHexString(address) + " length : " + (int) NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE + " stag : " + this.stag + " refCount: " + buf.refCnt()); /* update counters */ allocated += NettyConstants.DATANODE_NETTY_ALLOCATION_SIZE; perc = allocated * 100 / NettyConstants.DATANODE_NETTY_STORAGE_LIMIT; this.stag++; LOG.info("Allocation done : " + perc + "% , allocated " + allocated + " / " + NettyConstants.DATANODE_NETTY_STORAGE_LIMIT); } while (true) { DataNodeStatistics statistics = this.getDataNode(); LOG.info("Datanode statistics, freeBlocks " + statistics.getFreeBlockCount()); Thread.sleep(2000); } /* now we wait for the other thread */ //datanode.join(); }
From source file:com.linecorp.armeria.client.encoding.AbstractStreamDecoderTest.java
License:Apache License
@Test public void decodedBufferShouldNotLeak() { final StreamDecoder decoder = newDecoder(); final ByteBuf buf = Unpooled.buffer(); decoder.decode(new ByteBufHttpData(buf, false)); assertThat(buf.refCnt()).isZero(); }
From source file:com.linecorp.armeria.common.stream.AbstractStreamMessageAndWriterTest.java
License:Apache License
@Test public void releaseWhenWritingToClosedStream_ByteBuf() { StreamMessageAndWriter<Object> stream = newStreamWriter(ImmutableList.of()); final ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer().retain(); stream.close();/*from w w w . j a va 2s. co m*/ await().untilAsserted(() -> assertThat(stream.isOpen()).isFalse()); assertThat(stream.tryWrite(buf)).isFalse(); assertThat(buf.refCnt()).isOne(); assertThatThrownBy(() -> stream.write(buf)).isInstanceOf(ClosedPublisherException.class); assertThat(buf.refCnt()).isZero(); }
From source file:com.linecorp.armeria.common.stream.AbstractStreamMessageAndWriterTest.java
License:Apache License
@Test public void releaseWhenWritingToClosedStream_ByteBuf_Supplier() { StreamMessageAndWriter<Object> stream = newStreamWriter(ImmutableList.of()); final ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer().retain(); stream.close();/*from w w w. jav a2 s . c o m*/ await().untilAsserted(() -> assertThat(stream.isOpen()).isFalse()); assertThat(stream.tryWrite(() -> buf)).isFalse(); assertThat(buf.refCnt()).isOne(); assertThatThrownBy(() -> stream.write(() -> buf)).isInstanceOf(ClosedPublisherException.class); assertThat(buf.refCnt()).isZero(); }
From source file:com.linecorp.armeria.common.stream.AbstractStreamMessageTest.java
License:Apache License
@Test public void releaseOnConsumption_ByteBuf() throws Exception { final ByteBuf buf = newPooledBuffer(); StreamMessage<ByteBuf> stream = newStream(ImmutableList.of(buf)); if (stream instanceof StreamWriter) { ((StreamWriter<ByteBuf>) stream).write(buf); ((StreamWriter<?>) stream).close(); }/*ww w. j a v a 2s . c o m*/ assertThat(buf.refCnt()).isEqualTo(1); stream.subscribe(new Subscriber<ByteBuf>() { @Override public void onSubscribe(Subscription subscription) { subscription.request(1); } @Override public void onNext(ByteBuf o) { assertThat(o).isNotSameAs(buf); assertThat(o).isInstanceOf(UnpooledHeapByteBuf.class); assertThat(o.refCnt()).isEqualTo(1); assertThat(buf.refCnt()).isZero(); } @Override public void onError(Throwable throwable) { Exceptions.throwUnsafely(throwable); } @Override public void onComplete() { completed = true; } }); await().untilAsserted(() -> assertThat(completed).isTrue()); }
From source file:com.linecorp.armeria.common.stream.DefaultStreamMessageTest.java
License:Apache License
@Test public void releaseOnConsumption_ByteBuf() throws Exception { final DefaultStreamMessage<ByteBuf> m = new DefaultStreamMessage<>(); final ByteBuf buf = newPooledBuffer(); assertThat(m.write(buf)).isTrue();/* w w w . j a va2 s.c o m*/ assertThat(buf.refCnt()).isEqualTo(1); m.subscribe(new Subscriber<ByteBuf>() { @Override public void onSubscribe(Subscription subscription) { subscription.request(1); } @Override public void onNext(ByteBuf o) { assertThat(o).isNotSameAs(buf); assertThat(o).isInstanceOf(UnpooledHeapByteBuf.class); assertThat(o.refCnt()).isEqualTo(1); assertThat(buf.refCnt()).isZero(); } @Override public void onError(Throwable throwable) { Exceptions.throwUnsafely(throwable); } @Override public void onComplete() { } }); }
From source file:com.linecorp.armeria.common.stream.DefaultStreamMessageTest.java
License:Apache License
@Test public void releaseWhenWritingToClosedStream_ByteBuf() { final DefaultStreamMessage<Object> m = new DefaultStreamMessage<>(); final ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(); m.close();// w w w . j a va2 s. c o m assertThat(m.write(buf)).isFalse(); assertThat(buf.refCnt()).isZero(); }
From source file:com.linecorp.armeria.common.stream.StreamMessageDuplicatorTest.java
License:Apache License
@Test public void lastDuplicateStream() { final DefaultStreamMessage<ByteBuf> publisher = new DefaultStreamMessage<>(); final ByteBufDuplicator duplicator = new ByteBufDuplicator(publisher); duplicator.duplicateStream().subscribe(new ByteBufSubscriber(), ImmediateEventExecutor.INSTANCE); duplicator.duplicateStream(true).subscribe(new ByteBufSubscriber(), ImmediateEventExecutor.INSTANCE); // duplicateStream() is not allowed anymore. assertThatThrownBy(duplicator::duplicateStream).isInstanceOf(IllegalStateException.class); // Only used to read refCnt, not an actual reference. final ByteBuf[] bufs = new ByteBuf[30]; for (int i = 0; i < 30; i++) { final ByteBuf buf = newUnpooledBuffer(); bufs[i] = buf;//from w w w. ja v a2 s . c om publisher.write(buf); assertThat(buf.refCnt()).isOne(); } for (int i = 0; i < 25; i++) { // first 25 signals are removed from the queue. assertThat(bufs[i].refCnt()).isZero(); } for (int i = 25; i < 30; i++) { // rest of them are still in the queue. assertThat(bufs[i].refCnt()).isOne(); } duplicator.close(); for (int i = 25; i < 30; i++) { // rest of them are cleared after calling duplicator.close() assertThat(bufs[i].refCnt()).isZero(); } }
From source file:com.linecorp.armeria.common.stream.StreamMessageDuplicatorTest.java
License:Apache License
@Test public void raiseExceptionInOnNext() { final DefaultStreamMessage<ByteBuf> publisher = new DefaultStreamMessage<>(); final ByteBufDuplicator duplicator = new ByteBufDuplicator(publisher); final ByteBuf buf = newUnpooledBuffer(); publisher.write(buf);//from w w w .j a v a2 s. c o m assertThat(buf.refCnt()).isOne(); // Release the buf after writing to the publisher which must not happen! buf.release(); final ByteBufSubscriber subscriber = new ByteBufSubscriber(); duplicator.duplicateStream().subscribe(subscriber, ImmediateEventExecutor.INSTANCE); assertThatThrownBy(() -> subscriber.completionFuture().get()) .hasCauseInstanceOf(IllegalReferenceCountException.class); }