Search in sources :

Example 1 with DONE

use of org.xnio.IoFuture.Status.DONE in project undertow by undertow-io.

the class JsrWebSocketServer07Test method testTextByCompletion.

@org.junit.Test
public void testTextByCompletion() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<SendResult> sendResult = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult<?> latch = new FutureResult<>();
    final FutureResult<Void> latch2 = new FutureResult<>();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Whole<String>() {

                @Override
                public void onMessage(String message) {
                    session.getAsyncRemote().sendText(message, result -> {
                        sendResult.set(result);
                        if (result.getException() != null) {
                            latch2.setException(new IOException(result.getException()));
                        } else {
                            latch2.setResult(null);
                        }
                    });
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorkerSupplier(), DefaultServer.getBufferPool(), Collections.emptyList(), false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch));
    // FIXME UNDERTOW-1862 assertEquals(DONE, latch.getIoFuture().await());
    latch.getIoFuture().await();
    assertEquals(DONE, latch2.getIoFuture().await());
    SendResult result = sendResult.get();
    Assert.assertNotNull(result);
    assertNull(result.getException());
    client.destroy();
}
Also used : ServletException(javax.servlet.ServletException) TestClassIntrospector(io.undertow.servlet.test.util.TestClassIntrospector) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) Endpoint(javax.websocket.Endpoint) Future(java.util.concurrent.Future) MessageHandler(javax.websocket.MessageHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) URI(java.net.URI) NetworkUtils(io.undertow.util.NetworkUtils) SendResult(javax.websocket.SendResult) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Session(javax.websocket.Session) ContainerProvider(javax.websocket.ContainerProvider) DefaultServer(io.undertow.testutils.DefaultServer) FilterInfo(io.undertow.servlet.api.FilterInfo) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) Writer(java.io.Writer) AjpIgnore(io.undertow.testutils.AjpIgnore) UndertowSession(io.undertow.websockets.jsr.UndertowSession) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) CloseReason(javax.websocket.CloseReason) HttpOneOnly(io.undertow.testutils.HttpOneOnly) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) SendHandler(javax.websocket.SendHandler) WebSocketVersion(io.netty.handler.codec.http.websocketx.WebSocketVersion) PathHandler(io.undertow.server.handlers.PathHandler) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) OutputStream(java.io.OutputStream) EndpointConfig(javax.websocket.EndpointConfig) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) JsrWebSocketFilter(io.undertow.websockets.jsr.JsrWebSocketFilter) ServletContainer(io.undertow.servlet.api.ServletContainer) DeploymentManager(io.undertow.servlet.api.DeploymentManager) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) TimeUnit(java.util.concurrent.TimeUnit) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) DONE(org.xnio.IoFuture.Status.DONE) Assert.assertNull(org.junit.Assert.assertNull) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FutureResult(org.xnio.FutureResult) DispatcherType(javax.servlet.DispatcherType) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) MessageHandler(javax.websocket.MessageHandler) URI(java.net.URI) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) SendResult(javax.websocket.SendResult) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession)

Aggregations

Unpooled (io.netty.buffer.Unpooled)1 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)1 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)1 PingWebSocketFrame (io.netty.handler.codec.http.websocketx.PingWebSocketFrame)1 PongWebSocketFrame (io.netty.handler.codec.http.websocketx.PongWebSocketFrame)1 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1 WebSocketVersion (io.netty.handler.codec.http.websocketx.WebSocketVersion)1 PathHandler (io.undertow.server.handlers.PathHandler)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 DeploymentManager (io.undertow.servlet.api.DeploymentManager)1 FilterInfo (io.undertow.servlet.api.FilterInfo)1 ServletContainer (io.undertow.servlet.api.ServletContainer)1 TestClassIntrospector (io.undertow.servlet.test.util.TestClassIntrospector)1 AjpIgnore (io.undertow.testutils.AjpIgnore)1 DefaultServer (io.undertow.testutils.DefaultServer)1 HttpOneOnly (io.undertow.testutils.HttpOneOnly)1 NetworkUtils (io.undertow.util.NetworkUtils)1 JsrWebSocketFilter (io.undertow.websockets.jsr.JsrWebSocketFilter)1 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)1 UndertowSession (io.undertow.websockets.jsr.UndertowSession)1