Search in sources :

Example 26 with AsyncHttpClient

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

the class AsyncHttpClientRegistryTest method testRegisterIfNew.

@Test(groups = "standalone")
public void testRegisterIfNew() throws IOException {
    try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
        try (AsyncHttpClient ahc2 = AsyncHttpClientFactory.getAsyncHttpClient()) {
            Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc));
            Assert.assertFalse(AsyncHttpClientRegistryImpl.getInstance().registerIfNew(TEST_AHC, ahc2));
            Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC) == ahc);
            Assert.assertNotNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc2));
            Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC) == ahc2);
            Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().registerIfNew(TEST_AHC + 1, ahc));
            Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC + 1) == ahc);
        }
    }
}
Also used : AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 27 with AsyncHttpClient

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

the class AsyncHttpClientRegistryTest method testGetAndRegister.

@Test(groups = "standalone")
public void testGetAndRegister() throws IOException {
    try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
        Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC));
        Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc));
        Assert.assertNotNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC));
    }
}
Also used : AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 28 with AsyncHttpClient

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

the class AsyncHttpObservableTest method testObserveMultiple.

@Test(groups = "standalone")
public void testObserveMultiple() {
    final TestSubscriber<Response> tester = new TestSubscriber<>();
    try (AsyncHttpClient client = asyncHttpClient()) {
        Observable<Response> o1 = AsyncHttpObservable.observe(() -> client.prepareGet("http://gatling.io"));
        Observable<Response> o2 = AsyncHttpObservable.observe(() -> client.prepareGet("http://www.wisc.edu").setFollowRedirect(true));
        Observable<Response> o3 = AsyncHttpObservable.observe(() -> client.prepareGet("http://www.umn.edu").setFollowRedirect(true));
        Observable<Response> all = Observable.merge(o1, o2, o3);
        all.subscribe(tester);
        tester.awaitTerminalEvent();
        tester.assertTerminalEvent();
        tester.assertNoErrors();
        tester.assertCompleted();
        List<Response> responses = tester.getOnNextEvents();
        assertNotNull(responses);
        assertEquals(responses.size(), 3);
        for (Response response : responses) {
            assertEquals(response.getStatusCode(), 200);
        }
    } catch (Exception e) {
        Thread.currentThread().interrupt();
    }
}
Also used : Response(org.asynchttpclient.Response) TestSubscriber(rx.observers.TestSubscriber) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 29 with AsyncHttpClient

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

the class AsyncHttpSingleTest method testAbort.

@Test(groups = "standalone")
public void testAbort() throws Exception {
    final TestSubscriber<Response> subscriber = new TestSubscriber<>();
    try (AsyncHttpClient client = asyncHttpClient()) {
        final Single<Response> underTest = AsyncHttpSingle.create(client.prepareGet("http://gatling.io"), () -> new AsyncCompletionHandlerBase() {

            @Override
            public State onStatusReceived(HttpResponseStatus status) {
                return State.ABORT;
            }
        });
        underTest.subscribe(subscriber);
        subscriber.awaitTerminalEvent();
    }
    subscriber.assertTerminalEvent();
    subscriber.assertNoErrors();
    subscriber.assertCompleted();
    subscriber.assertValue(null);
}
Also used : Response(org.asynchttpclient.Response) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) TestSubscriber(rx.observers.TestSubscriber) AsyncCompletionHandlerBase(org.asynchttpclient.AsyncCompletionHandlerBase) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 30 with AsyncHttpClient

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

the class TextMessageTest method echoDoubleListenerText.

@Test(groups = "standalone", timeOut = 60000)
public void echoDoubleListenerText() 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(message);
                latch.countDown();
            }

            @Override
            public void onOpen(WebSocket websocket) {
            }

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

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                latch.countDown();
            }
        }).addWebSocketListener(new WebSocketTextListener() {

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

            @Override
            public void onOpen(WebSocket websocket) {
            }

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

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                latch.countDown();
            }
        }).build()).get();
        websocket.sendMessage("ECHO");
        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)

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