Skip to content

Commit

Permalink
JAVA-2204: NettyByteBuf.duplicate now calls retain on the duplicated …
Browse files Browse the repository at this point in the history
…proxied ByteBuf
  • Loading branch information
jyemin committed Jun 7, 2016
1 parent 79a6e93 commit ba9a644
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Expand Up @@ -208,7 +208,7 @@ public ByteBuf asReadOnly() {

@Override
public ByteBuf duplicate() {
return new NettyByteBuf(proxied.duplicate(), isWriting);
return new NettyByteBuf(proxied.duplicate().retain(), isWriting);
}

@Override
Expand Down
Expand Up @@ -83,4 +83,69 @@ class ByteBufSpecification extends Specification {
then:
thrown(UnsupportedOperationException)
}

def 'should manage reference count of proxied Netty ByteBuf correctly'() {
given:
def nettyBuf = ByteBufAllocator.DEFAULT.buffer(16)

when:
def buf = new NettyByteBuf(nettyBuf)

then:
nettyBuf.refCnt() == 1

when:
buf.retain()

then:
nettyBuf.refCnt() == 2

when:
buf.release()

then:
nettyBuf.refCnt() == 1

when:
buf.release()

then:
nettyBuf.refCnt() == 0
}

def 'should manage reference count of duplicated proxied Netty ByteBuf correctly'() {
given:
def nettyBuf = ByteBufAllocator.DEFAULT.buffer(16)
def buf = new NettyByteBuf(nettyBuf)

when:
def duplicated = buf.duplicate()

then:
nettyBuf.refCnt() == 2

when:
buf.retain()

then:
nettyBuf.refCnt() == 3

when:
buf.release()

then:
nettyBuf.refCnt() == 2

when:
duplicated.release()

then:
nettyBuf.refCnt() == 1

when:
buf.release()

then:
nettyBuf.refCnt() == 0
}
}

0 comments on commit ba9a644

Please sign in to comment.