Examples of AsyncHttpClient


Examples of com.ning.http.client.AsyncHttpClient

                    latch.countDown();
                }
            }
        }, new RecyclableBroadcaster("suspend"));

        AsyncHttpClient c = new AsyncHttpClient();
        try {
            c.prepareGet(urlTarget).execute(new AsyncCompletionHandler<String>() {

                @Override
                public String onCompleted(Response response) throws Exception {
                    try {
                        assertEquals(response.getResponseBody(),
                                AtmosphereResourceImpl.createCompatibleStringJunk());
                    } finally {
                        latch.countDown();
                    }
                    return null;
                }
            });

            suspended.await(20, TimeUnit.SECONDS);
            Response r = c.prepareGet(urlTarget).execute().get();

            try {
                latch.await(20, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }

            assertNotNull(r);
            assertEquals(r.getStatusCode(), 200);
        } catch (Exception e) {
            logger.error("test failed", e);
            fail(e.getMessage());
        }
        c.close();

    }
View Full Code Here

Examples of com.ning.http.client.AsyncHttpClient

        logger.info("Running testBroadcasterScope");
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch2 = new CountDownLatch(1);

        servletLatch = new CountDownLatch(1);
        AsyncHttpClient c = new AsyncHttpClient();
        Broadcaster b = null;
        try {
            long currentTime = System.currentTimeMillis();
            final AtomicReference<Response> r = new AtomicReference();
            c.prepareGet(urlTarget).execute(new AsyncCompletionHandler<Response>() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    r.set(response);
                    latch.countDown();
                    return response;
                }
            });

            servletLatch.await();

            String id = broadcasterId.get();

            b = BroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class, id);
            assertNotNull(b);
            b.broadcast("resume").get();

            try {
                latch.await();
            } catch (InterruptedException e) {
                fail(e.getMessage());
                return;
            }

            long time = System.currentTimeMillis() - currentTime;
            if (time < 5000) {
                assertTrue(true);
            } else {
                assertFalse(false);
            }
            assertNotNull(r.get());
            assertEquals(r.get().getStatusCode(), 200);
            String resume = r.get().getResponseBody();
            assertEquals(resume, "resumeresume");

            c.prepareGet(urlTarget).execute(new AsyncCompletionHandler<Response>() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    r.set(response);
                    latch2.countDown();
                    return response;
                }
            });

            try {
                latch2.await();
            } catch (InterruptedException e) {
                fail(e.getMessage());
                return;
            }

            assertFalse(id.equals(broadcasterId.get()));
        } catch (Exception e) {
            logger.error("test failed", e);
            fail(e.getMessage());
        } finally {
           if (b != null) b.destroy();
        }
        c.close();
    }
View Full Code Here

Examples of com.ning.http.client.AsyncHttpClient

    @Test(enabled = false)
    public void testProgrammaticDisconnection() {
        logger.info("{}: running test: testProgrammaticDisconnection", getClass().getSimpleName());

        AsyncHttpClient c = new AsyncHttpClient();
        long t1 = System.currentTimeMillis();

        try {
            Response r = c.prepareGet(urlTarget + "/forever").execute().get(30, TimeUnit.SECONDS);
            assertNotNull(r);
        } catch (Exception e) {
        }
        long current = System.currentTimeMillis() - t1;
        assertTrue(current > 20000 && current < 25000);
        c.close();
    }
View Full Code Here

Examples of com.ning.http.client.AsyncHttpClient

    @Test(timeOut = 25000)
    public void testProgrammaticResume() {
        logger.info("{}: running test: testProgrammaticResume", getClass().getSimpleName());

        AsyncHttpClient c = new AsyncHttpClient();
        final AtomicReference<String> location = new AtomicReference<String>();
        final AtomicReference<String> response = new AtomicReference<String>("");
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch locationLatch = new CountDownLatch(1);
        try {
            c.prepareGet(urlTarget + "/suspendAndResume").execute(new AsyncHandler<String>() {

                public void onThrowable(Throwable throwable) {
                    fail("onThrowable", throwable);
                }

                public STATE onBodyPartReceived(HttpResponseBodyPart bp) throws Exception {

                    logger.info("body part byte string: {}", new String(bp.getBodyPartBytes()));
                    response.set(response.get() + new String(bp.getBodyPartBytes()));
                    locationLatch.countDown();
                    return STATE.CONTINUE;
                }

                public STATE onStatusReceived(HttpResponseStatus hs) throws Exception {
                    return STATE.CONTINUE;
                }

                public STATE onHeadersReceived(HttpResponseHeaders rh) throws Exception {
                    location.set(rh.getHeaders().getFirstValue("Location"));
                    return STATE.CONTINUE;
                }

                public String onCompleted() throws Exception {
                    latch.countDown();
                    return "";
                }
            });

            locationLatch.await(5, TimeUnit.SECONDS);

            Response r = c.prepareGet(location.get()).execute().get(10, TimeUnit.SECONDS);
            latch.await(20, TimeUnit.SECONDS);
            assertNotNull(r);
            assertEquals(r.getStatusCode(), 200);
            assertEquals(response.get(), "suspendresume");

        } catch (Exception e) {
            logger.error("test failed", e);
            fail(e.getMessage());
        }

        c.close();
    }
