DefaultPromise.getNow() does not correctly handle DefaultPromise.setUncancellable() (#8154)
Motivation: We do not correctly check for previous calles of setUncancellable() in getNow() which may result in ClassCastException as we incorrectly return the internally UNCANCELLABLE object and not null if setUncancellable() we as called before. Modifications: Correctly check for UNCANCELLABLE and add unit test. Result: Fixes https://github.com/netty/netty/issues/8135.
This commit is contained in:
parent
952eeb8e1e
commit
0dc71cee3a
@ -301,7 +301,7 @@ public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V> {
|
|||||||
@Override
|
@Override
|
||||||
public V getNow() {
|
public V getNow() {
|
||||||
Object result = this.result;
|
Object result = this.result;
|
||||||
if (result instanceof CauseHolder || result == SUCCESS) {
|
if (result instanceof CauseHolder || result == SUCCESS || result == UNCANCELLABLE) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (V) result;
|
return (V) result;
|
||||||
|
@ -37,10 +37,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
|
|
||||||
import static java.lang.Math.max;
|
import static java.lang.Math.max;
|
||||||
import static org.hamcrest.Matchers.lessThan;
|
import static org.hamcrest.Matchers.lessThan;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertSame;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public class DefaultPromiseTest {
|
public class DefaultPromiseTest {
|
||||||
@ -258,6 +255,22 @@ public class DefaultPromiseTest {
|
|||||||
assertTrue(promise.isSuccess());
|
assertTrue(promise.isSuccess());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setUncancellableGetNow() {
|
||||||
|
final Promise<String> promise = new DefaultPromise<String>(ImmediateEventExecutor.INSTANCE);
|
||||||
|
assertNull(promise.getNow());
|
||||||
|
assertTrue(promise.setUncancellable());
|
||||||
|
assertNull(promise.getNow());
|
||||||
|
assertFalse(promise.isDone());
|
||||||
|
assertFalse(promise.isSuccess());
|
||||||
|
|
||||||
|
promise.setSuccess("success");
|
||||||
|
|
||||||
|
assertTrue(promise.isDone());
|
||||||
|
assertTrue(promise.isSuccess());
|
||||||
|
assertEquals("success", promise.getNow());
|
||||||
|
}
|
||||||
|
|
||||||
private static void testStackOverFlowChainedFuturesA(int promiseChainLength, final EventExecutor executor,
|
private static void testStackOverFlowChainedFuturesA(int promiseChainLength, final EventExecutor executor,
|
||||||
boolean runTestInExecutorThread)
|
boolean runTestInExecutorThread)
|
||||||
throws InterruptedException {
|
throws InterruptedException {
|
||||||
|
Loading…
Reference in New Issue
Block a user