Skip to content

Commit 4a64799

Browse files
committed
[UNDERTOW-2364] Review anonymous classes in Undertow io.undertow.websockets.jsr.test
1 parent 6ae61c6 commit 4a64799

File tree

3 files changed

+97
-87
lines changed

3 files changed

+97
-87
lines changed

websockets-jsr/src/test/java/io/undertow/websockets/jsr/test/BinaryPartialEndpoint.java

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,52 +35,56 @@ public final class BinaryPartialEndpoint extends Endpoint {
3535

3636
@Override
3737
public void onOpen(final Session session, EndpointConfig config) {
38-
session.addMessageHandler(new MessageHandler.Partial<byte[]>() {
38+
session.addMessageHandler(new BinaryPartialMessageHandler(session));
3939

40-
private ByteArrayOutputStream buffer;
40+
}
4141

42-
@Override
43-
public void onMessage(byte[] bytes, boolean last) {
44-
if (last) {
45-
if (buffer == null) {
46-
onRequest(bytes);
47-
} else {
48-
try {
49-
buffer(bytes);
50-
byte[] tmp = buffer.toByteArray();
51-
onRequest(tmp);
52-
} finally {
53-
buffer = null;
54-
}
55-
}
56-
} else {
57-
buffer(bytes);
58-
}
59-
}
42+
private static class BinaryPartialMessageHandler implements MessageHandler.Partial<byte[]> {
6043

61-
private void onRequest(final byte[] bytes) {
62-
// Just return the received bytes for the test
63-
DefaultServer.getWorker().execute(new Runnable() {
64-
@Override
65-
public void run() {
66-
try {
67-
session.getBasicRemote().sendBinary(
68-
ByteBuffer.wrap(bytes));
69-
} catch (IOException e) {
70-
throw new IllegalStateException(e);
71-
}
72-
}
73-
});
74-
}
44+
private final Session session;
45+
private ByteArrayOutputStream buffer;
7546

76-
private void buffer(byte[] data) {
47+
BinaryPartialMessageHandler(Session session) {
48+
this.session = session;
49+
}
50+
51+
@Override
52+
public void onMessage(byte[] bytes, boolean last) {
53+
if (last) {
7754
if (buffer == null) {
78-
buffer = new ByteArrayOutputStream(8096);
55+
onRequest(bytes);
56+
} else {
57+
try {
58+
buffer(bytes);
59+
byte[] tmp = buffer.toByteArray();
60+
onRequest(tmp);
61+
} finally {
62+
buffer = null;
63+
}
7964
}
80-
buffer.write(data, 0, data.length);
65+
} else {
66+
buffer(bytes);
8167
}
68+
}
8269

83-
});
70+
private void onRequest(final byte[] bytes) {
71+
// Just return the received bytes for the test
72+
DefaultServer.getWorker().execute(() -> {
73+
try {
74+
session.getBasicRemote().sendBinary(
75+
ByteBuffer.wrap(bytes));
76+
} catch (IOException e) {
77+
throw new IllegalStateException(e);
78+
}
79+
});
80+
}
81+
82+
private void buffer(byte[] data) {
83+
if (buffer == null) {
84+
buffer = new ByteArrayOutputStream(8096);
85+
}
86+
buffer.write(data, 0, data.length);
87+
}
8488

8589
}
8690
}

websockets-jsr/src/test/java/io/undertow/websockets/jsr/test/ProgramaticErrorEndpoint.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,13 @@ public static String getMessage() {
4848

4949
@Override
5050
public void onOpen(Session session, EndpointConfig config) {
51-
session.addMessageHandler(new MessageHandler.Whole<String>() {
51+
session.addMessageHandler((MessageHandler.Whole<String>) message -> {
5252

53-
@Override
54-
public void onMessage(String message) {
55-
56-
QUEUE.add(message);
57-
if (message.equals("app-error")) {
58-
throw new RuntimeException("an error");
59-
} else if (message.equals("io-error")) {
60-
throw new RuntimeException(new IOException());
61-
}
53+
QUEUE.add(message);
54+
if (message.equals("app-error")) {
55+
throw new RuntimeException("an error");
56+
} else if (message.equals("io-error")) {
57+
throw new RuntimeException(new IOException());
6258
}
6359
});
6460
}

websockets-jsr/src/test/java/io/undertow/websockets/jsr/test/TestMessagesReceivedInOrder.java

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -109,45 +109,7 @@ public void testMessagesReceivedInOrder() throws Exception {
109109
final CountDownLatch done = new CountDownLatch(1);
110110
final AtomicReference<String> error = new AtomicReference<>();
111111
ContainerProvider.getWebSocketContainer()
112-
.connectToServer(new Endpoint() {
113-
@Override
114-
public void onOpen(final Session session, EndpointConfig endpointConfig) {
115-
116-
try {
117-
RemoteEndpoint.Basic rem = session.getBasicRemote();
118-
List<String> messages = new ArrayList<>();
119-
for (int i = 0; i < MESSAGES; i++) {
120-
byte[] data = new byte[2048];
121-
(new Random()).nextBytes(data);
122-
String crc = md5(data);
123-
rem.sendBinary(ByteBuffer.wrap(data));
124-
messages.add(crc);
125-
}
126-
127-
List<String> received = EchoSocket.receivedEchos.getIoFuture().get();
128-
StringBuilder sb = new StringBuilder();
129-
boolean fail = false;
130-
for (int i = 0; i < messages.size(); i++) {
131-
if (received.size() <= i) {
132-
fail = true;
133-
sb.append(i + ": should be " + messages.get(i) + " but is empty.");
134-
} else {
135-
if (!messages.get(i).equals(received.get(i))) {
136-
fail = true;
137-
sb.append(i + ": should be " + messages.get(i) + " but is " + received.get(i) + " (but found at " + received.indexOf(messages.get(i)) + ").");
138-
}
139-
}
140-
}
141-
if(fail) {
142-
error.set(sb.toString());
143-
}
144-
done.countDown();
145-
146-
} catch (Throwable t) {
147-
t.printStackTrace();
148-
}
149-
}
150-
}, clientEndpointConfig, new URI(DefaultServer.getDefaultServerURL() + "/webSocket")
112+
.connectToServer(new MessageOrderValidatorEndpoint(error, done), clientEndpointConfig, new URI(DefaultServer.getDefaultServerURL() + "/webSocket")
151113
);
152114
assertTrue(done.await(30, TimeUnit.SECONDS));
153115
if(error.get() != null) {
@@ -186,4 +148,52 @@ private static String md5(byte[] buffer) {
186148
throw new InternalError("MD5 not supported on this platform");
187149
}
188150
}
151+
152+
private static class MessageOrderValidatorEndpoint extends Endpoint {
153+
private final AtomicReference<String> error;
154+
private final CountDownLatch done;
155+
156+
MessageOrderValidatorEndpoint(AtomicReference<String> error, CountDownLatch done) {
157+
this.error = error;
158+
this.done = done;
159+
}
160+
161+
@Override
162+
public void onOpen(final Session session, EndpointConfig endpointConfig) {
163+
164+
try {
165+
RemoteEndpoint.Basic rem = session.getBasicRemote();
166+
List<String> messages = new ArrayList<>();
167+
for (int i = 0; i < MESSAGES; i++) {
168+
byte[] data = new byte[2048];
169+
(new Random()).nextBytes(data);
170+
String crc = md5(data);
171+
rem.sendBinary(ByteBuffer.wrap(data));
172+
messages.add(crc);
173+
}
174+
175+
List<String> received = EchoSocket.receivedEchos.getIoFuture().get();
176+
StringBuilder sb = new StringBuilder();
177+
boolean fail = false;
178+
for (int i = 0; i < messages.size(); i++) {
179+
if (received.size() <= i) {
180+
fail = true;
181+
sb.append(i + ": should be " + messages.get(i) + " but is empty.");
182+
} else {
183+
if (!messages.get(i).equals(received.get(i))) {
184+
fail = true;
185+
sb.append(i + ": should be " + messages.get(i) + " but is " + received.get(i) + " (but found at " + received.indexOf(messages.get(i)) + ").");
186+
}
187+
}
188+
}
189+
if(fail) {
190+
error.set(sb.toString());
191+
}
192+
done.countDown();
193+
194+
} catch (Throwable t) {
195+
t.printStackTrace();
196+
}
197+
}
198+
}
189199
}

0 commit comments

Comments
 (0)