Skip to content

Commit b9cc668

Browse files
committed
Reworked test
1 parent b7eb2f1 commit b9cc668

File tree

2 files changed

+103
-72
lines changed

2 files changed

+103
-72
lines changed

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,21 @@ public boolean reconnectBlocking() throws InterruptedException {
184184
* @since 1.3.8
185185
*/
186186
private void reset() {
187-
close();
188-
this.draft.reset();
189-
if (this.socket != null) {
190-
try {
191-
this.socket.close();
192-
} catch ( IOException e ) {
193-
e.printStackTrace();
187+
try {
188+
closeBlocking();
189+
if( writeThread != null ) {
190+
this.writeThread.interrupt();
191+
this.writeThread = null;
194192
}
195-
try {
196-
this.socket = new Socket( this.socket.getInetAddress(), this.socket.getPort() );
197-
} catch ( IOException e ) {
198-
e.printStackTrace();
193+
this.draft.reset();
194+
if( this.socket != null ) {
195+
this.socket.close();
196+
this.socket = null;
199197
}
198+
} catch ( Exception e ) {
199+
onError( e );
200+
engine.closeConnection( CloseFrame.ABNORMAL_CLOSE, e.getMessage() );
201+
return;
200202
}
201203
connectLatch = new CountDownLatch( 1 );
202204
closeLatch = new CountDownLatch( 1 );

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

Lines changed: 90 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,26 @@
3333
import org.java_websocket.server.WebSocketServer;
3434
import org.java_websocket.util.SocketUtil;
3535
import org.java_websocket.util.ThreadCheck;
36-
import org.junit.Rule;
37-
import org.junit.Test;
36+
import org.junit.*;
3837

38+
import java.io.IOException;
3939
import java.net.InetSocketAddress;
4040
import java.net.URI;
4141
import java.util.concurrent.CountDownLatch;
4242

4343
public class Issue256Test {
4444

45-
/**
46-
* This causes problems on my jenkins. Need to fix this so just deactivate it for the pull request
45+
private static WebSocketServer ws;
46+
47+
private static int port;
48+
static CountDownLatch countServerDownLatch = new CountDownLatch( 1 );
4749
@Rule
4850
public ThreadCheck zombies = new ThreadCheck();
4951

50-
private void runTestScenarioReconnect(boolean reconnectBlocking) throws Exception {
51-
final CountDownLatch countServerDownLatch = new CountDownLatch( 1 );
52-
int port = SocketUtil.getAvailablePort();
53-
WebSocketServer ws = new WebSocketServer( new InetSocketAddress( port ) ) {
52+
@BeforeClass
53+
public static void startServer() throws Exception {
54+
port = SocketUtil.getAvailablePort();
55+
ws = new WebSocketServer( new InetSocketAddress( port ) , 16) {
5456
@Override
5557
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
5658

@@ -63,25 +65,32 @@ public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
6365

6466
@Override
6567
public void onMessage( WebSocket conn, String message ) {
66-
68+
conn.send( message );
6769
}
6870

6971
@Override
7072
public void onError( WebSocket conn, Exception ex ) {
7173

74+
ex.printStackTrace( );
75+
Assert.fail( "There should be no exception!" );
7276
}
7377

7478
@Override
7579
public void onStart() {
7680
countServerDownLatch.countDown();
7781
}
7882
};
83+
ws.setConnectionLostTimeout( 0 );
7984
ws.start();
80-
countServerDownLatch.await();
85+
}
86+
87+
private void runTestScenarioReconnect( boolean closeBlocking ) throws Exception {
88+
final CountDownLatch countDownLatch0 = new CountDownLatch( 1 );
89+
final CountDownLatch countDownLatch1 = new CountDownLatch( 2 );
8190
WebSocketClient clt = new WebSocketClient( new URI( "ws://localhost:" + port ) ) {
8291
@Override
8392
public void onOpen( ServerHandshake handshakedata ) {
84-
93+
countDownLatch1.countDown();
8594
}
8695

8796
@Override
@@ -91,110 +100,130 @@ public void onMessage( String message ) {
91100

92101
@Override
93102
public void onClose( int code, String reason, boolean remote ) {
94-
103+
countDownLatch0.countDown();
95104
}
96105

97106
@Override
98107
public void onError( Exception ex ) {
99-
108+
ex.printStackTrace( );
109+
Assert.fail("There should be no exception!");
100110
}
101111
};
102112
clt.connectBlocking();
103-
clt.send("test");
104-
clt.getSocket().close();
105-
if (reconnectBlocking) {
106-
clt.reconnectBlocking();
113+
if( closeBlocking ) {
114+
clt.closeBlocking();
107115
} else {
108-
clt.reconnect();
116+
clt.getSocket().close();
109117
}
110-
Thread.sleep( 100 );
118+
countDownLatch0.await();
119+
clt.reconnectBlocking();
120+
clt.closeBlocking();
121+
}
111122

123+
@AfterClass
124+
public static void successTests() throws InterruptedException, IOException {
112125
ws.stop();
113-
Thread.sleep( 100 );
114126
}
115127

116-
@Test
128+
@Test(timeout = 5000)
117129
public void runReconnectBlockingScenario0() throws Exception {
118-
runTestScenarioReconnect(true);
130+
runTestScenarioReconnect( true );
119131
}
120-
@Test
132+
133+
@Test(timeout = 5000)
121134
public void runReconnectBlockingScenario1() throws Exception {
122-
runTestScenarioReconnect(true);
135+
runTestScenarioReconnect( true );
123136
}
124-
@Test
137+
138+
@Test(timeout = 5000)
125139
public void runReconnectBlockingScenario2() throws Exception {
126-
runTestScenarioReconnect(true);
140+
runTestScenarioReconnect( true );
127141
}
128-
@Test
142+
143+
@Test(timeout = 5000)
129144
public void runReconnectBlockingScenario3() throws Exception {
130-
runTestScenarioReconnect(true);
145+
runTestScenarioReconnect( true );
131146
}
132-
@Test
147+
148+
@Test(timeout = 5000)
133149
public void runReconnectBlockingScenario4() throws Exception {
134-
runTestScenarioReconnect(true);
150+
runTestScenarioReconnect( true );
135151
}
136-
@Test
152+
153+
@Test(timeout = 5000)
137154
public void runReconnectBlockingScenario5() throws Exception {
138-
runTestScenarioReconnect(true);
155+
runTestScenarioReconnect( true );
139156
}
140-
@Test
157+
158+
@Test(timeout = 5000)
141159
public void runReconnectBlockingScenario6() throws Exception {
142-
runTestScenarioReconnect(true);
160+
runTestScenarioReconnect( true );
143161
}
144-
@Test
162+
163+
@Test(timeout = 5000)
145164
public void runReconnectBlockingScenario7() throws Exception {
146-
runTestScenarioReconnect(true);
165+
runTestScenarioReconnect( true );
147166
}
148-
@Test
167+
168+
@Test(timeout = 5000)
149169
public void runReconnectBlockingScenario8() throws Exception {
150-
runTestScenarioReconnect(true);
170+
runTestScenarioReconnect( true );
151171
}
152-
@Test
172+
173+
@Test(timeout = 5000)
153174
public void runReconnectBlockingScenario9() throws Exception {
154-
runTestScenarioReconnect(true);
175+
runTestScenarioReconnect( true );
155176
}
156177

157-
@Test
178+
@Test(timeout = 5000)
158179
public void runReconnectScenario0() throws Exception {
159-
runTestScenarioReconnect(false);
180+
runTestScenarioReconnect( false );
160181
}
161-
@Test
182+
183+
@Test(timeout = 5000)
162184
public void runReconnectScenario1() throws Exception {
163-
runTestScenarioReconnect(false);
185+
runTestScenarioReconnect( false );
164186
}
165-
@Test
187+
188+
@Test(timeout = 5000)
166189
public void runReconnectScenario2() throws Exception {
167-
runTestScenarioReconnect(false);
190+
runTestScenarioReconnect( false );
168191
}
169-
@Test
192+
193+
@Test(timeout = 5000)
170194
public void runReconnectScenario3() throws Exception {
171-
runTestScenarioReconnect(false);
195+
runTestScenarioReconnect( false );
172196
}
173-
@Test
197+
198+
@Test(timeout = 5000)
174199
public void runReconnectScenario4() throws Exception {
175-
runTestScenarioReconnect(false);
200+
runTestScenarioReconnect( false );
176201
}
177-
@Test
202+
203+
@Test(timeout = 5000)
178204
public void runReconnectScenario5() throws Exception {
179-
runTestScenarioReconnect(false);
205+
runTestScenarioReconnect( false );
180206
}
181-
@Test
207+
208+
@Test(timeout = 5000)
182209
public void runReconnectScenario6() throws Exception {
183-
runTestScenarioReconnect(false);
210+
runTestScenarioReconnect( false );
184211
}
185-
@Test
212+
213+
@Test(timeout = 5000)
186214
public void runReconnectScenario7() throws Exception {
187-
runTestScenarioReconnect(false);
215+
runTestScenarioReconnect( false );
188216
}
189-
@Test
217+
218+
@Test(timeout = 5000)
190219
public void runReconnectScenario8() throws Exception {
191-
runTestScenarioReconnect(false);
220+
runTestScenarioReconnect( false );
192221
}
193-
@Test
222+
223+
@Test(timeout = 5000)
194224
public void runReconnectScenario9() throws Exception {
195-
runTestScenarioReconnect(false);
225+
runTestScenarioReconnect( false );
196226
}
197227

198-
*/
199228
}
200229

0 commit comments

Comments
 (0)