public void testParallelThreadExecution()

in deltaspike/cdictrl/tck/src/main/java/org/apache/deltaspike/cdise/tck/ContainerCtrlTckTest.java [83:154]


    public void testParallelThreadExecution() throws Exception
    {
        final CdiContainer cc = CdiContainerLoader.getCdiContainer();
        Assert.assertNotNull(cc);

        cc.boot();
        cc.getContextControl().startContexts();

        final BeanManager bm = cc.getBeanManager();
        Assert.assertNotNull(bm);

        final AtomicInteger numErrors = new AtomicInteger(0);
        final ContextControl contextControl = cc.getContextControl();

        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    contextControl.startContext(SessionScoped.class);
                    contextControl.startContext(RequestScoped.class);


                    Set<Bean<?>> beans = bm.getBeans(CarRepair.class);
                    Bean<?> bean = bm.resolve(beans);

                    CarRepair carRepair = (CarRepair)
                            bm.getReference(bean, CarRepair.class, bm.createCreationalContext(bean));
                    Assert.assertNotNull(carRepair);

                    for (int i = 0; i < 100000; i++)
                    {
                        // we need the threads doing something ;)
                        Assert.assertNotNull(carRepair.getCar());
                        Assert.assertNotNull(carRepair.getCar().getUser());
                        Assert.assertNull(carRepair.getCar().getUser().getName());
                    }
                    contextControl.stopContext(RequestScoped.class);
                    contextControl.stopContext(SessionScoped.class);
                }
                catch (Throwable e)
                {
                    log.log(Level.SEVERE, "An exception happened on a new worker thread", e);
                    numErrors.incrementAndGet();
                }
            }
        };


        Thread[] threads = new Thread[NUM_THREADS];
        for (int i = 0 ; i < NUM_THREADS; i++)
        {
            threads[i] = new Thread(runnable);
        }

        for (int i = 0 ; i < NUM_THREADS; i++)
        {
            threads[i].start();
        }

        for (int i = 0 ; i < NUM_THREADS; i++)
        {
            threads[i].join();
        }

        Assert.assertEquals("An error happened while executing parallel threads", 0, numErrors.get());


        cc.shutdown();
    }