deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject git commit: DELTASPIKE-745 add TCK test for parallel threads
Date Tue, 21 Oct 2014 18:11:53 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 2708723b1 -> 3b0abce3c


DELTASPIKE-745 add TCK test for parallel threads


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/3b0abce3
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/3b0abce3
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/3b0abce3

Branch: refs/heads/master
Commit: 3b0abce3ca9939dc953877cb06942bc716e82cfd
Parents: 2708723
Author: Mark Struberg <struberg@apache.org>
Authored: Tue Oct 21 20:11:16 2014 +0200
Committer: Mark Struberg <struberg@apache.org>
Committed: Tue Oct 21 20:11:16 2014 +0200

----------------------------------------------------------------------
 .../cdise/tck/ContainerCtrlTckTest.java         | 82 ++++++++++++++++++++
 1 file changed, 82 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/3b0abce3/deltaspike/cdictrl/tck/src/main/java/org/apache/deltaspike/cdise/tck/ContainerCtrlTckTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/cdictrl/tck/src/main/java/org/apache/deltaspike/cdise/tck/ContainerCtrlTckTest.java
b/deltaspike/cdictrl/tck/src/main/java/org/apache/deltaspike/cdise/tck/ContainerCtrlTckTest.java
index e719210..388ff2c 100644
--- a/deltaspike/cdictrl/tck/src/main/java/org/apache/deltaspike/cdise/tck/ContainerCtrlTckTest.java
+++ b/deltaspike/cdictrl/tck/src/main/java/org/apache/deltaspike/cdise/tck/ContainerCtrlTckTest.java
@@ -20,13 +20,19 @@ package org.apache.deltaspike.cdise.tck;
 
 
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.enterprise.context.ContextNotActiveException;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.SessionScoped;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 
 import org.apache.deltaspike.cdise.api.CdiContainer;
 import org.apache.deltaspike.cdise.api.CdiContainerLoader;
+import org.apache.deltaspike.cdise.api.ContextControl;
 import org.apache.deltaspike.cdise.tck.beans.Car;
 import org.apache.deltaspike.cdise.tck.beans.CarRepair;
 import org.apache.deltaspike.cdise.tck.beans.TestUser;
@@ -43,6 +49,8 @@ import org.junit.Test;
  */
 public class ContainerCtrlTckTest
 {
+    private static final Logger log = Logger.getLogger(ContainerCtrlTckTest.class.getName());
+    private static final int NUM_THREADS = 100;
 
     @Rule
     public VersionControlRule versionControlRule = new VersionControlRule();
@@ -72,6 +80,80 @@ public class ContainerCtrlTckTest
     }
 
     @Test
+    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);
+
+        Runnable runnable = new Runnable()
+        {
+            @Override
+            public void run()
+            {
+                ContextControl contextControl = cc.getContextControl();
+                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);
+
+                try
+                {
+                    for (int i = 0; i < 100000; i++)
+                    {
+                        // we need the threads doing something ;)
+                        Assert.assertNotNull(carRepair.getCar());
+                        Assert.assertNotNull(carRepair.getCar().getUser());
+                        Assert.assertNotNull(carRepair.getCar().getUser().getName());
+                    }
+                }
+                catch (Exception e)
+                {
+                    log.log(Level.SEVERE, "An exception happened on a new worker thread",
e);
+                    numErrors.incrementAndGet();
+                }
+                contextControl.stopContext(RequestScoped.class);
+                contextControl.stopContext(SessionScoped.class);
+            }
+        };
+
+
+        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();
+    }
+
+    @Test
     public void testSimpleContainerBoot()
     {
         CdiContainer cc = CdiContainerLoader.getCdiContainer();


Mime
View raw message