Skip to content

Commit 8e9c82a

Browse files
authored
Merge pull request TooTallNate#748 from marci4/Issue713
Added tests for broadcast
2 parents 5233c9d + 9f169f7 commit 8e9c82a

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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.Assert;
36+
import org.junit.Test;
37+
38+
import java.net.InetSocketAddress;
39+
import java.net.URI;
40+
import java.net.URISyntaxException;
41+
import java.nio.ByteBuffer;
42+
import java.util.ArrayList;
43+
import java.util.concurrent.CountDownLatch;
44+
45+
import static org.junit.Assert.assertTrue;
46+
47+
public class Issue713Test {
48+
49+
CountDownLatch countDownLatchString = new CountDownLatch( 10 );
50+
CountDownLatch countDownLatchConnect = new CountDownLatch( 10 );
51+
CountDownLatch countDownLatchBytebuffer = new CountDownLatch( 10 );
52+
@Test(timeout=2000)
53+
public void testIssue() throws Exception {
54+
final int port = SocketUtil.getAvailablePort();
55+
WebSocketServer server = new WebSocketServer( new InetSocketAddress( port ) ) {
56+
@Override
57+
public void onOpen(WebSocket conn, ClientHandshake handshake ) {
58+
}
59+
60+
@Override
61+
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
62+
}
63+
64+
@Override
65+
public void onMessage( WebSocket conn, String message ) {
66+
67+
}
68+
69+
@Override
70+
public void onError( WebSocket conn, Exception ex ) {
71+
72+
}
73+
74+
@Override
75+
public void onStart() {
76+
try {
77+
for (int i = 0; i < 10; i++) {
78+
TestWebSocket tw = new TestWebSocket(port);
79+
tw.connect();
80+
}
81+
} catch (Exception e) {
82+
Assert.fail("Exception during connect!");
83+
}
84+
}
85+
};
86+
server.start();
87+
88+
countDownLatchConnect.await();
89+
server.broadcast("Hello world!");
90+
countDownLatchString.await();
91+
server.broadcast("Hello world".getBytes());
92+
countDownLatchBytebuffer.await();
93+
countDownLatchBytebuffer = new CountDownLatch( 10 );
94+
server.broadcast(ByteBuffer.wrap("Hello world".getBytes()));
95+
countDownLatchBytebuffer.await();
96+
97+
98+
countDownLatchString = new CountDownLatch( 5 );
99+
ArrayList<WebSocket> specialList = new ArrayList<WebSocket>(server.getConnections());
100+
specialList.remove(8);
101+
specialList.remove(6);
102+
specialList.remove(4);
103+
specialList.remove(2);
104+
specialList.remove(0);
105+
server.broadcast("Hello world", specialList);
106+
countDownLatchString.await();
107+
108+
countDownLatchBytebuffer = new CountDownLatch( 5 );
109+
server.broadcast("Hello world".getBytes());
110+
countDownLatchBytebuffer.await();
111+
}
112+
113+
114+
class TestWebSocket extends WebSocketClient {
115+
116+
TestWebSocket(int port) throws URISyntaxException {
117+
super(new URI( "ws://localhost:" + port));
118+
}
119+
120+
@Override
121+
public void onOpen( ServerHandshake handshakedata ) {
122+
countDownLatchConnect.countDown();
123+
}
124+
125+
@Override
126+
public void onMessage( String message ) {
127+
countDownLatchString.countDown();
128+
}
129+
@Override
130+
public void onMessage( ByteBuffer message ) {
131+
countDownLatchBytebuffer.countDown();
132+
}
133+
134+
@Override
135+
public void onClose( int code, String reason, boolean remote ) {
136+
137+
}
138+
139+
@Override
140+
public void onError( Exception ex ) {
141+
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)