Skip to content

Commit 04df4e2

Browse files
author
Stephane Landelle
committed
AHC Future.get should throw CancellationException when it was cancelled, close AsyncHttpClient#633
1 parent db341f1 commit 04df4e2

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

api/src/test/java/org/asynchttpclient/async/AsyncProvidersBasicTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ public Response onCompleted(Response response) throws Exception {
834834
}
835835
}
836836

837-
@Test(groups = { "standalone", "default_provider", "async" })
837+
@Test(groups = { "standalone", "default_provider", "async" }, expectedExceptions = { CancellationException.class })
838838
public void asyncDoPostDelayCancelTest() throws Exception {
839839
AsyncHttpClient client = getAsyncHttpClient(null);
840840
try {
@@ -850,8 +850,8 @@ public void onThrowable(Throwable t) {
850850
}
851851
});
852852
future.cancel(true);
853-
Response response = future.get(TIMEOUT, TimeUnit.SECONDS);
854-
assertNull(response);
853+
future.get(TIMEOUT, TimeUnit.SECONDS);
854+
855855
} finally {
856856
client.close();
857857
}

providers/netty3/src/main/java/org/asynchttpclient/providers/netty3/future/NettyResponseFuture.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public NettyResponseFuture(Uri uri,//
113113

114114
@Override
115115
public boolean isDone() {
116-
return isDone.get() || isCancelled.get();
116+
return isDone.get() || isCancelled();
117117
}
118118

119119
@Override
@@ -161,6 +161,9 @@ public V get(long l, TimeUnit tu) throws InterruptedException, TimeoutException,
161161

162162
private V getContent() throws ExecutionException {
163163

164+
if (isCancelled())
165+
throw new CancellationException();
166+
164167
ExecutionException e = exEx.get();
165168
if (e != null)
166169
throw e;
@@ -194,11 +197,14 @@ private V getContent() throws ExecutionException {
194197
/** org.asynchttpclient.ListenableFuture **/
195198
/*********************************************/
196199

197-
public final void done() {
198-
200+
private boolean terminateAndExit() {
199201
cancelTimeouts();
202+
return isDone.getAndSet(true) || isCancelled.get();
203+
}
200204

201-
if (isDone.getAndSet(true) || isCancelled.get())
205+
public final void done() {
206+
207+
if (terminateAndExit())
202208
return;
203209

204210
try {
@@ -219,9 +225,7 @@ public final void done() {
219225

220226
public final void abort(final Throwable t) {
221227

222-
cancelTimeouts();
223-
224-
if (isDone.get() || isCancelled.getAndSet(true))
228+
if (terminateAndExit())
225229
return;
226230

227231
exEx.compareAndSet(null, new ExecutionException(t));

providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/future/NettyResponseFuture.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public NettyResponseFuture(Uri uri,//
115115

116116
@Override
117117
public boolean isDone() {
118-
return isDone.get() || isCancelled.get();
118+
return isDone.get() || isCancelled();
119119
}
120120

121121
@Override
@@ -163,6 +163,9 @@ public V get(long l, TimeUnit tu) throws InterruptedException, TimeoutException,
163163

164164
private V getContent() throws ExecutionException {
165165

166+
if (isCancelled())
167+
throw new CancellationException();
168+
166169
ExecutionException e = exEx.get();
167170
if (e != null)
168171
throw e;
@@ -196,11 +199,14 @@ private V getContent() throws ExecutionException {
196199
/** org.asynchttpclient.ListenableFuture **/
197200
/*********************************************/
198201

199-
public final void done() {
200-
202+
private boolean terminateAndExit() {
201203
cancelTimeouts();
204+
return isDone.getAndSet(true) || isCancelled.get();
205+
}
202206

203-
if (isDone.getAndSet(true) || isCancelled.get())
207+
public final void done() {
208+
209+
if (terminateAndExit())
204210
return;
205211

206212
try {
@@ -221,9 +227,7 @@ public final void done() {
221227

222228
public final void abort(final Throwable t) {
223229

224-
cancelTimeouts();
225-
226-
if (isDone.get() || isCancelled.getAndSet(true))
230+
if (terminateAndExit())
227231
return;
228232

229233
exEx.compareAndSet(null, new ExecutionException(t));

0 commit comments

Comments
 (0)