Fix the error message in ReferenceCounted.release (#7921)

Motivation:

When a buffer is over-released, the current error message of `IllegalReferenceCountException` is `refCnt: XXX, increment: XXX`, which is confusing. The correct message should be `refCnt: XXX, decrement: XXX`.

Modifications:

Pass `-decrement` to create `IllegalReferenceCountException`.

Result:

The error message will be `refCnt: XXX, decrement: XXX` when a buffer is over-released.
This commit is contained in:
Xiaoyan Lin 2018-05-08 11:09:16 -07:00 committed by Norman Maurer
parent 437849bd93
commit a0ed6ec06c
4 changed files with 28 additions and 2 deletions

View File

@ -97,7 +97,7 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
} else if (oldRef < decrement || oldRef - decrement > oldRef) {
// Ensure we don't over-release, and avoid underflow.
refCntUpdater.getAndAdd(this, decrement);
throw new IllegalReferenceCountException(oldRef, decrement);
throw new IllegalReferenceCountException(oldRef, -decrement);
}
return false;
}

View File

@ -29,6 +29,7 @@ import java.nio.channels.ScatteringByteChannel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class AbstractReferenceCountedByteBufTest {
@ -55,6 +56,18 @@ public class AbstractReferenceCountedByteBufTest {
referenceCounted.release(Integer.MAX_VALUE);
}
@Test
public void testReleaseErrorMessage() {
AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();
assertTrue(referenceCounted.release());
try {
referenceCounted.release(1);
fail("IllegalReferenceCountException didn't occur");
} catch (IllegalReferenceCountException e) {
assertEquals("refCnt: 0, decrement: 1", e.getMessage());
}
}
@Test(expected = IllegalReferenceCountException.class)
public void testRetainResurrect() {
AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();

View File

@ -84,7 +84,7 @@ public abstract class AbstractReferenceCounted implements ReferenceCounted {
} else if (oldRef < decrement || oldRef - decrement > oldRef) {
// Ensure we don't over-release, and avoid underflow.
refCntUpdater.getAndAdd(this, decrement);
throw new IllegalReferenceCountException(oldRef, decrement);
throw new IllegalReferenceCountException(oldRef, -decrement);
}
return false;
}

View File

@ -19,6 +19,7 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class AbstractReferenceCountedTest {
@ -45,6 +46,18 @@ public class AbstractReferenceCountedTest {
referenceCounted.release(Integer.MAX_VALUE);
}
@Test
public void testReleaseErrorMessage() {
AbstractReferenceCounted referenceCounted = newReferenceCounted();
assertTrue(referenceCounted.release());
try {
referenceCounted.release(1);
fail("IllegalReferenceCountException didn't occur");
} catch (IllegalReferenceCountException e) {
assertEquals("refCnt: 0, decrement: 1", e.getMessage());
}
}
@Test(expected = IllegalReferenceCountException.class)
public void testRetainResurrect() {
AbstractReferenceCounted referenceCounted = newReferenceCounted();