Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[#1037] Fixed WSAsync-bug when AHC throws exception on timeout etc.. …

…Have added invokeWithException to Promise to be able to "forward" exception
  • Loading branch information...
commit e0400da60371c365063fccf941663d1e7c237938 1 parent 8f51ed7
@mbknor mbknor authored
View
18 framework/src/play/libs/F.java
@@ -44,22 +44,40 @@ public V getOrNull() {
public V get() throws InterruptedException, ExecutionException {
taskLock.await();
+ if (exception != null) {
+ // The result of the promise is an exception - throw it
+ throw new ExecutionException(exception);
+ }
return result;
}
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
taskLock.await(timeout, unit);
+ if (exception != null) {
+ // The result of the promise is an exception - throw it
+ throw new ExecutionException(exception);
+ }
return result;
}
List<F.Action<Promise<V>>> callbacks = new ArrayList<F.Action<Promise<V>>>();
boolean invoked = false;
V result = null;
+ Throwable exception = null;
public void invoke(V result) {
+ invokeWithResultOrException(result, null);
+ }
+
+ public void invokeWithException(Throwable t) {
+ invokeWithResultOrException(null, t);
+ }
+
+ protected void invokeWithResultOrException(V result, Throwable t) {
synchronized (this) {
if (!invoked) {
invoked = true;
this.result = result;
+ this.exception = t;
taskLock.countDown();
} else {
return;
View
3  framework/src/play/libs/ws/WSAsync.java
@@ -379,7 +379,8 @@ public HttpResponse onCompleted(Response response) throws Exception {
}
@Override
public void onThrowable(Throwable t) {
- throw new RuntimeException(t);
+ // An error happened - must "forward" the exception to the one waiting for the result
+ smartFuture.invokeWithException(t);
}
});
View
52 samples-and-tests/just-test-cases/app/controllers/SlowResponseTestController.java
@@ -0,0 +1,52 @@
+package controllers;
+
+import org.junit.Assert;
+import play.Logger;
+import play.libs.WS;
+import play.mvc.Controller;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: mortenkjetland
+ * Date: 9/6/11
+ * Time: 10:27 AM
+ * To change this template use File | Settings | File Templates.
+ */
+public class SlowResponseTestController extends Controller {
+
+
+ public static void slowResponse() throws Exception {
+ int seconds = 2;
+ Logger.info("Sleeping " + seconds + " before sending response");
+ Thread.sleep(seconds*1000);
+ renderText("Response after sleep");
+ }
+
+ public static void testWSAsyncWithException() {
+ String url = "http://localhost:9003/SlowResponseTestController/slowResponse";
+ // first make sure SlowResponse works
+ String res = await(WS.url(url).getAsync()).getString();
+ Assert.assertEquals("Response after sleep", res);
+
+ // assert that we get exception if we have too short timeout
+ boolean gotException = false;
+ try {
+ await(WS.url(url).timeout("1s").getAsync());
+ } catch (Exception e) {
+ gotException = true;
+ }
+ Assert.assertTrue("Did not get exception!", gotException);
+
+ // assert that we get exception if we connect to invalid port
+ gotException = false;
+ try {
+ await(WS.url("http://localhost:55651").getAsync());
+ } catch (Exception e) {
+ gotException = true;
+ }
+ Assert.assertTrue("Did not get exception!", gotException);
+
+ renderText("ok");
+ }
+
+}
View
7 samples-and-tests/just-test-cases/test/RestTest.java
@@ -144,4 +144,11 @@ public void testEncodingEcho() {
assertEquals("abc|id|abc|body|", WS.url("http://localhost:9003/encoding/echo/abc").get().getString());
}
+ @Test
+ public void testWSAsyncWithException() {
+ String url = "http://localhost:9003/SlowResponseTestController/testWSAsyncWithException";
+ String res = WS.url(url).get().getString();
+ assertEquals("ok", res);
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.