-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for connectBlocking cleanup (#1399)
- Loading branch information
1 parent
aa84b39
commit d80369a
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/test/java/org/java_websocket/client/ConnectBlockingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.java_websocket.client; | ||
|
||
import java.io.IOException; | ||
import java.net.*; | ||
import java.util.Set; | ||
import java.util.concurrent.*; | ||
import org.java_websocket.WebSocket; | ||
import org.java_websocket.handshake.*; | ||
import org.java_websocket.client.*; | ||
import org.java_websocket.server.WebSocketServer; | ||
import org.java_websocket.util.SocketUtil; | ||
import org.java_websocket.enums.ReadyState; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
public class ConnectBlockingTest { | ||
|
||
@Test(timeout = 1000) | ||
public void test_ConnectBlockingCleanup() throws Throwable { | ||
|
||
Set<Thread> threadSet1 = Thread.getAllStackTraces().keySet(); | ||
final CountDownLatch ready = new CountDownLatch(1); | ||
final CountDownLatch accepted = new CountDownLatch(1); | ||
|
||
final int port = SocketUtil.getAvailablePort(); | ||
|
||
/* TCP server which listens to a port, but does not answer handshake */ | ||
Thread server = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
ServerSocket serverSocket = new ServerSocket(port); | ||
ready.countDown(); | ||
Socket clientSocket = serverSocket.accept(); | ||
accepted.countDown(); | ||
} catch (Throwable t) { | ||
assertTrue(t instanceof InterruptedException); | ||
} | ||
} | ||
}); | ||
server.start(); | ||
ready.await(); | ||
|
||
WebSocketClient client = new WebSocketClient(URI.create("ws://localhost:" + port)) { | ||
@Override | ||
public void onOpen(ServerHandshake handshake) { | ||
} | ||
@Override | ||
public void onClose(int code, String reason, boolean remote) {} | ||
@Override | ||
public void onMessage(String message) {} | ||
@Override | ||
public void onError(Exception ex) {} | ||
}; | ||
boolean connected = client.connectBlocking(100, TimeUnit.MILLISECONDS); | ||
assertEquals("TCP socket should have been accepted", 0, accepted.getCount()); | ||
assertFalse("WebSocket should not be connected (as server didn't send handshake)", connected); | ||
|
||
server.interrupt(); | ||
server.join(); | ||
|
||
Set<Thread> threadSet2 = Thread.getAllStackTraces().keySet(); | ||
assertEquals("no threads left over", threadSet1, threadSet2); | ||
assertEquals("WebSocket is in closed state", ReadyState.NOT_YET_CONNECTED, client.getReadyState()); | ||
} | ||
} |