Skip to content

Commit

Permalink
Issue #5774 - make fields and classes in Client/Server ConfigTest sta…
Browse files Browse the repository at this point in the history
…tic (#5777)

* Issue #5774 - make fields and classes in Client/Server ConfigTest static

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>

* changes from review

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Dec 10, 2020
1 parent 0a70fbf commit 1753dec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ public class ClientConfigTest
private Server server;
private WebSocketClient client;
private ServerConnector connector;
private final EchoSocket serverSocket = new EchoSocket();

private EchoSocket serverSocket = new EchoSocket();

private static String message = "this message is over 20 characters long";
private final int inputBufferSize = 200;
private final int maxMessageSize = 20;
private final int idleTimeout = 500;
private static final String MESSAGE = "this message is over 20 characters long";
private static final int INPUT_BUFFER_SIZE = 200;
private static final int MAX_MESSAGE_SIZE = 20;
private static final int IDLE_TIMEOUT = 500;

public static Stream<Arguments> data()
{
Expand Down Expand Up @@ -98,21 +97,21 @@ public void stop() throws Exception
server.stop();
}

@WebSocket(idleTimeout = idleTimeout, maxTextMessageSize = maxMessageSize, maxBinaryMessageSize = maxMessageSize, inputBufferSize = inputBufferSize, batchMode = BatchMode.ON)
public class AnnotatedConfigEndpoint extends EventSocket
@WebSocket(idleTimeout = IDLE_TIMEOUT, maxTextMessageSize = MAX_MESSAGE_SIZE, maxBinaryMessageSize = MAX_MESSAGE_SIZE, inputBufferSize = INPUT_BUFFER_SIZE, batchMode = BatchMode.ON)
public static class AnnotatedConfigEndpoint extends EventSocket
{
}

@WebSocket
public class SessionConfigEndpoint extends EventSocket
public static class SessionConfigEndpoint extends EventSocket
{
@Override
public void onOpen(Session session)
{
session.setIdleTimeout(Duration.ofMillis(idleTimeout));
session.setMaxTextMessageSize(maxMessageSize);
session.setMaxBinaryMessageSize(maxMessageSize);
session.setInputBufferSize(inputBufferSize);
session.setIdleTimeout(Duration.ofMillis(IDLE_TIMEOUT));
session.setMaxTextMessageSize(MAX_MESSAGE_SIZE);
session.setMaxBinaryMessageSize(MAX_MESSAGE_SIZE);
session.setInputBufferSize(INPUT_BUFFER_SIZE);
super.onOpen(session);
}
}
Expand All @@ -122,10 +121,10 @@ public EventSocket getClientSocket(String param)
switch (param)
{
case "clientConfig":
client.setInputBufferSize(inputBufferSize);
client.setMaxBinaryMessageSize(maxMessageSize);
client.setIdleTimeout(Duration.ofMillis(idleTimeout));
client.setMaxTextMessageSize(maxMessageSize);
client.setInputBufferSize(INPUT_BUFFER_SIZE);
client.setMaxBinaryMessageSize(MAX_MESSAGE_SIZE);
client.setIdleTimeout(Duration.ofMillis(IDLE_TIMEOUT));
client.setMaxTextMessageSize(MAX_MESSAGE_SIZE);
return new EventSocket();

case "annotatedConfig":
Expand All @@ -152,7 +151,7 @@ public void testInputBufferSize(String param) throws Exception
WebSocketCoreSession coreSession = (WebSocketCoreSession)((WebSocketSession)clientEndpoint.session).getCoreSession();
WebSocketConnection connection = coreSession.getConnection();

assertThat(connection.getInputBufferSize(), is(inputBufferSize));
assertThat(connection.getInputBufferSize(), is(INPUT_BUFFER_SIZE));

clientEndpoint.session.close();
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
Expand All @@ -171,7 +170,7 @@ public void testMaxBinaryMessageSize(String param) throws Exception
CompletableFuture<Session> connect = client.connect(clientEndpoint, uri);

connect.get(5, TimeUnit.SECONDS);
clientEndpoint.session.getRemote().sendBytes(BufferUtil.toBuffer(message));
clientEndpoint.session.getRemote().sendBytes(BufferUtil.toBuffer(MESSAGE));
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));

