|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2017 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.handshake.ClientHandshake; |
| 31 | +import org.java_websocket.handshake.ServerHandshake; |
| 32 | +import org.java_websocket.server.WebSocketServer; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import java.net.InetSocketAddress; |
| 36 | +import java.net.URI; |
| 37 | +import java.util.concurrent.CountDownLatch; |
| 38 | + |
| 39 | +import static org.junit.Assert.assertTrue; |
| 40 | + |
| 41 | +public class Issue609 { |
| 42 | + |
| 43 | + CountDownLatch countDownLatch = new CountDownLatch( 1 ); |
| 44 | + |
| 45 | + boolean wasOpenClient; |
| 46 | + boolean wasOpenServer; |
| 47 | + @Test |
| 48 | + public void testIssue() throws Exception { |
| 49 | + WebSocketServer server = new WebSocketServer( new InetSocketAddress( 8887 ) ) { |
| 50 | + @Override |
| 51 | + public void onOpen( WebSocket conn, ClientHandshake handshake ) { |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onClose( WebSocket conn, int code, String reason, boolean remote ) { |
| 56 | + wasOpenServer = conn.isOpen(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onMessage( WebSocket conn, String message ) { |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void onError( WebSocket conn, Exception ex ) { |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onStart() { |
| 71 | + |
| 72 | + } |
| 73 | + }; |
| 74 | + server.start(); |
| 75 | + WebSocketClient webSocket = new WebSocketClient( new URI( "ws://localhost:8887" ) ) { |
| 76 | + @Override |
| 77 | + public void onOpen( ServerHandshake handshakedata ) { |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void onMessage( String message ) { |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void onClose( int code, String reason, boolean remote ) { |
| 88 | + wasOpenClient= isOpen(); |
| 89 | + countDownLatch.countDown(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void onError( Exception ex ) { |
| 94 | + |
| 95 | + } |
| 96 | + }; |
| 97 | + webSocket.connectBlocking(); |
| 98 | + assertTrue( webSocket.isOpen() ); |
| 99 | + webSocket.getSocket().close(); |
| 100 | + countDownLatch.await(); |
| 101 | + assertTrue( !webSocket.isOpen() ); |
| 102 | + assertTrue( !wasOpenClient ); |
| 103 | + assertTrue( !wasOpenServer ); |
| 104 | + server.stop(); |
| 105 | + } |
| 106 | +} |
0 commit comments