|
| 1 | +package org.java_websocket.issues; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import static org.junit.Assert.fail; |
| 6 | + |
| 7 | +import java.net.InetSocketAddress; |
| 8 | +import java.net.URI; |
| 9 | +import java.util.Timer; |
| 10 | +import java.util.TimerTask; |
| 11 | +import java.util.concurrent.CountDownLatch; |
| 12 | +import org.java_websocket.WebSocket; |
| 13 | +import org.java_websocket.client.WebSocketClient; |
| 14 | +import org.java_websocket.handshake.ClientHandshake; |
| 15 | +import org.java_websocket.handshake.ServerHandshake; |
| 16 | +import org.java_websocket.server.WebSocketServer; |
| 17 | +import org.java_websocket.util.SocketUtil; |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +public class Issue1203Test { |
| 22 | + private final CountDownLatch countServerDownLatch = new CountDownLatch(1); |
| 23 | + private final CountDownLatch countClientDownLatch = new CountDownLatch(1); |
| 24 | + boolean isClosedCalled = false; |
| 25 | + @Test(timeout = 50000) |
| 26 | + public void testIssue() throws Exception { |
| 27 | + int port = SocketUtil.getAvailablePort(); |
| 28 | + WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) { |
| 29 | + @Override |
| 30 | + public void onOpen(WebSocket conn, ClientHandshake handshake) { |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void onClose(WebSocket conn, int code, String reason, boolean remote) { |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void onMessage(WebSocket conn, String message) { |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void onError(WebSocket conn, Exception ex) { |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void onStart() { |
| 47 | + countServerDownLatch.countDown(); |
| 48 | + } |
| 49 | + }; |
| 50 | + final WebSocketClient client = new WebSocketClient(new URI("ws://localhost:" + port)) { |
| 51 | + @Override |
| 52 | + public void onOpen(ServerHandshake handshakedata) { |
| 53 | + countClientDownLatch.countDown(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onMessage(String message) { |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onClose(int code, String reason, boolean remote) { |
| 63 | + isClosedCalled = true; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void onError(Exception ex) { |
| 68 | + |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + server.setConnectionLostTimeout(10); |
| 73 | + server.start(); |
| 74 | + countServerDownLatch.await(); |
| 75 | + |
| 76 | + client.setConnectionLostTimeout(10); |
| 77 | + Timer timer = new Timer(); |
| 78 | + TimerTask task = new TimerTask() { |
| 79 | + @Override |
| 80 | + public void run() { |
| 81 | + try { |
| 82 | + client.connectBlocking(); |
| 83 | + } catch (InterruptedException e) { |
| 84 | + e.printStackTrace(); |
| 85 | + } |
| 86 | + } |
| 87 | + }; |
| 88 | + timer.schedule(task, 15000); |
| 89 | + countClientDownLatch.await(); |
| 90 | + Thread.sleep(30000); |
| 91 | + Assert.assertFalse(isClosedCalled); |
| 92 | + client.closeBlocking(); |
| 93 | + server.stop(); |
| 94 | + } |
| 95 | +} |
0 commit comments