use of jakarta.websocket.WebSocketContainer in project tomcat by apache.
the class TestWsWebSocketContainerTimeoutClient method doTestWriteTimeoutClient.
private void doTestWriteTimeoutClient(boolean setTimeoutOnContainer) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(BlockingConfig.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
// Set the async timeout
if (setTimeoutOnContainer) {
wsContainer.setAsyncSendTimeout(TIMEOUT_MS);
}
tomcat.start();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://" + getHostName() + ":" + getPort() + BlockingConfig.PATH));
if (!setTimeoutOnContainer) {
wsSession.getAsyncRemote().setSendTimeout(TIMEOUT_MS);
}
long lastSend = 0;
// Should send quickly until the network buffers fill up and then block
// until the timeout kicks in
Exception exception = null;
try {
while (true) {
lastSend = System.currentTimeMillis();
Future<Void> f = wsSession.getAsyncRemote().sendBinary(ByteBuffer.wrap(MESSAGE_BINARY_4K));
f.get();
}
} catch (Exception e) {
exception = e;
}
long timeout = System.currentTimeMillis() - lastSend;
// Clear the server side block and prevent further blocks to allow the
// server to shutdown cleanly
BlockingPojo.clearBlock();
// Close the client session, primarily to allow the
// BackgroundProcessManager to shut down.
wsSession.close();
String msg = "Time out was [" + timeout + "] ms";
// Check correct time passed
Assert.assertTrue(msg, timeout >= TIMEOUT_MS - MARGIN);
// Check the timeout wasn't too long
Assert.assertTrue(msg, timeout < TIMEOUT_MS * 2);
Assert.assertNotNull(exception);
}
use of jakarta.websocket.WebSocketContainer in project tomcat by apache.
the class TestWsPingPongMessages method testPingPongMessages.
@Test
public void testPingPongMessages() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
CountDownLatch latch = new CountDownLatch(1);
TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties().get("endpoint");
tep.setLatch(latch);
PongMessageHandler handler = new PongMessageHandler(latch);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendPing(applicationData);
boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
Assert.assertTrue(latchResult);
Assert.assertArrayEquals(applicationData.array(), (handler.getMessages().peek()).getApplicationData().array());
}
use of jakarta.websocket.WebSocketContainer in project tomcat by apache.
the class TestWsSubprotocols method testWsSubprotocols.
@Test
public void testWsSubprotocols() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
tomcat.start();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().preferredSubprotocols(Arrays.asList("sp3")).build(), new URI("ws://localhost:" + getPort() + SubProtocolsEndpoint.PATH_BASIC));
Assert.assertTrue(wsSession.isOpen());
if (wsSession.getNegotiatedSubprotocol() != null) {
Assert.assertTrue(wsSession.getNegotiatedSubprotocol().isEmpty());
}
wsSession.close();
SubProtocolsEndpoint.recycle();
wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().preferredSubprotocols(Arrays.asList("sp2")).build(), new URI("ws://localhost:" + getPort() + SubProtocolsEndpoint.PATH_BASIC));
Assert.assertTrue(wsSession.isOpen());
Assert.assertEquals("sp2", wsSession.getNegotiatedSubprotocol());
// Client thread might move faster than server. Wait for upto 5s for the
// subProtocols to be set
int count = 0;
while (count < 50 && SubProtocolsEndpoint.subprotocols == null) {
count++;
Thread.sleep(100);
}
Assert.assertNotNull(SubProtocolsEndpoint.subprotocols);
Assert.assertArrayEquals(new String[] { "sp1", "sp2" }, SubProtocolsEndpoint.subprotocols.toArray(new String[2]));
wsSession.close();
SubProtocolsEndpoint.recycle();
}
use of jakarta.websocket.WebSocketContainer in project tomcat by apache.
the class TestWebSocketFrameClient method echoTester.
public void echoTester(String path, ClientEndpointConfig clientEndpointConfig) throws Exception {
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
if (clientEndpointConfig == null) {
clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
}
// Increase default timeout from 5s to 10s to try and reduce errors on
// CI systems.
clientEndpointConfig.getUserProperties().put(Constants.IO_TIMEOUT_MS_PROPERTY, "10000");
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + path));
CountDownLatch latch = new CountDownLatch(1);
BasicText handler = new BasicText(latch);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendText("Hello");
boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
Assert.assertTrue(latchResult);
Queue<String> messages = handler.getMessages();
Assert.assertEquals(1, messages.size());
for (String message : messages) {
Assert.assertEquals("Hello", message);
}
wsSession.close();
}
use of jakarta.websocket.WebSocketContainer in project tomcat by apache.
the class TestShutdown method testShutdownBufferedMessages.
@Test
public void testShutdownBufferedMessages() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(EchoBufferedConfig.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + "/test"));
CountDownLatch latch = new CountDownLatch(1);
BasicText handler = new BasicText(latch);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendText("Hello");
int count = 0;
while (count < 10 && EchoBufferedEndpoint.messageCount.get() == 0) {
Thread.sleep(200);
count++;
}
Assert.assertNotEquals("Message not received by server", EchoBufferedEndpoint.messageCount.get(), 0);
tomcat.stop();
Assert.assertTrue("Latch expired waiting for message", latch.await(10, TimeUnit.SECONDS));
}
Aggregations