clean up and rename fields in EventSocket WebSocket test utility class
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
parent
94956d6e8b
commit
ca404f157f
|
@ -69,7 +69,7 @@ public class ConcurrentConnectTest
|
|||
@Override
|
||||
public void configure(WebSocketServletFactory factory)
|
||||
{
|
||||
factory.register(EventSocket.EchoSocket.class);
|
||||
factory.register(EchoSocket.class);
|
||||
serverFactory = (WebSocketServerFactory)factory;
|
||||
}
|
||||
};
|
||||
|
@ -116,22 +116,22 @@ public class ConcurrentConnectTest
|
|||
|
||||
for (EventSocket l : listeners)
|
||||
{
|
||||
assertTrue(l.open.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(l.openLatch.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
for (EventSocket l : listeners)
|
||||
{
|
||||
l.getSession().getRemote().sendString("ping");
|
||||
assertThat(l.receivedMessages.poll(5, TimeUnit.SECONDS), is("ping"));
|
||||
l.getSession().close(StatusCode.NORMAL, "close from client");
|
||||
l.session.getRemote().sendString("ping");
|
||||
assertThat(l.textMessages.poll(5, TimeUnit.SECONDS), is("ping"));
|
||||
l.session.close(StatusCode.NORMAL, "close from client");
|
||||
}
|
||||
|
||||
for (EventSocket l : listeners)
|
||||
{
|
||||
assertTrue(l.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(l.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertThat(l.closeCode, is(StatusCode.NORMAL));
|
||||
assertThat(l.closeReason, is("close from client"));
|
||||
assertNull(l.failure);
|
||||
assertNull(l.error);
|
||||
}
|
||||
|
||||
closeListener.closeLatch.await(5, TimeUnit.SECONDS);
|
||||
|
|
|
@ -21,23 +21,22 @@ package org.eclipse.jetty.websocket.tests;
|
|||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
|
||||
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@WebSocket
|
||||
public class EchoSocket
|
||||
public class EchoSocket extends EventSocket
|
||||
{
|
||||
@OnWebSocketMessage
|
||||
public void onMessage(Session session, String msg) throws IOException
|
||||
@Override
|
||||
public void onMessage(String message) throws IOException
|
||||
{
|
||||
session.getRemote().sendString(msg);
|
||||
super.onMessage(message);
|
||||
session.getRemote().sendString(message);
|
||||
}
|
||||
|
||||
@OnWebSocketMessage
|
||||
public void onBinaryMessage(Session session, byte[] data, int offset, int len) throws IOException
|
||||
@Override
|
||||
public void onMessage(byte[] buf, int offset, int len) throws IOException
|
||||
{
|
||||
session.getRemote().sendBytes(ByteBuffer.wrap(data, offset, len));
|
||||
super.onMessage(buf, offset, len);
|
||||
session.getRemote().sendBytes(ByteBuffer.wrap(buf, offset, len));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -138,9 +138,9 @@ public class ErrorCloseTest
|
|||
EventSocket clientSocket = new EventSocket();
|
||||
client.connect(clientSocket, serverUri).get(5, TimeUnit.SECONDS);
|
||||
|
||||
assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertThat(serverSocket.failure.getMessage(), is("throwing from onOpen"));
|
||||
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertThat(serverSocket.error.getMessage(), is("throwing from onOpen"));
|
||||
|
||||
// Check we have stopped the WebSocketSession properly.
|
||||
assertFalse(serverSocket.session.isOpen());
|
||||
|
@ -156,9 +156,9 @@ public class ErrorCloseTest
|
|||
client.connect(clientSocket, serverUri).get(5, TimeUnit.SECONDS);
|
||||
clientSocket.session.getRemote().sendString("trigger onMessage error");
|
||||
|
||||
assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertThat(serverSocket.failure.getMessage(), is("throwing from onMessage"));
|
||||
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertThat(serverSocket.error.getMessage(), is("throwing from onMessage"));
|
||||
|
||||
// Check we have stopped the WebSocketSession properly.
|
||||
assertFalse(serverSocket.session.isOpen());
|
||||
|
@ -177,8 +177,8 @@ public class ErrorCloseTest
|
|||
try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketSession.class))
|
||||
{
|
||||
client.connect(clientSocket, serverUri).get(5, TimeUnit.SECONDS);
|
||||
assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
// Check we have stopped the WebSocketSession properly.
|
||||
|
@ -199,8 +199,8 @@ public class ErrorCloseTest
|
|||
try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketSession.class))
|
||||
{
|
||||
clientSocket.session.getRemote().sendString("trigger onMessage error");
|
||||
assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
// Check we have stopped the WebSocketSession properly.
|
||||
|
@ -218,14 +218,14 @@ public class ErrorCloseTest
|
|||
client.connect(clientSocket, serverUri).get(5, TimeUnit.SECONDS);
|
||||
|
||||
// Set a short idleTimeout on the server.
|
||||
assertTrue(serverSocket.open.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS));
|
||||
serverSocket.session.setIdleTimeout(1000);
|
||||
|
||||
// Wait for server to timeout.
|
||||
try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketSession.class))
|
||||
{
|
||||
assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
// Check we have stopped the WebSocketSession properly.
|
||||
|
@ -245,8 +245,8 @@ public class ErrorCloseTest
|
|||
try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketSession.class))
|
||||
{
|
||||
clientSocket.session.disconnect();
|
||||
assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
// Check we have stopped the WebSocketSession properly.
|
||||
|
@ -266,8 +266,8 @@ public class ErrorCloseTest
|
|||
try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketSession.class))
|
||||
{
|
||||
serverSocket.session.disconnect();
|
||||
assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
// Check we have stopped the WebSocketSession properly.
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
package org.eclipse.jetty.websocket.tests;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
|
@ -26,6 +27,7 @@ import org.eclipse.jetty.util.BlockingArrayQueue;
|
|||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.StatusCode;
|
||||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
|
||||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
|
||||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
|
||||
|
@ -35,24 +37,20 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
|||
@WebSocket
|
||||
public class EventSocket
|
||||
{
|
||||
private static Logger LOG = Log.getLogger(EventSocket.class);
|
||||
private static final Logger LOG = Log.getLogger(EventSocket.class);
|
||||
|
||||
public Session session;
|
||||
private String behavior;
|
||||
public volatile Throwable failure = null;
|
||||
public volatile int closeCode = -1;
|
||||
public volatile String closeReason = null;
|
||||
|
||||
public BlockingQueue<String> receivedMessages = new BlockingArrayQueue<>();
|
||||
public BlockingQueue<String> textMessages = new BlockingArrayQueue<>();
|
||||
public BlockingQueue<ByteBuffer> binaryMessages = new BlockingArrayQueue<>();
|
||||
public volatile int closeCode = StatusCode.UNDEFINED;
|
||||
public volatile String closeReason;
|
||||
public volatile Throwable error = null;
|
||||
|
||||
public CountDownLatch open = new CountDownLatch(1);
|
||||
public CountDownLatch error = new CountDownLatch(1);
|
||||
public CountDownLatch closed = new CountDownLatch(1);
|
||||
|
||||
public Session getSession()
|
||||
{
|
||||
return session;
|
||||
}
|
||||
public CountDownLatch openLatch = new CountDownLatch(1);
|
||||
public CountDownLatch errorLatch = new CountDownLatch(1);
|
||||
public CountDownLatch closeLatch = new CountDownLatch(1);
|
||||
|
||||
@OnWebSocketConnect
|
||||
public void onOpen(Session session)
|
||||
|
@ -61,7 +59,7 @@ public class EventSocket
|
|||
behavior = session.getPolicy().getBehavior().name();
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("{} onOpen(): {}", toString(), session);
|
||||
open.countDown();
|
||||
openLatch.countDown();
|
||||
}
|
||||
|
||||
@OnWebSocketMessage
|
||||
|
@ -69,7 +67,16 @@ public class EventSocket
|
|||
{
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("{} onMessage(): {}", toString(), message);
|
||||
receivedMessages.offer(message);
|
||||
textMessages.offer(message);
|
||||
}
|
||||
|
||||
@OnWebSocketMessage
|
||||
public void onMessage(byte[] buf, int offset, int len) throws IOException
|
||||
{
|
||||
ByteBuffer message = ByteBuffer.wrap(buf, offset, len);
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("{} onMessage(): {}", toString(), message);
|
||||
binaryMessages.offer(message);
|
||||
}
|
||||
|
||||
@OnWebSocketClose
|
||||
|
@ -79,7 +86,7 @@ public class EventSocket
|
|||
LOG.debug("{} onClose(): {}:{}", toString(), statusCode, reason);
|
||||
closeCode = statusCode;
|
||||
closeReason = reason;
|
||||
closed.countDown();
|
||||
closeLatch.countDown();
|
||||
}
|
||||
|
||||
@OnWebSocketError
|
||||
|
@ -87,8 +94,8 @@ public class EventSocket
|
|||
{
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("{} onError(): {}", toString(), cause);
|
||||
failure = cause;
|
||||
error.countDown();
|
||||
error = cause;
|
||||
errorLatch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -96,15 +103,4 @@ public class EventSocket
|
|||
{
|
||||
return String.format("[%s@%s]", behavior, Integer.toHexString(hashCode()));
|
||||
}
|
||||
|
||||
@WebSocket
|
||||
public static class EchoSocket extends EventSocket
|
||||
{
|
||||
@Override
|
||||
public void onMessage(String message) throws IOException
|
||||
{
|
||||
super.onMessage(message);
|
||||
session.getRemote().sendStringByFuture(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -115,21 +115,21 @@ public class SuspendResumeTest
|
|||
clientSocket.session.getRemote().sendString("suspend");
|
||||
clientSocket.session.getRemote().sendString("hello world");
|
||||
|
||||
assertThat(serverSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("suspend"));
|
||||
assertNull(serverSocket.receivedMessages.poll(1, TimeUnit.SECONDS));
|
||||
assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("suspend"));
|
||||
assertNull(serverSocket.textMessages.poll(1, TimeUnit.SECONDS));
|
||||
|
||||
serverSocket.suspendToken.resume();
|
||||
assertThat(serverSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("suspend"));
|
||||
assertNull(serverSocket.receivedMessages.poll(1, TimeUnit.SECONDS));
|
||||
assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("suspend"));
|
||||
assertNull(serverSocket.textMessages.poll(1, TimeUnit.SECONDS));
|
||||
|
||||
serverSocket.suspendToken.resume();
|
||||
assertThat(serverSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("hello world"));
|
||||
assertNull(serverSocket.receivedMessages.poll(1, TimeUnit.SECONDS));
|
||||
assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("hello world"));
|
||||
assertNull(serverSocket.textMessages.poll(1, TimeUnit.SECONDS));
|
||||
|
||||
// make sure both sides are closed
|
||||
clientSocket.session.close();
|
||||
assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
|
||||
// check we closed normally
|
||||
assertThat(clientSocket.closeCode, is(StatusCode.NORMAL));
|
||||
|
@ -145,29 +145,29 @@ public class SuspendResumeTest
|
|||
connect.get(5, TimeUnit.SECONDS);
|
||||
|
||||
// verify connection by sending a message from server to client
|
||||
assertTrue(serverSocket.open.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS));
|
||||
serverSocket.session.getRemote().sendString("verification");
|
||||
assertThat(clientSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("verification"));
|
||||
assertThat(clientSocket.textMessages.poll(5, TimeUnit.SECONDS), is("verification"));
|
||||
|
||||
// suspend the client so that no read events occur
|
||||
SuspendToken suspendToken = clientSocket.session.suspend();
|
||||
|
||||
// verify client can still send messages
|
||||
clientSocket.session.getRemote().sendString("message-from-client");
|
||||
assertThat(serverSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("message-from-client"));
|
||||
assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("message-from-client"));
|
||||
|
||||
// the message is not received as it is suspended
|
||||
serverSocket.session.getRemote().sendString("message-from-server");
|
||||
assertNull(clientSocket.receivedMessages.poll(2, TimeUnit.SECONDS));
|
||||
assertNull(clientSocket.textMessages.poll(2, TimeUnit.SECONDS));
|
||||
|
||||
// client should receive message after it resumes
|
||||
suspendToken.resume();
|
||||
assertThat(clientSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("message-from-server"));
|
||||
assertThat(clientSocket.textMessages.poll(5, TimeUnit.SECONDS), is("message-from-server"));
|
||||
|
||||
// make sure both sides are closed
|
||||
clientSocket.session.close();
|
||||
assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
|
||||
// check we closed normally
|
||||
assertThat(clientSocket.closeCode, is(StatusCode.NORMAL));
|
||||
|
@ -183,14 +183,14 @@ public class SuspendResumeTest
|
|||
connect.get(5, TimeUnit.SECONDS);
|
||||
|
||||
// verify connection by sending a message from server to client
|
||||
assertTrue(serverSocket.open.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS));
|
||||
serverSocket.session.getRemote().sendString("verification");
|
||||
assertThat(clientSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("verification"));
|
||||
assertThat(clientSocket.textMessages.poll(5, TimeUnit.SECONDS), is("verification"));
|
||||
|
||||
// make sure both sides are closed
|
||||
clientSocket.session.close();
|
||||
assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
|
||||
// check we closed normally
|
||||
assertThat(clientSocket.closeCode, is(StatusCode.NORMAL));
|
||||
|
|
|
@ -144,7 +144,7 @@ public class WebSocketStatsTest
|
|||
}
|
||||
}
|
||||
|
||||
assertTrue(socket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(socket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(wsConnectionClosed.await(5, TimeUnit.SECONDS));
|
||||
|
||||
assertThat(statistics.getConnectionsMax(), is(1L));
|
||||
|
|
|
@ -89,14 +89,14 @@ public class WriteAfterStopTest
|
|||
ClientUpgradeRequest upgradeRequest = new ClientUpgradeRequest();
|
||||
upgradeRequest.addExtensions("permessage-deflate");
|
||||
Session session = client.connect(clientSocket, uri, upgradeRequest).get(5, TimeUnit.SECONDS);
|
||||
clientSocket.getSession().getRemote().sendStringByFuture("init deflater");
|
||||
assertThat(serverSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("init deflater"));
|
||||
clientSocket.session.getRemote().sendStringByFuture("init deflater");
|
||||
assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("init deflater"));
|
||||
session.close(StatusCode.NORMAL, null);
|
||||
|
||||
// make sure both sides are closed
|
||||
clientSocket.session.close();
|
||||
assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
|
||||
|
||||
// check we closed normally
|
||||
assertThat(clientSocket.closeCode, is(StatusCode.NORMAL));
|
||||
|
|
|
@ -36,6 +36,7 @@ import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
|
|||
import org.eclipse.jetty.websocket.client.WebSocketClient;
|
||||
import org.eclipse.jetty.websocket.server.NativeWebSocketServletContainerInitializer;
|
||||
import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter;
|
||||
import org.eclipse.jetty.websocket.tests.EchoSocket;
|
||||
import org.eclipse.jetty.websocket.tests.EventSocket;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
|
@ -69,7 +70,7 @@ public class ClientTimeoutTest
|
|||
try
|
||||
{
|
||||
createEndpoint.await(5, TimeUnit.SECONDS);
|
||||
return new EventSocket.EchoSocket();
|
||||
return new EchoSocket();
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue