Search in sources :

Example 1 with WebSocketScopeContext

use of org.eclipse.jetty.cdi.websocket.WebSocketScopeContext in project jetty.project by eclipse.

the class WebSocketScopeBaselineTest method testScopeBehavior.

/**
     * Test behavior of {@link WebSocketScope} in basic operation.
     * <p>
     * Food is declared as part of WebSocketScope, and as such, only 1 instance of it can exist.
     * @throws Exception on test failure
     */
@Test
public void testScopeBehavior() throws Exception {
    ScopedInstance<WebSocketScopeContext> wsScopeBean = newInstance(WebSocketScopeContext.class);
    WebSocketScopeContext wsScope = wsScopeBean.instance;
    wsScope.create();
    Meal meal1;
    try {
        wsScope.begin();
        ScopedInstance<Meal> meal1Bean = newInstance(Meal.class);
        meal1 = meal1Bean.instance;
        ScopedInstance<Meal> meal2Bean = newInstance(Meal.class);
        Meal meal2 = meal2Bean.instance;
        assertThat("Meals are not the same", meal1, not(sameInstance(meal2)));
        assertThat("Meal 1 Entree Constructed", meal1.getEntree().isConstructed(), is(true));
        assertThat("Meal 1 Side Constructed", meal1.getSide().isConstructed(), is(true));
        /* Since Food is annotated with @WebSocketScope, there can only be one instance of it
             * in use with the 2 Meal objects.
             */
        assertThat("Meal parts not the same", meal1.getEntree(), sameInstance(meal1.getSide()));
        assertThat("Meal entrees are the same", meal1.getEntree(), sameInstance(meal2.getEntree()));
        assertThat("Meal sides are the same", meal1.getSide(), sameInstance(meal2.getSide()));
        meal1Bean.destroy();
        meal2Bean.destroy();
    } finally {
        wsScope.end();
    }
    Food entree1 = meal1.getEntree();
    Food side1 = meal1.getSide();
    assertThat("Meal 1 entree destroyed", entree1.isDestroyed(), is(false));
    assertThat("Meal 1 side destroyed", side1.isDestroyed(), is(false));
    wsScope.destroy();
    // assertThat("Meal 1 entree destroyed",entree1.isDestroyed(),is(true));
    // assertThat("Meal 1 side destroyed",side1.isDestroyed(),is(true));
    wsScopeBean.destroy();
}
Also used : WebSocketScopeContext(org.eclipse.jetty.cdi.websocket.WebSocketScopeContext) Test(org.junit.Test)

Example 2 with WebSocketScopeContext

use of org.eclipse.jetty.cdi.websocket.WebSocketScopeContext in project jetty.project by eclipse.

the class WebSocketScopeSessionTest method testMultiSession_Overlapping.

@Test
public void testMultiSession_Overlapping() throws Exception {
    final CountDownLatch midLatch = new CountDownLatch(2);
    final CountDownLatch end1Latch = new CountDownLatch(1);
    Callable<Session> call1 = new Callable<Session>() {

        @Override
        public Session call() throws Exception {
            Session ret = null;
            ScopedInstance<WebSocketScopeContext> wsScope1Bean = newInstance(WebSocketScopeContext.class);
            WebSocketScopeContext wsScope1 = wsScope1Bean.instance;
            wsScope1.create();
            try {
                // Scope 1
                wsScope1.begin();
                BogusSession sess = new BogusSession("1");
                wsScope1.setSession(sess);
                midLatch.countDown();
                midLatch.await(1, TimeUnit.SECONDS);
                ScopedInstance<BogusSocket> sock1Bean = newInstance(BogusSocket.class);
                BogusSocket sock1 = sock1Bean.instance;
                assertThat("Socket 1 Session", sock1.getSession(), sameInstance((Session) sess));
                ret = sock1.getSession();
                sock1Bean.destroy();
            } finally {
                wsScope1.end();
            }
            wsScope1.destroy();
            wsScope1Bean.destroy();
            end1Latch.countDown();
            return ret;
        }
    };
    final CountDownLatch end2Latch = new CountDownLatch(1);
    Callable<Session> call2 = new Callable<Session>() {

        @Override
        public Session call() throws Exception {
            Session ret = null;
            ScopedInstance<WebSocketScopeContext> wsScope2Bean = newInstance(WebSocketScopeContext.class);
            WebSocketScopeContext wsScope2 = wsScope2Bean.instance;
            wsScope2.create();
            try {
                // Scope 2
                wsScope2.begin();
                BogusSession sess = new BogusSession("2");
                wsScope2.setSession(sess);
                ScopedInstance<BogusSocket> sock2Bean = newInstance(BogusSocket.class);
                midLatch.countDown();
                midLatch.await(1, TimeUnit.SECONDS);
                BogusSocket sock2 = sock2Bean.instance;
                ret = sock2.getSession();
                assertThat("Socket 2 Session", sock2.getSession(), sameInstance((Session) sess));
                sock2Bean.destroy();
            } finally {
                wsScope2.end();
            }
            wsScope2.destroy();
            wsScope2Bean.destroy();
            end2Latch.countDown();
            return ret;
        }
    };
    ExecutorService svc = Executors.newFixedThreadPool(4);
    Future<Session> fut1 = svc.submit(call1);
    Future<Session> fut2 = svc.submit(call2);
    Session sess1 = fut1.get(1, TimeUnit.SECONDS);
    Session sess2 = fut2.get(1, TimeUnit.SECONDS);
    assertThat("Sessions are different", sess1, not(sameInstance(sess2)));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) WebSocketScopeContext(org.eclipse.jetty.cdi.websocket.WebSocketScopeContext) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 3 with WebSocketScopeContext

use of org.eclipse.jetty.cdi.websocket.WebSocketScopeContext in project jetty.project by eclipse.

the class WebSocketScopeSessionTest method testSessionActivation.

@Test
public void testSessionActivation() throws Exception {
    ScopedInstance<WebSocketScopeContext> wsScopeBean = newInstance(WebSocketScopeContext.class);
    WebSocketScopeContext wsScope = wsScopeBean.instance;
    wsScope.create();
    try {
        // Scope 1
        wsScope.begin();
        BogusSession sess = new BogusSession("1");
        wsScope.setSession(sess);
        ScopedInstance<BogusSocket> sock1Bean = newInstance(BogusSocket.class);
        BogusSocket sock1 = sock1Bean.instance;
        assertThat("Socket 1 Session", sock1.getSession().toString(), is(sess.toString()));
        sock1Bean.destroy();
    } finally {
        wsScope.end();
    }
    wsScope.destroy();
    wsScopeBean.destroy();
}
Also used : WebSocketScopeContext(org.eclipse.jetty.cdi.websocket.WebSocketScopeContext) Test(org.junit.Test)

Example 4 with WebSocketScopeContext

use of org.eclipse.jetty.cdi.websocket.WebSocketScopeContext in project jetty.project by eclipse.

the class WebSocketScopeSessionTest method testMultiSession_Sequential.

@Test
public void testMultiSession_Sequential() throws Exception {
    ScopedInstance<WebSocketScopeContext> wsScope1Bean = newInstance(WebSocketScopeContext.class);
    WebSocketScopeContext wsScope1 = wsScope1Bean.instance;
    ScopedInstance<WebSocketScopeContext> wsScope2Bean = newInstance(WebSocketScopeContext.class);
    WebSocketScopeContext wsScope2 = wsScope2Bean.instance;
    wsScope1.create();
    try {
        // Scope 1
        wsScope1.begin();
        BogusSession sess = new BogusSession("1");
        wsScope1.setSession(sess);
        ScopedInstance<BogusSocket> sock1Bean = newInstance(BogusSocket.class);
        BogusSocket sock1 = sock1Bean.instance;
        assertThat("Socket 1 Session", sock1.getSession(), sameInstance((Session) sess));
        sock1Bean.destroy();
    } finally {
        wsScope1.end();
    }
    wsScope1.destroy();
    wsScope1Bean.destroy();
    wsScope2.create();
    try {
        // Scope 2
        wsScope2.begin();
        BogusSession sess = new BogusSession("2");
        wsScope2.setSession(sess);
        ScopedInstance<BogusSocket> sock2Bean = newInstance(BogusSocket.class);
        BogusSocket sock2 = sock2Bean.instance;
        assertThat("Socket 2 Session", sock2.getSession(), sameInstance((Session) sess));
        sock2Bean.destroy();
    } finally {
        wsScope2.end();
    }
    wsScope2.destroy();
    wsScope2Bean.destroy();
}
Also used : WebSocketScopeContext(org.eclipse.jetty.cdi.websocket.WebSocketScopeContext) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Aggregations

WebSocketScopeContext (org.eclipse.jetty.cdi.websocket.WebSocketScopeContext)4 Test (org.junit.Test)4 Session (org.eclipse.jetty.websocket.api.Session)2 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1