|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2018 Nathan Rajlich |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person |
| 5 | + * obtaining a copy of this software and associated documentation |
| 6 | + * files (the "Software"), to deal in the Software without |
| 7 | + * restriction, including without limitation the rights to use, |
| 8 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the |
| 10 | + * Software is furnished to do so, subject to the following |
| 11 | + * conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be |
| 14 | + * included in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +package org.java_websocket.issues; |
| 27 | + |
| 28 | +import org.java_websocket.WebSocket; |
| 29 | +import org.java_websocket.client.WebSocketClient; |
| 30 | +import org.java_websocket.framing.CloseFrame; |
| 31 | +import org.java_websocket.handshake.ClientHandshake; |
| 32 | +import org.java_websocket.handshake.ServerHandshake; |
| 33 | +import org.java_websocket.server.WebSocketServer; |
| 34 | +import org.java_websocket.util.SocketUtil; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +import java.net.InetSocketAddress; |
| 38 | +import java.net.URI; |
| 39 | +import java.util.concurrent.CountDownLatch; |
| 40 | + |
| 41 | +import static org.junit.Assert.assertTrue; |
| 42 | + |
| 43 | +public class Issue677Test { |
| 44 | + |
| 45 | + CountDownLatch countDownLatch0 = new CountDownLatch( 1 ); |
| 46 | + CountDownLatch countServerDownLatch = new CountDownLatch( 1 ); |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testIssue() throws Exception { |
| 50 | + int port = SocketUtil.getAvailablePort(); |
| 51 | + WebSocketClient webSocket0 = new WebSocketClient( new URI( "ws://localhost:" + port ) ) { |
| 52 | + @Override |
| 53 | + public void onOpen( ServerHandshake handshakedata ) { |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onMessage( String message ) { |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onClose( int code, String reason, boolean remote ) { |
| 64 | + countDownLatch0.countDown(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onError( Exception ex ) { |
| 69 | + |
| 70 | + } |
| 71 | + }; |
| 72 | + WebSocketClient webSocket1 = new WebSocketClient( new URI( "ws://localhost:" + port ) ) { |
| 73 | + @Override |
| 74 | + public void onOpen( ServerHandshake handshakedata ) { |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void onMessage( String message ) { |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void onClose( int code, String reason, boolean remote ) { |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onError( Exception ex ) { |
| 87 | + |
| 88 | + } |
| 89 | + }; |
| 90 | + WebSocketServer server = new WebSocketServer( new InetSocketAddress( port ) ) { |
| 91 | + @Override |
| 92 | + public void onOpen( WebSocket conn, ClientHandshake handshake ) { |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onClose( WebSocket conn, int code, String reason, boolean remote ) { |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void onMessage( WebSocket conn, String message ) { |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void onError( WebSocket conn, Exception ex ) { |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void onStart() { |
| 111 | + countServerDownLatch.countDown(); |
| 112 | + } |
| 113 | + }; |
| 114 | + server.start(); |
| 115 | + countServerDownLatch.await(); |
| 116 | + webSocket0.connectBlocking(); |
| 117 | + assertTrue( "webSocket.isOpen()", webSocket0.isOpen() ); |
| 118 | + webSocket0.close(); |
| 119 | + assertTrue( "webSocket.isClosing()", webSocket0.isClosing() ); |
| 120 | + countDownLatch0.await(); |
| 121 | + assertTrue( "webSocket.isClosed()", webSocket0.isClosed() ); |
| 122 | + webSocket1.connectBlocking(); |
| 123 | + assertTrue( "webSocket.isOpen()", webSocket1.isOpen() ); |
| 124 | + webSocket1.closeConnection(CloseFrame.ABNORMAL_CLOSE, "Abnormal close!"); |
| 125 | + assertTrue( "webSocket.isClosed()", webSocket1.isClosed() ); |
| 126 | + server.stop(); |
| 127 | + } |
| 128 | +} |
0 commit comments