use of org.apache.catalina.servlets.DefaultServlet 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 org.apache.catalina.servlets.DefaultServlet 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));
}
use of org.apache.catalina.servlets.DefaultServlet in project tomcat by apache.
the class TestConnectionLimit method testSingleMachine.
/*
* Simple test to see how many outgoing connections can be created on a
* single machine.
*/
@Test
public void testSingleMachine() 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");
Assert.assertTrue(tomcat.getConnector().setProperty("maxConnections", "-1"));
tomcat.start();
URI uri = new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_ASYNC);
AtomicInteger counter = new AtomicInteger(0);
int threadCount = 50;
Thread[] threads = new ConnectionThread[threadCount];
for (int i = 0; i < threadCount; i++) {
threads[i] = new ConnectionThread(counter, uri);
threads[i].start();
}
// Wait for the threads to die
for (Thread thread : threads) {
thread.join();
}
System.out.println("Maximum connection count was " + counter.get());
}
Aggregations