Issue #3406 - fix jetty websocket tests

Signed-off-by: lachan-roberts <lachlan@webtide.com>
This commit is contained in:
lachan-roberts 2019-03-07 10:52:03 +11:00
parent 155974bc07
commit 59ec0da768
5 changed files with 22 additions and 19 deletions

View File

@ -118,18 +118,19 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketPoli
* @param websocket the websocket object * @param websocket the websocket object
* @param toUri the websocket uri to connect to * @param toUri the websocket uri to connect to
* @param request the upgrade request information * @param request the upgrade request information
* @param listener the upgrade listener * @param upgradeListener the upgrade listener
* @return the future for the session, available on success of connect * @return the future for the session, available on success of connect
* @throws IOException if unable to connect * @throws IOException if unable to connect
*/ */
public CompletableFuture<Session> connect(Object websocket, URI toUri, UpgradeRequest request, UpgradeListener listener) throws IOException public CompletableFuture<Session> connect(Object websocket, URI toUri, UpgradeRequest request, UpgradeListener upgradeListener) throws IOException
{ {
for (Connection.Listener listener : getBeans(Connection.Listener.class)) for (Connection.Listener listener : getBeans(Connection.Listener.class))
coreClient.addBean(listener); coreClient.addBean(listener);
JettyClientUpgradeRequest upgradeRequest = new JettyClientUpgradeRequest(this, coreClient, request, toUri, websocket); JettyClientUpgradeRequest upgradeRequest = new JettyClientUpgradeRequest(this, coreClient, request, toUri, websocket);
if (listener != null) if (upgradeListener != null)
upgradeRequest.addListener(listener); upgradeRequest.addListener(upgradeListener);
coreClient.connect(upgradeRequest); coreClient.connect(upgradeRequest);
return upgradeRequest.getFutureSession(); return upgradeRequest.getFutureSession();
} }

View File

@ -28,7 +28,6 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
@ -39,12 +38,12 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.CloseException;
import org.eclipse.jetty.websocket.api.MessageTooLargeException; import org.eclipse.jetty.websocket.api.MessageTooLargeException;
import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketFrameListener; import org.eclipse.jetty.websocket.api.WebSocketFrameListener;
import org.eclipse.jetty.websocket.api.WebSocketListener; import org.eclipse.jetty.websocket.api.WebSocketListener;
import org.eclipse.jetty.websocket.api.WebSocketTimeoutException;
import org.eclipse.jetty.websocket.api.util.WSURI; import org.eclipse.jetty.websocket.api.util.WSURI;
import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.core.CloseStatus; import org.eclipse.jetty.websocket.core.CloseStatus;
@ -250,8 +249,8 @@ public class ClientCloseTest
// client reads -1 (EOF) // client reads -1 (EOF)
// client triggers close event on client ws-endpoint // client triggers close event on client ws-endpoint
clientSocket.assertReceivedCloseEvent(clientTimeout * 2, clientSocket.assertReceivedCloseEvent(clientTimeout * 2,
is(StatusCode.SHUTDOWN), is(StatusCode.ABNORMAL),
containsString("timeout")); containsString("Channel Closed"));
} }
clientSessionTracker.assertClosedProperly(client); clientSessionTracker.assertClosedProperly(client);
@ -283,12 +282,11 @@ public class ClientCloseTest
// client close should occur // client close should occur
clientSocket.assertReceivedCloseEvent(clientTimeout * 2, clientSocket.assertReceivedCloseEvent(clientTimeout * 2,
is(StatusCode.SHUTDOWN), is(StatusCode.SHUTDOWN),
containsString("timeout")); containsString("Timeout"));
// client idle timeout triggers close event on client ws-endpoint // client idle timeout triggers close event on client ws-endpoint
assertThat("OnError Latch", clientSocket.errorLatch.await(2, SECONDS), is(true)); assertThat("OnError Latch", clientSocket.errorLatch.await(2, SECONDS), is(true));
assertThat("OnError", clientSocket.error.get(), instanceOf(CloseException.class)); assertThat("OnError", clientSocket.error.get(), instanceOf(WebSocketTimeoutException.class));
assertThat("OnError.cause", clientSocket.error.get().getCause(), instanceOf(TimeoutException.class));
} }
clientSessionTracker.assertClosedProperly(client); clientSessionTracker.assertClosedProperly(client);
@ -328,7 +326,7 @@ public class ClientCloseTest
// clients disconnect // clients disconnect
for (int i = 0; i < sessionCount; i++) for (int i = 0; i < sessionCount; i++)
{ {
clientSockets.get(i).assertReceivedCloseEvent(timeout, is(StatusCode.ABNORMAL), containsString("Disconnected")); clientSockets.get(i).assertReceivedCloseEvent(timeout, is(StatusCode.ABNORMAL), containsString("Channel Closed"));
} }
// ensure all Sessions are gone. connections are gone. etc. (client and server) // ensure all Sessions are gone. connections are gone. etc. (client and server)
@ -368,7 +366,7 @@ public class ClientCloseTest
// client triggers close event on client ws-endpoint // client triggers close event on client ws-endpoint
// assert - close code==1006 (abnormal) // assert - close code==1006 (abnormal)
clientSocket.assertReceivedCloseEvent(timeout, is(StatusCode.ABNORMAL), containsString("Eof")); clientSocket.assertReceivedCloseEvent(timeout, is(StatusCode.ABNORMAL), containsString("Channel Closed"));
clientSessionTracker.assertClosedProperly(client); clientSessionTracker.assertClosedProperly(client);
} }