assertThat(clientEndpoint.error, instanceOf(MessageTooLargeException.class));
Expand All @@ -190,7 +189,7 @@ public void testIdleTimeout(String param) throws Exception

connect.get(5, TimeUnit.SECONDS);
clientEndpoint.session.getRemote().sendString("hello world");
Thread.sleep(idleTimeout + 500);
Thread.sleep(IDLE_TIMEOUT + 500);

assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.error, instanceOf(WebSocketTimeoutException.class));
Expand All @@ -208,7 +207,7 @@ public void testMaxTextMessageSize(String param) throws Exception
CompletableFuture<Session> connect = client.connect(clientEndpoint, uri);

connect.get(5, TimeUnit.SECONDS);
clientEndpoint.session.getRemote().sendString(message);
clientEndpoint.session.getRemote().sendString(MESSAGE);
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));

assertThat(clientEndpoint.error, instanceOf(MessageTooLargeException.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ public class ServerConfigTest
private Server server;
private WebSocketClient client;
private ServerConnector connector;
private ConnectionListener listener = new ConnectionListener();
private final ConnectionListener listener = new ConnectionListener();

private static String message = "this message is over 20 characters long";
private static final int inputBufferSize = 200;
private static final int maxMessageSize = 20;
private static final int idleTimeout = 500;
private static final String MESSAGE = "this message is over 20 characters long";
private static final int INPUT_BUFFER_SIZE = 200;
private static final int MAX_MESSAGE_SIZE = 20;
private static final int IDLE_TIMEOUT = 500;

private EventSocket annotatedEndpoint = new AnnotatedConfigEndpoint();
private EventSocket sessionConfigEndpoint = new SessionConfigEndpoint();
private EventSocket standardEndpoint = new EventSocket();
private final EventSocket annotatedEndpoint = new AnnotatedConfigEndpoint();
private final EventSocket sessionConfigEndpoint = new SessionConfigEndpoint();
private final EventSocket standardEndpoint = new EventSocket();

private EventSocket getServerEndpoint(String path)
{
Expand All @@ -95,7 +95,7 @@ public static Stream<Arguments> data()
return Stream.of("servletConfig", "annotatedConfig", "containerConfig", "sessionConfig").map(Arguments::of);
}

@WebSocket(idleTimeout = idleTimeout, maxTextMessageSize = maxMessageSize, maxBinaryMessageSize = maxMessageSize, inputBufferSize = inputBufferSize, batchMode = BatchMode.ON)
@WebSocket(idleTimeout = IDLE_TIMEOUT, maxTextMessageSize = MAX_MESSAGE_SIZE, maxBinaryMessageSize = MAX_MESSAGE_SIZE, inputBufferSize = INPUT_BUFFER_SIZE, batchMode = BatchMode.ON)
public static class AnnotatedConfigEndpoint extends EventSocket
{
}
Expand All @@ -106,10 +106,10 @@ public static class SessionConfigEndpoint extends EventSocket
@Override
public void onOpen(Session session)
{
session.setIdleTimeout(Duration.ofMillis(idleTimeout));
session.setMaxTextMessageSize(maxMessageSize);
session.setMaxBinaryMessageSize(maxMessageSize);
session.setInputBufferSize(inputBufferSize);
session.setIdleTimeout(Duration.ofMillis(IDLE_TIMEOUT));
session.setMaxTextMessageSize(MAX_MESSAGE_SIZE);
session.setMaxBinaryMessageSize(MAX_MESSAGE_SIZE);
session.setInputBufferSize(INPUT_BUFFER_SIZE);
super.onOpen(session);
}
}
Expand All @@ -119,10 +119,10 @@ public class WebSocketFactoryConfigServlet extends JettyWebSocketServlet
@Override
public void configure(JettyWebSocketServletFactory factory)
{
factory.setIdleTimeout(Duration.ofMillis(idleTimeout));
factory.setMaxTextMessageSize(maxMessageSize);
factory.setMaxBinaryMessageSize(maxMessageSize);
factory.setInputBufferSize(inputBufferSize);
factory.setIdleTimeout(Duration.ofMillis(IDLE_TIMEOUT));
factory.setMaxTextMessageSize(MAX_MESSAGE_SIZE);
factory.setMaxBinaryMessageSize(MAX_MESSAGE_SIZE);
factory.setInputBufferSize(INPUT_BUFFER_SIZE);
factory.addMapping("/", (req, resp) -> standardEndpoint);
}
}
Expand All @@ -145,10 +145,10 @@ public void configure(JettyWebSocketServletFactory factory)
}
}

