Search in sources :

Example 31 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.

the class TextMessageTest method echoTwoMessagesTest.

@Test(groups = "standalone")
public void echoTwoMessagesTest() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient()) {
        final CountDownLatch latch = new CountDownLatch(2);
        final AtomicReference<String> text = new AtomicReference<>("");
        /* WebSocket websocket = */
        c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

            @Override
            public void onMessage(String message) {
                text.set(text.get() + message);
                latch.countDown();
            }

            @Override
            public void onOpen(WebSocket websocket) {
                websocket.sendMessage("ECHO").sendMessage("ECHO");
            }

            @Override
            public void onClose(WebSocket websocket) {
                latch.countDown();
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                latch.countDown();
            }
        }).build()).get();
        latch.await();
        assertEquals(text.get(), "ECHOECHO");
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 32 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.

the class TextMessageTest method onTimeoutCloseTest.

@Test(groups = "standalone", timeOut = 60000)
public void onTimeoutCloseTest() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient()) {
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicReference<String> text = new AtomicReference<>("");
        c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketListener() {

            @Override
            public void onOpen(WebSocket websocket) {
            }

            @Override
            public void onClose(WebSocket websocket) {
                text.set("OnClose");
                latch.countDown();
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                latch.countDown();
            }
        }).build()).get();
        latch.await();
        assertEquals(text.get(), "OnClose");
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 33 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.

the class TextMessageTest method onClose.

@Test(groups = "standalone", timeOut = 60000)
public void onClose() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient()) {
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicReference<String> text = new AtomicReference<>("");
        WebSocket websocket = c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketListener() {

            @Override
            public void onOpen(WebSocket websocket) {
            }

            @Override
            public void onClose(WebSocket websocket) {
                text.set("OnClose");
                latch.countDown();
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                latch.countDown();
            }
        }).build()).get();
        websocket.close();
        latch.await();
        assertEquals(text.get(), "OnClose");
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 34 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.

the class WebSocketWriteCompleteListenerTest method sendTextMessageExpectFailure.

@Test(groups = "standalone", timeOut = 60000, expectedExceptions = ExecutionException.class)
public void sendTextMessageExpectFailure() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient()) {
        WebSocket websocket = getWebSocket(c);
        websocket.close();
        closeFuture.get();
        websocket.sendMessage("TEXT", resultHandler());
        resultFuture.get(10, TimeUnit.SECONDS);
    }
}
Also used : AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 35 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.

the class WebSocketWriteCompleteListenerTest method streamText.

@Test(groups = "standalone")
public void streamText() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient()) {
        getWebSocket(c).stream("STREAM", true, resultHandler());
        resultFuture.get();
    }
}
Also used : AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Aggregations

AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)146 Test (org.testng.annotations.Test)119 Response (org.asynchttpclient.Response)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)66 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 CountDownLatch (java.util.concurrent.CountDownLatch)31 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 RequestBuilder (org.asynchttpclient.RequestBuilder)16 IOException (java.io.IOException)14 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 ExecutionException (java.util.concurrent.ExecutionException)13 Request (org.asynchttpclient.Request)13 WebSocket (org.asynchttpclient.ws.WebSocket)12 Test (org.junit.Test)11 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)10 File (java.io.File)9 WebSocketTextListener (org.asynchttpclient.ws.WebSocketTextListener)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7