View Full Code Here

Examples of com.ning.http.client.AsyncHttpClient

    @Test
    public void testResumeOnBroadcastUsingBroadcasterFactory() {
        logger.info("{}: running test: testResumeOnBroadcast", getClass().getSimpleName());

        AsyncHttpClient c = new AsyncHttpClient();
        long t1 = System.currentTimeMillis();

        try {
            Response r = c.prepareGet(urlTarget + "/subscribeAndUsingExternalThread").execute().get();
            assertNotNull(r);
            assertEquals(r.getStatusCode(), 200);
            long current = System.currentTimeMillis() - t1;
            assertTrue(current > 5000 && current < 10000);
        } catch (Exception e) {
            logger.error("test failed", e);
            fail(e.getMessage());
        }

        c.close();
    }
View Full Code Here

Examples of com.ning.http.client.AsyncHttpClient

    @Test(timeOut = 25000)
    public void testDelayBroadcast() {
        logger.info("{}: running test: testDelayBroadcast", getClass().getSimpleName());

        final CountDownLatch latch = new CountDownLatch(1);
        AsyncHttpClient c = new AsyncHttpClient();
        try {
            final AtomicReference<Response> response = new AtomicReference<Response>();
            c.prepareGet(urlTarget + "/forever").execute(new AsyncCompletionHandler<Response>() {

                @Override
                public Response onCompleted(Response r) throws Exception {
                    try {
                        response.set(r);
                        return r;
                    } finally {
                        latch.countDown();
                    }
                }
            });

            // Let Atmosphere suspend the connections.
            Thread.sleep(2500);
            c.preparePost(urlTarget + "/delay").addParameter("message", "foo").execute().get();
            c.preparePost(urlTarget + "/publishAndResume").addParameter("message", "bar").execute().get();

            try {
                latch.await(20, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }

            Response r = response.get();

            assertNotNull(r);
            assertEquals(r.getResponseBody(), AtmosphereResourceImpl.createCompatibleStringJunk() + "foo\nbar\n");
            assertEquals(r.getStatusCode(), 200);
        } catch (Exception e) {
            logger.error("test failed", e);
            fail(e.getMessage());
        }

        c.close();
    }
View Full Code Here

Examples of com.ning.http.client.AsyncHttpClient

        logger.info("{}: running test: testDelayNextBroadcast", getClass().getSimpleName());

        final CountDownLatch latch = new CountDownLatch(1);
        long t1 = System.currentTimeMillis();

        AsyncHttpClient c = new AsyncHttpClient();
        try {
            final AtomicReference<Response> response = new AtomicReference<Response>();
            c.prepareGet(urlTarget + "/forever").execute(new AsyncCompletionHandler<Response>() {

                @Override
                public Response onCompleted(Response r) throws Exception {
                    try {
                        response.set(r);
                        return r;
                    } finally {
                        latch.countDown();
                    }
                }
            });

            // Let Atmosphere suspend the connections.
            Thread.sleep(2500);
            c.preparePost(urlTarget + "/delay").addParameter("message", "foo").execute().get();
            c.preparePost(urlTarget + "/delayAndResume").addParameter("message", "bar").execute().get();

            try {
                latch.await(20, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }

            Response r = response.get();

            assertNotNull(r);
            assertEquals(r.getResponseBody(), AtmosphereResourceImpl.createCompatibleStringJunk() + "foo\nbar\n");
            assertEquals(r.getStatusCode(), 200);
            long current = System.currentTimeMillis() - t1;
            assertTrue(current > 5000 && current < 10000);
        } catch (Exception e) {
            logger.error("test failed", e);
            fail(e.getMessage());
        }

        c.close();
    }
View Full Code Here

Examples of com.ning.http.client.AsyncHttpClient

    public void testScheduleBroadcast() {
        logger.info("{}: running test: testScheduleBroadcast", getClass().getSimpleName());

        final CountDownLatch latch = new CountDownLatch(1);
        long t1 = System.currentTimeMillis();
        AsyncHttpClient c = new AsyncHttpClient();
        try {
            final AtomicReference<Response> response = new AtomicReference<Response>();
            c.prepareGet(urlTarget + "/foreverWithoutComments").execute(new AsyncCompletionHandler<Response>() {

                @Override
                public Response onCompleted(Response r) throws Exception {
                    try {
                        response.set(r);
                        return r;
                    } finally {
                        latch.countDown();
                    }
                }
            });

            // Let Atmosphere suspend the connections.
            Thread.sleep(2500);
            c.preparePost(urlTarget + "/scheduleAndResume").addParameter("message", "foo").execute().get();

            try {
                latch.await(20, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }

            Response r = response.get();
            assertNotNull(r);
            assertEquals(r.getStatusCode(), 200);
            assertEquals(r.getResponseBody(), "foo\n");
            long current = System.currentTimeMillis() - t1;
            assertTrue(current > 5000 && current < 10000);
        } catch (Exception e) {
            logger.error("test failed", e);
            fail(e.getMessage());
        }

        c.close();
    }
View Full Code Here

Examples of com.ning.http.client.AsyncHttpClient

    public void testBroadcastFilter() {
        logger.info("{}: running test: testBroadcastFilter", getClass().getSimpleName());

        final CountDownLatch latch = new CountDownLatch(1);
        long t1 = System.currentTimeMillis();
        AsyncHttpClient c = new AsyncHttpClient();
        try {
            final AtomicReference<Response> response = new AtomicReference<Response>();
            c.prepareGet(urlTarget + "/foreverWithoutComments").execute(new AsyncCompletionHandler<Response>() {

                @Override
                public Response onCompleted(Response r) throws Exception {
                    try {
                        response.set(r);
                        return r;
                    } finally {
                        latch.countDown();
                    }
                }
            });

            // Let Atmosphere suspend the connections.
            Thread.sleep(2500);
            c.preparePost(urlTarget + "/filter").addParameter("message", "<script>foo</script>").execute().get();

            try {
                latch.await(20, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }

            Response r = response.get();
            assertNotNull(r);
            assertEquals(r.getStatusCode(), 200);
            assertEquals(r.getResponseBody(), "&lt;script&gt;foo&lt;/script&gt;<br />");
        } catch (Exception e) {
            logger.error("test failed", e);
            fail(e.getMessage());
        }

        c.close();
    }
View Full Code Here

Examples of com.ning.http.client.AsyncHttpClient

    public void testAggregateFilter() {
        logger.info("{}: running test: testAggregateFilter", getClass().getSimpleName());

        final CountDownLatch latch = new CountDownLatch(1);
        long t1 = System.currentTimeMillis();
        AsyncHttpClient c = new AsyncHttpClient();
        try {
            final AtomicReference<Response> response = new AtomicReference<Response>();
            c.prepareGet(urlTarget + "/foreverWithoutComments").execute(new AsyncCompletionHandler<Response>() {

                @Override
                public Response onCompleted(Response r) throws Exception {
                    try {
                        response.set(r);
                        return r;
                    } finally {
                        latch.countDown();
                    }
                }
            });

            // Let Atmosphere suspend the connections.
            Thread.sleep(2500);
            for (int i = 0; i < 10; i++) {
                c.preparePost(urlTarget + "/aggregate").addParameter("message",
                        "==================================================").execute().get(5, TimeUnit.SECONDS);
            }


            try {
                latch.await(20, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }

            Response r = response.get();
            assertNotNull(r);
            assertEquals(r.getStatusCode(), 200);
            assertEquals(r.getResponseBody(), "==================================================\n" +
                    "==================================================\n" +
                    "==================================================\n" +
                    "==================================================\n" +
                    "==================================================\n" +
                    "==================================================\n");
        } catch (Exception e) {
            logger.error("test failed", e);
            fail(e.getMessage());
        }

        c.close();
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.