Skip to content

Commit 7890fff

Browse files
committed
Added test for TooTallNate#900
Overwrite channel to always throw IOException
1 parent 0f6cbdd commit 7890fff

2 files changed

Lines changed: 156 additions & 1 deletion

File tree

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ public synchronized void closeConnection( int code, String message, boolean remo
512512
try {
513513
channel.close();
514514
} catch ( IOException e ) {
515-
if( e.getMessage().equals( "Broken pipe" ) ) {
515+
if( e.getMessage() != null && e.getMessage().equals( "Broken pipe" ) ) {
516516
log.trace( "Caught IOException: Broken pipe during closeConnection()", e );
517517
} else {
518518
log.error("Exception during channel.close()", e);
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright (c) 2010-2020 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+
27+
package org.java_websocket.issues;
28+
29+
import org.java_websocket.WebSocket;
30+
import org.java_websocket.WebSocketImpl;
31+
import org.java_websocket.WrappedByteChannel;
32+
import org.java_websocket.client.WebSocketClient;
33+
import org.java_websocket.handshake.ClientHandshake;
34+
import org.java_websocket.handshake.ServerHandshake;
35+
import org.java_websocket.server.WebSocketServer;
36+
import org.java_websocket.util.SocketUtil;
37+
import org.junit.Test;
38+
39+
import java.io.IOException;
40+
import java.io.InputStream;
41+
import java.lang.reflect.Method;
42+
import java.net.InetSocketAddress;
43+
import java.net.URI;
44+
import java.nio.ByteBuffer;
45+
import java.util.ArrayList;
46+
import java.util.concurrent.CountDownLatch;
47+
48+
public class Issue900Test {
49+
50+
CountDownLatch countServerDownLatch = new CountDownLatch(1);
51+
CountDownLatch countCloseCallsDownLatch = new CountDownLatch(1);
52+
53+
@Test(timeout = 2000)
54+
public void testIssue() throws Exception {
55+
int port = SocketUtil.getAvailablePort();
56+
final WebSocketClient client = new WebSocketClient(new URI("ws://localhost:" + port)) {
57+
@Override
58+
public void onOpen(ServerHandshake handshakedata) {
59+
60+
}
61+
62+
@Override
63+
public void onMessage(String message) {
64+
}
65+
66+
@Override
67+
public void onClose(int code, String reason, boolean remote) {
68+
}
69+
70+
@Override
71+
public void onError(Exception ex) {
72+
73+
}
74+
};
75+
WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) {
76+
@Override
77+
public void onOpen(WebSocket conn, ClientHandshake handshake) {
78+
}
79+
80+
@Override
81+
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
82+
countCloseCallsDownLatch.countDown();
83+
}
84+
85+
@Override
86+
public void onMessage(WebSocket conn, String message) {
87+
88+
}
89+
90+
@Override
91+
public void onError(WebSocket conn, Exception ex) {
92+
93+
}
94+
95+
@Override
96+
public void onStart() {
97+
countServerDownLatch.countDown();
98+
}
99+
};
100+
new Thread(server).start();
101+
countServerDownLatch.await();
102+
client.connectBlocking();
103+
WebSocketImpl websocketImpl = (WebSocketImpl)new ArrayList<WebSocket>(server.getConnections()).get(0);
104+
websocketImpl.setChannel(new ExceptionThrowingByteChannel());
105+
server.broadcast("test");
106+
countCloseCallsDownLatch.await();
107+
}
108+
class ExceptionThrowingByteChannel implements WrappedByteChannel {
109+
110+
@Override
111+
public boolean isNeedWrite() {
112+
return false;
113+
}
114+
115+
@Override
116+
public void writeMore() throws IOException {
117+
throw new IOException();
118+
}
119+
120+
@Override
121+
public boolean isNeedRead() {
122+
return false;
123+
}
124+
125+
@Override
126+
public int readMore(ByteBuffer dst) throws IOException {
127+
throw new IOException();
128+
}
129+
130+
@Override
131+
public boolean isBlocking() {
132+
return false;
133+
}
134+
135+
@Override
136+
public int read(ByteBuffer dst) throws IOException {
137+
throw new IOException();
138+
}
139+
140+
@Override
141+
public int write(ByteBuffer src) throws IOException {
142+
throw new IOException();
143+
}
144+
145+
@Override
146+
public boolean isOpen() {
147+
return false;
148+
}
149+
150+
@Override
151+
public void close() throws IOException {
152+
throw new IOException();
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)