Repository navigation
Expand file tree
/
Copy pathIssue1460Test.java
More file actions
148 lines (127 loc) · 4.84 KB
/
Copy pathIssue1460Test.java
File metadata and controls
148 lines (127 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package org.java_websocket.issues;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.extensions.IExtension;
import org.java_websocket.framing.CloseFrame;
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.server.WebSocketServer;
import org.java_websocket.util.SocketUtil;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Regression test for #1460: a fatal Error (VirtualMachineError / ThreadDeath / LinkageError)
* rethrown by WebSocketImpl#decodeFrames must reach onError and close the client connection
* instead of silently killing the connect/read thread with no callback and no cleanup.
*/
public class Issue1460Test {
/**
* Draft that emulates a fatal Error during frame decoding, i.e. the trio
* WebSocketImpl#decodeFrames rethrows.
*/
private static class FatalErrorDraft extends Draft_6455 {
@Override
public List<Framedata> translateFrame(ByteBuffer buffer) {
throw new LinkageError("simulated fatal error during frame processing");
}
@Override
public Draft copyInstance() {
ArrayList<IExtension> newExtensions = new ArrayList<>();
for (IExtension knownExtension : getKnownExtensions()) {
newExtensions.add(knownExtension.copyInstance());
}
ArrayList<org.java_websocket.protocols.IProtocol> newProtocols = new ArrayList<>();
for (org.java_websocket.protocols.IProtocol knownProtocol : getKnownProtocols()) {
newProtocols.add(knownProtocol.copyInstance());
}
return new FatalErrorDraft(newExtensions, newProtocols, getMaxFrameSize());
}
FatalErrorDraft(List<IExtension> inputExtensions,
List<org.java_websocket.protocols.IProtocol> inputProtocols, int inputMaxFrameSize) {
super(inputExtensions, inputProtocols, inputMaxFrameSize);
}
FatalErrorDraft() {
super();
}
}
@Test
@Timeout(value = 10, unit = TimeUnit.SECONDS)
public void testFatalErrorTriggersOnErrorAndClose() throws InterruptedException {
int port = SocketUtil.getAvailablePort();
final CountDownLatch startLatch = new CountDownLatch(1);
final CountDownLatch errorLatch = new CountDownLatch(1);
final CountDownLatch closeLatch = new CountDownLatch(1);
final AtomicReference<Throwable> capturedError = new AtomicReference<>();
final AtomicInteger capturedCode = new AtomicInteger(-1);
WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) {
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
conn.send("trigger");
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
}
@Override
public void onMessage(WebSocket conn, String message) {
}
@Override
public void onMessage(WebSocket conn, ByteBuffer message) {
}
@Override
public void onError(WebSocket conn, Exception ex) {
ex.printStackTrace();
}
@Override
public void onStart() {
startLatch.countDown();
}
};
WebSocketClient client = new WebSocketClient(
URI.create("ws://localhost:" + port), new FatalErrorDraft()) {
@Override
public void onOpen(ServerHandshake handshakedata) {
}
@Override
public void onMessage(String message) {
}
@Override
public void onClose(int code, String reason, boolean remote) {
capturedCode.set(code);
closeLatch.countDown();
}
@Override
public void onError(Exception ex) {
capturedError.set(ex);
errorLatch.countDown();
}
};
server.start();
assertTrue(startLatch.await(5, TimeUnit.SECONDS), "server should start");
client.connectBlocking();
try {
assertTrue(errorLatch.await(5, TimeUnit.SECONDS),
"onError must be invoked for a fatal Error");
assertTrue(closeLatch.await(5, TimeUnit.SECONDS),
"onClose must be invoked for a fatal Error");
assertTrue(capturedError.get().getCause() instanceof LinkageError,
"onError must wrap the fatal Error");
assertEquals(CloseFrame.UNEXPECTED_CONDITION, capturedCode.get());
} finally {
server.stop();
}
}
}