View File

@ -145,6 +145,8 @@ public class ClientSessionsTest
String received = cliSock.messageQueue.poll(5, TimeUnit.SECONDS); String received = cliSock.messageQueue.poll(5, TimeUnit.SECONDS);
assertThat("Message", received, containsString("Hello World!")); assertThat("Message", received, containsString("Hello World!"));
sess.close(StatusCode.NORMAL, null);
} }
cliSock.assertReceivedCloseEvent(30000, is(StatusCode.NORMAL)); cliSock.assertReceivedCloseEvent(30000, is(StatusCode.NORMAL));

View File

@ -36,6 +36,7 @@ import org.eclipse.jetty.websocket.api.util.WSURI;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.common.WebSocketSessionImpl; import org.eclipse.jetty.websocket.common.WebSocketSessionImpl;
import org.eclipse.jetty.websocket.core.internal.WebSocketChannel;
import org.eclipse.jetty.websocket.server.JettyWebSocketServletContainerInitializer; import org.eclipse.jetty.websocket.server.JettyWebSocketServletContainerInitializer;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet; import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
@ -148,7 +149,7 @@ public class ServerCloseTest
// Verify that server socket got close event // Verify that server socket got close event
AbstractCloseEndpoint serverEndpoint = serverEndpointCreator.pollLastCreated(); AbstractCloseEndpoint serverEndpoint = serverEndpointCreator.pollLastCreated();
assertThat("Fast Close Latch", serverEndpoint.closeLatch.await(5, SECONDS), is(true)); assertThat("Fast Close Latch", serverEndpoint.closeLatch.await(5, SECONDS), is(true));
assertThat("Fast Close.statusCode", serverEndpoint.closeStatusCode, is(StatusCode.ABNORMAL)); assertThat("Fast Close.statusCode", serverEndpoint.closeStatusCode, is(StatusCode.NORMAL));
} }
finally finally
{ {
@ -172,7 +173,7 @@ public class ServerCloseTest
Future<Session> futSession = client.connect(clientEndpoint, wsUri, request); Future<Session> futSession = client.connect(clientEndpoint, wsUri, request);
Session session = null; Session session = null;
try(StacklessLogging ignore = new StacklessLogging(FastFailEndpoint.class, WebSocketSessionImpl.class)) try(StacklessLogging ignore = new StacklessLogging(WebSocketChannel.class))
{ {
session = futSession.get(5, SECONDS); session = futSession.get(5, SECONDS);
@ -219,12 +220,13 @@ public class ServerCloseTest
clientEndpoint.getEndPoint().close(); clientEndpoint.getEndPoint().close();
// Verify that client got close // Verify that client got close
clientEndpoint.assertReceivedCloseEvent(5000, is(StatusCode.ABNORMAL), containsString("Disconnected")); clientEndpoint.assertReceivedCloseEvent(5000, is(StatusCode.ABNORMAL), containsString("Channel Closed"));
// Verify that server socket got close event // Verify that server socket got close event
AbstractCloseEndpoint serverEndpoint = serverEndpointCreator.pollLastCreated(); AbstractCloseEndpoint serverEndpoint = serverEndpointCreator.pollLastCreated();
serverEndpoint.assertReceivedCloseEvent(5000, is(StatusCode.ABNORMAL), containsString("Disconnected")); serverEndpoint.assertReceivedCloseEvent(5000, is(StatusCode.ABNORMAL), containsString("Channel Closed"));
} finally }
finally
{ {
close(session); close(session);
} }

View File

@ -134,7 +134,7 @@ public class WebSocketChannelState
default: default:
if (_closeStatus == null || CloseStatus.isOrdinary(_closeStatus)) if (_closeStatus == null || CloseStatus.isOrdinary(_closeStatus))
_closeStatus = CloseStatus.NO_CLOSE_STATUS; _closeStatus = new CloseStatus(CloseStatus.NO_CLOSE, "Channel Closed");
_channelState = State.CLOSED; _channelState = State.CLOSED;
return true; return true;
} }