Skip to content

Commit 66f03e0

Browse files
committed
Added tests for the exception
1 parent da467db commit 66f03e0

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

src/test/java/org/java_websocket/issues/AllIssueTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
org.java_websocket.issues.Issue256Test.class,
3737
org.java_websocket.issues.Issue661Test.class,
3838
org.java_websocket.issues.Issue666Test.class,
39-
org.java_websocket.issues.Issue677Test.class
39+
org.java_websocket.issues.Issue677Test.class,
40+
org.java_websocket.issues.Issue732Test.class
4041
})
4142
/**
4243
* Start all tests for issues
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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.WebSocketImpl;
30+
import org.java_websocket.client.WebSocketClient;
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.java_websocket.util.ThreadCheck;
36+
import org.junit.Assert;
37+
import org.junit.Rule;
38+
import org.junit.Test;
39+
40+
import java.net.InetSocketAddress;
41+
import java.net.URI;
42+
import java.util.concurrent.CountDownLatch;
43+
44+
import static org.junit.Assert.fail;
45+
46+
public class Issue732Test {
47+
48+
@Rule
49+
public ThreadCheck zombies = new ThreadCheck();
50+
51+
private CountDownLatch countServerDownLatch = new CountDownLatch(1);
52+
53+
@Test(timeout = 2000)
54+
public void testIssue() throws Exception {
55+
WebSocketImpl.DEBUG = true;
56+
int port = SocketUtil.getAvailablePort();
57+
final WebSocketClient webSocket = new WebSocketClient(new URI("ws://localhost:" + port)) {
58+
@Override
59+
public void onOpen(ServerHandshake handshakedata) {
60+
try {
61+
this.reconnect();
62+
Assert.fail("Exception should be thrown");
63+
} catch (IllegalStateException e) {
64+
//
65+
}
66+
}
67+
68+
@Override
69+
public void onMessage(String message) {
70+
try {
71+
this.reconnect();
72+
Assert.fail("Exception should be thrown");
73+
} catch (IllegalStateException e) {
74+
send("hi");
75+
}
76+
}
77+
78+
@Override
79+
public void onClose(int code, String reason, boolean remote) {
80+
try {
81+
this.reconnect();
82+
Assert.fail("Exception should be thrown");
83+
} catch (IllegalStateException e) {
84+
//
85+
}
86+
}
87+
88+
@Override
89+
public void onError(Exception ex) {
90+
try {
91+
this.reconnect();
92+
Assert.fail("Exception should be thrown");
93+
} catch (IllegalStateException e) {
94+
//
95+
}
96+
}
97+
};
98+
WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) {
99+
@Override
100+
public void onOpen(WebSocket conn, ClientHandshake handshake) {
101+
conn.send("hi");
102+
}
103+
104+
@Override
105+
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
106+
countServerDownLatch.countDown();
107+
}
108+
109+
@Override
110+
public void onMessage(WebSocket conn, String message) {
111+
conn.close();
112+
}
113+
114+
@Override
115+
public void onError(WebSocket conn, Exception ex) {
116+
fail("There should be no onError!");
117+
}
118+
119+
@Override
120+
public void onStart() {
121+
webSocket.connect();
122+
}
123+
};
124+
server.start();
125+
countServerDownLatch.await();
126+
server.stop();
127+
}
128+
}

0 commit comments

Comments
 (0)