Search in sources :

Example 11 with AbstractReflectorAtmosphereHandler

use of org.atmosphere.handler.AbstractReflectorAtmosphereHandler in project atmosphere by Atmosphere.

the class AtmosphereResourceListenerTest method testListenersCallback.

@Test
public void testListenersCallback() throws IOException, ServletException {
    framework.addAtmosphereHandler("/a", new AbstractReflectorAtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
        }

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").build();
    final AtomicReference<Boolean> suspended = new AtomicReference<Boolean>();
    final AtomicReference<Boolean> resumed = new AtomicReference<Boolean>();
    final AtomicReference<Boolean> disconnected = new AtomicReference<Boolean>();
    final AtomicReference<Boolean> preSuspended = new AtomicReference<Boolean>();
    final AtomicReference<Boolean> broadcasted = new AtomicReference<Boolean>();
    final AtmosphereResourceEventListener listener = new AtmosphereResourceEventListener() {

        @Override
        public void onPreSuspend(AtmosphereResourceEvent event) {
            preSuspended.set(true);
        }

        @Override
        public void onSuspend(AtmosphereResourceEvent event) {
            suspended.set(true);
        }

        @Override
        public void onResume(AtmosphereResourceEvent event) {
            resumed.set(true);
        }

        @Override
        public void onHeartbeat(AtmosphereResourceEvent event) {
        }

        @Override
        public void onDisconnect(AtmosphereResourceEvent event) {
            disconnected.set(true);
        }

        @Override
        public void onBroadcast(AtmosphereResourceEvent event) {
            broadcasted.set(true);
        }

        @Override
        public void onThrowable(AtmosphereResourceEvent event) {
        }

        @Override
        public void onClose(AtmosphereResourceEvent event) {
        }
    };
    framework.interceptor(new AtmosphereInterceptor() {

        @Override
        public void configure(AtmosphereConfig config) {
        }

        @Override
        public void destroy() {
        }

        @Override
        public Action inspect(AtmosphereResource r) {
            r.addEventListener(listener).suspend();
            try {
                r.getBroadcaster().broadcast("void").get();
            } catch (InterruptedException e) {
            } catch (ExecutionException e) {
            }
            return Action.CONTINUE;
        }

        @Override
        public void postInspect(AtmosphereResource r) {
            r.resume();
        }
    });
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertTrue(preSuspended.get());
    assertTrue(suspended.get());
    assertTrue(resumed.get());
    assertTrue(broadcasted.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) AbstractReflectorAtmosphereHandler(org.atmosphere.handler.AbstractReflectorAtmosphereHandler) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 12 with AbstractReflectorAtmosphereHandler

use of org.atmosphere.handler.AbstractReflectorAtmosphereHandler in project atmosphere by Atmosphere.

the class AtmosphereResourceListenerTest method testOnClose.

@Test
public void testOnClose() throws IOException, ServletException {
    framework.addAtmosphereHandler("/a", new AbstractReflectorAtmosphereHandler() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
        }

        @Override
        public void destroy() {
        }
    });
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").build();
    final AtomicReference<Boolean> closed = new AtomicReference<Boolean>();
    final AtmosphereResourceEventListener listener = new AtmosphereResourceEventListenerAdapter() {

        @Override
        public void onClose(AtmosphereResourceEvent event) {
            closed.set(true);
        }
    };
    framework.interceptor(new AtmosphereInterceptor() {

        @Override
        public void configure(AtmosphereConfig config) {
        }

        @Override
        public void destroy() {
        }

        @Override
        public Action inspect(AtmosphereResource r) {
            r.addEventListener(listener).suspend();
            try {
                r.getBroadcaster().broadcast("void").get();
            } catch (InterruptedException e) {
            } catch (ExecutionException e) {
            }
            return Action.CONTINUE;
        }

        @Override
        public void postInspect(AtmosphereResource r) {
            try {
                r.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    assertTrue(closed.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) AbstractReflectorAtmosphereHandler(org.atmosphere.handler.AbstractReflectorAtmosphereHandler) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Aggregations

IOException (java.io.IOException)12 AbstractReflectorAtmosphereHandler (org.atmosphere.handler.AbstractReflectorAtmosphereHandler)12 Test (org.testng.annotations.Test)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 HashMap (java.util.HashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 ServletException (javax.servlet.ServletException)1