Example usage for io.netty.buffer ByteBufHolder refCnt

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

Introduction

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

Prototype

int refCnt();

Source Link

Document

Returns the reference count of this object.

Usage

From source file:com.linecorp.armeria.common.stream.AbstractStreamMessageTest.java

License:Apache License

@Test
public void releaseOnConsumption_HttpData() throws Exception {
    final ByteBufHttpData data = new ByteBufHttpData(newPooledBuffer(), false);
    StreamMessage<ByteBufHolder> stream = newStream(ImmutableList.of(data));

    if (stream instanceof StreamWriter) {
        ((StreamWriter<ByteBufHolder>) stream).write(data);
        ((StreamWriter<?>) stream).close();
    }/* www  .  ja v a  2  s. co  m*/
    assertThat(data.refCnt()).isEqualTo(1);

    stream.subscribe(new Subscriber<ByteBufHolder>() {
        @Override
        public void onSubscribe(Subscription subscription) {
            subscription.request(1);
        }

        @Override
        public void onNext(ByteBufHolder o) {
            assertThat(o).isNotSameAs(data);
            assertThat(o).isInstanceOf(ByteBufHttpData.class);
            assertThat(o.content()).isInstanceOf(UnpooledHeapByteBuf.class);
            assertThat(o.refCnt()).isEqualTo(1);
            assertThat(data.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_HttpData() throws Exception {
    final DefaultStreamMessage<ByteBufHolder> m = new DefaultStreamMessage<>();
    final ByteBufHttpData data = new ByteBufHttpData(newPooledBuffer(), false);

    assertThat(m.write(data)).isTrue();//from   w ww  . j  a v a 2 s. co  m
    assertThat(data.refCnt()).isEqualTo(1);

    m.subscribe(new Subscriber<ByteBufHolder>() {
        @Override
        public void onSubscribe(Subscription subscription) {
            subscription.request(1);
        }

        @Override
        public void onNext(ByteBufHolder o) {
            assertThat(o).isNotSameAs(data);
            assertThat(o).isInstanceOf(ByteBufHttpData.class);
            assertThat(o.content()).isInstanceOf(UnpooledHeapByteBuf.class);
            assertThat(o.refCnt()).isEqualTo(1);
            assertThat(data.refCnt()).isZero();
        }

        @Override
        public void onError(Throwable throwable) {
            Exceptions.throwUnsafely(throwable);
        }

        @Override
        public void onComplete() {
        }
    });
}