public class ConnectionListener implements Connection.Listener
public static class ConnectionListener implements Connection.Listener
{
private AtomicInteger opened = new AtomicInteger(0);
private CountDownLatch closed = new CountDownLatch(1);
private final AtomicInteger opened = new AtomicInteger(0);
private final CountDownLatch closed = new CountDownLatch(1);

@Override
public void onOpened(Connection connection)
Expand Down Expand Up @@ -188,10 +188,10 @@ public void start() throws Exception

JettyWebSocketServletContainerInitializer.configure(contextHandler, (context, container) ->
{
container.setIdleTimeout(Duration.ofMillis(idleTimeout));
container.setMaxTextMessageSize(maxMessageSize);
container.setMaxBinaryMessageSize(maxMessageSize);
container.setInputBufferSize(inputBufferSize);
container.setIdleTimeout(Duration.ofMillis(IDLE_TIMEOUT));
container.setMaxTextMessageSize(MAX_MESSAGE_SIZE);
container.setMaxBinaryMessageSize(MAX_MESSAGE_SIZE);
container.setInputBufferSize(INPUT_BUFFER_SIZE);
container.addMapping("/containerConfig", (req, resp) -> standardEndpoint);
});

Expand Down Expand Up @@ -223,7 +223,7 @@ public void testInputBufferSize(String path) throws Exception
WebSocketCoreSession coreSession = (WebSocketCoreSession)((WebSocketSession)serverEndpoint.session).getCoreSession();
WebSocketConnection connection = coreSession.getConnection();

assertThat(connection.getInputBufferSize(), is(inputBufferSize));
assertThat(connection.getInputBufferSize(), is(INPUT_BUFFER_SIZE));

serverEndpoint.session.close();
assertTrue(serverEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
Expand All @@ -245,7 +245,7 @@ public void testMaxBinaryMessageSize(String path) throws Exception
CompletableFuture<Session> connect = client.connect(clientEndpoint, uri);

connect.get(5, TimeUnit.SECONDS);
clientEndpoint.session.getRemote().sendBytes(BufferUtil.toBuffer(message));
clientEndpoint.session.getRemote().sendBytes(BufferUtil.toBuffer(MESSAGE));
assertTrue(serverEndpoint.closeLatch.await(5, TimeUnit.SECONDS));

assertThat(serverEndpoint.error, instanceOf(MessageTooLargeException.class));
Expand All @@ -269,7 +269,7 @@ public void testIdleTimeout(String path) throws Exception
clientEndpoint.session.getRemote().sendString("hello world");
String msg = serverEndpoint.textMessages.poll(500, TimeUnit.MILLISECONDS);
assertThat(msg, is("hello world"));
Thread.sleep(idleTimeout + 500);
Thread.sleep(IDLE_TIMEOUT + 500);

assertTrue(serverEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(serverEndpoint.error, instanceOf(WebSocketTimeoutException.class));
Expand All @@ -290,7 +290,7 @@ public void testMaxTextMessageSize(String path) throws Exception
CompletableFuture<Session> connect = client.connect(clientEndpoint, uri);

connect.get(5, TimeUnit.SECONDS);
clientEndpoint.session.getRemote().sendString(message);
clientEndpoint.session.getRemote().sendString(MESSAGE);
assertTrue(serverEndpoint.closeLatch.await(5, TimeUnit.SECONDS));

assertThat(serverEndpoint.error, instanceOf(MessageTooLargeException.class));
Expand Down

0 comments on commit 1753dec

Please sign in to comment.