Skip to content

Commit 2cd731b

Browse files
committed
fixed the issue with the way handshake failure was handled in web socket futures
Fixed the issue where in creating web socket clients, if the handshake failed, the failure wouldn't be reported to the callback (it would be called with a null exception parameter).
1 parent d69769a commit 2cd731b

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,8 @@ public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
688688
}
689689
WebSocket ws = WebSocketImpl.finishHandshake(req.getHeaders(), response);
690690
if (ws == null) {
691-
if (!ret.setComplete(new WebSocketHandshakeException("Unable to complete websocket handshake")))
691+
ex = new WebSocketHandshakeException("Unable to complete websocket handshake");
692+
if (!ret.setComplete(ex))
692693
return;
693694
}
694695
else {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.koushikdutta.async.test;
2+
3+
import com.koushikdutta.async.future.Future;
4+
import com.koushikdutta.async.http.AsyncHttpClient;
5+
import com.koushikdutta.async.http.WebSocket;
6+
import com.koushikdutta.async.http.server.AsyncHttpServer;
7+
import com.koushikdutta.async.http.server.AsyncHttpServerRequest;
8+
9+
import junit.framework.TestCase;
10+
11+
import java.util.concurrent.CountDownLatch;
12+
13+
14+
public class IssueWithWebSocketFuturesTests extends TestCase {
15+
16+
//testing that websocket callback gets called with the correct parameters.
17+
public void testWebSocketFutureWithHandshakeFailureCallback() throws Exception {
18+
19+
//creating a faulty server!
20+
AsyncHttpServer httpServer = new AsyncHttpServer();
21+
httpServer.websocket(".*", new AsyncHttpServer.WebSocketRequestCallback() {
22+
@Override
23+
public void onConnected(WebSocket webSocket, AsyncHttpServerRequest request) {
24+
25+
}
26+
});
27+
httpServer.listen(6666);
28+
29+
30+
31+
final Exception[] callbackException = {null};
32+
final WebSocket[] callbackWs = {null};
33+
final CountDownLatch countDownLatch = new CountDownLatch(1);
34+
35+
36+
//for some reason, it fails with a WebSocketHandshakeException.
37+
//But in general, if the handshake fails, the callback must be called with an exception.
38+
Future<WebSocket> wsFuture = AsyncHttpClient.getDefaultInstance().websocket("ws://127.0.0.1:6666", "ws", new AsyncHttpClient.WebSocketConnectCallback() {
39+
@Override
40+
public void onCompleted(Exception ex, WebSocket webSocket) {
41+
callbackException[0] = ex;
42+
callbackWs[0] = webSocket;
43+
countDownLatch.countDown();
44+
}
45+
});
46+
47+
48+
//wait for the future to complete
49+
countDownLatch.await();
50+
51+
//exactly one mut be null
52+
assertTrue(callbackWs[0] == null ^ callbackException[0] == null);
53+
54+
//callback parameters must be the same as the future's result
55+
assertEquals(wsFuture.tryGet(), callbackWs[0]);
56+
assertEquals(wsFuture.tryGetException(), callbackException[0]);
57+
58+
}
59+
}

0 commit comments

Comments
 (0)