|
| 1 | +package org.java_websocket.issues; |
| 2 | + |
| 3 | +import org.java_websocket.WebSocket; |
| 4 | +import org.java_websocket.client.WebSocketClient; |
| 5 | +import org.java_websocket.handshake.ClientHandshake; |
| 6 | +import org.java_websocket.handshake.ServerHandshake; |
| 7 | +import org.java_websocket.server.WebSocketServer; |
| 8 | +import org.java_websocket.util.SocketUtil; |
| 9 | +import org.junit.Assert; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.net.InetSocketAddress; |
| 13 | +import java.net.URI; |
| 14 | +import java.nio.ByteBuffer; |
| 15 | +import java.util.concurrent.CountDownLatch; |
| 16 | +import java.util.concurrent.atomic.AtomicInteger; |
| 17 | + |
| 18 | +public class Issue1160Test { |
| 19 | + private final CountDownLatch countServerStart = new CountDownLatch(1); |
| 20 | + |
| 21 | + static class TestClient extends WebSocketClient { |
| 22 | + private final CountDownLatch onCloseLatch; |
| 23 | + |
| 24 | + public TestClient(URI uri, CountDownLatch latch) { |
| 25 | + super(uri); |
| 26 | + onCloseLatch = latch; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void onOpen(ServerHandshake handshakedata) { |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void onMessage(String message) { |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void onClose(int code, String reason, boolean remote) { |
| 39 | + onCloseLatch.countDown(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onError(Exception ex) { |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + @Test(timeout = 5000) |
| 49 | + public void nonFatalErrorShallBeHandledByServer() throws Exception { |
| 50 | + final AtomicInteger isServerOnErrorCalledCounter = new AtomicInteger(0); |
| 51 | + |
| 52 | + int port = SocketUtil.getAvailablePort(); |
| 53 | + WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) { |
| 54 | + @Override |
| 55 | + public void onOpen(WebSocket conn, ClientHandshake handshake) { |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void onClose(WebSocket conn, int code, String reason, boolean remote) { |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onMessage(WebSocket conn, ByteBuffer message) { |
| 64 | + throw new Error("Some error"); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onMessage(WebSocket conn, String message) { |
| 69 | + throw new Error("Some error"); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onError(WebSocket conn, Exception ex) { |
| 74 | + isServerOnErrorCalledCounter.incrementAndGet(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void onStart() { |
| 79 | + countServerStart.countDown(); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + |
| 84 | + server.setConnectionLostTimeout(10); |
| 85 | + server.start(); |
| 86 | + countServerStart.await(); |
| 87 | + |
| 88 | + URI uri = new URI("ws://localhost:" + port); |
| 89 | + |
| 90 | + int CONNECTION_COUNT = 3; |
| 91 | + for (int i = 0; i < CONNECTION_COUNT; i++) { |
| 92 | + CountDownLatch countClientDownLatch = new CountDownLatch(1); |
| 93 | + WebSocketClient client = new TestClient(uri, countClientDownLatch); |
| 94 | + client.setConnectionLostTimeout(10); |
| 95 | + |
| 96 | + client.connectBlocking(); |
| 97 | + client.send(new byte[100]); |
| 98 | + countClientDownLatch.await(); |
| 99 | + client.closeBlocking(); |
| 100 | + } |
| 101 | + |
| 102 | + Assert.assertEquals(CONNECTION_COUNT, isServerOnErrorCalledCounter.get()); |
| 103 | + |
| 104 | + server.stop(); |
| 105 | + } |
| 106 | + |
| 107 | + @Test(timeout = 5000) |
| 108 | + public void fatalErrorShallNotBeHandledByServer() throws Exception { |
| 109 | + int port = SocketUtil.getAvailablePort(); |
| 110 | + |
| 111 | + final CountDownLatch countServerDownLatch = new CountDownLatch(1); |
| 112 | + WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) { |
| 113 | + @Override |
| 114 | + public void onOpen(WebSocket conn, ClientHandshake handshake) { |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void onClose(WebSocket conn, int code, String reason, boolean remote) { |
| 119 | + countServerDownLatch.countDown(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onMessage(WebSocket conn, ByteBuffer message) { |
| 124 | + throw new OutOfMemoryError("Some error"); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void onMessage(WebSocket conn, String message) { |
| 129 | + throw new OutOfMemoryError("Some error"); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void onError(WebSocket conn, Exception ex) { |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void onStart() { |
| 138 | + countServerStart.countDown(); |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + |
| 143 | + server.setConnectionLostTimeout(10); |
| 144 | + server.start(); |
| 145 | + countServerStart.await(); |
| 146 | + |
| 147 | + URI uri = new URI("ws://localhost:" + port); |
| 148 | + |
| 149 | + CountDownLatch countClientDownLatch = new CountDownLatch(1); |
| 150 | + WebSocketClient client = new TestClient(uri, countClientDownLatch); |
| 151 | + client.setConnectionLostTimeout(10); |
| 152 | + |
| 153 | + client.connectBlocking(); |
| 154 | + client.send(new byte[100]); |
| 155 | + countClientDownLatch.await(); |
| 156 | + countServerDownLatch.await(); |
| 157 | + Assert.assertTrue(countClientDownLatch.getCount() == 0 && countServerDownLatch.getCount() == 0); |
| 158 | + } |
| 159 | +} |
0 commit comments