[seam-commits] Seam SVN: r14951 - branches/community/Seam_2_3/seam-integration-tests/src/test/java/org/jboss/seam/test/integration.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue Jun 26 03:46:14 EDT 2012


Author: maschmid
Date: 2012-06-26 03:46:14 -0400 (Tue, 26 Jun 2012)
New Revision: 14951

Modified:
   branches/community/Seam_2_3/seam-integration-tests/src/test/java/org/jboss/seam/test/integration/ConcurrentFactoryTest.java
Log:
fix ConcurrentFactoryTest


Modified: branches/community/Seam_2_3/seam-integration-tests/src/test/java/org/jboss/seam/test/integration/ConcurrentFactoryTest.java
===================================================================
--- branches/community/Seam_2_3/seam-integration-tests/src/test/java/org/jboss/seam/test/integration/ConcurrentFactoryTest.java	2012-06-25 07:37:51 UTC (rev 14950)
+++ branches/community/Seam_2_3/seam-integration-tests/src/test/java/org/jboss/seam/test/integration/ConcurrentFactoryTest.java	2012-06-26 07:46:14 UTC (rev 14951)
@@ -2,75 +2,121 @@
 
 import static org.jboss.seam.ScopeType.APPLICATION;
 
-import org.jboss.arquillian.container.test.api.Deployment;
-import org.jboss.arquillian.container.test.api.OverProtocol;
-import org.jboss.arquillian.junit.Arquillian;
+import java.io.Serializable;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.jboss.seam.annotations.Create;
 import org.jboss.seam.annotations.Factory;
 import org.jboss.seam.annotations.In;
 import org.jboss.seam.annotations.Name;
-import org.jboss.seam.contexts.ServletLifecycle;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.core.Expressions;
 import org.jboss.seam.mock.JUnitSeamTest;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.After;
-import org.junit.runner.RunWith;
 
+// JBSEAM-4669
 public class ConcurrentFactoryTest 
     extends JUnitSeamTest 
 {
-	
-	// TODO: Implement a different way to run concurrent test for junit
-    // @Test(threadPoolSize = 2, invocationCount = 2)
-	@Test
-	@Ignore
+    private volatile boolean exceptionOccured = false;
+    static AtomicInteger testSequence = new AtomicInteger(0);
+
+    private void concurrentFactoryCallTest() throws Exception {
+       new ComponentTest() {
+          @Override
+          protected void testComponents() throws Exception {
+             int myTestSequence;
+             myTestSequence = testSequence.getAndIncrement();
+             if (myTestSequence == 0) {
+                assert "TestString".equals(getValue("#{concurrentFactoryTest.LockHoldingComponent.string}"));
+             } else {
+                try {
+                   Thread.sleep(500);
+                } catch (InterruptedException e) {
+                   e.printStackTrace();
+                }
+                assert "TestString".equals(getValue("#{concurrentFactoryTest.dependentString}"));
+             }
+             System.out.println(myTestSequence);
+
+          }
+      }.run();
+    }
+    
+    private class ConcurrentFactoryCallTestThread extends Thread
+    {
+        public void run() {
+            try
+            {
+                ConcurrentFactoryTest.this.concurrentFactoryCallTest();
+            }
+            catch (Throwable e)
+            {
+                e.printStackTrace();
+                ConcurrentFactoryTest.this.exceptionOccured = true;
+            }
+        };
+    }
+   
+    @Test(timeout=10000)
     public void concurrentFactoryCall() 
         throws Exception 
     {
-        new ComponentTest() {
-            @Override
-            protected void testComponents() throws Exception {
-                assert "slowly created String".equals(getValue("#{concurrentFactoryTest.component.injectedString}"));
-            }
-        }.run();
+       Thread thread1 = new ConcurrentFactoryCallTestThread();
+       Thread thread2 = new ConcurrentFactoryCallTestThread();
+
+       thread1.start();
+       thread2.start();
+    
+       thread1.join();
+       thread2.join();
+       
+       assert !exceptionOccured;
     }
     
-    @After
-    @Override
-    public void end()
-    {
-       if (session != null) {
-          // Because we run in threads. Only the first thread that finishes ends the session.
-          ServletLifecycle.endSession(session);
+    @Name("concurrentFactoryTest.LockHoldingComponent")
+    @Scope(APPLICATION)
+    static public class LockHoldingComponent implements Serializable {
+       @In(value = "concurrentFactoryTest.slowlyCreatedComponent", create = true) SlowlyCreatedComponent slowlyCreatedComponent;
+       
+       public String getString() {
+          return (String) Expressions.instance().createValueExpression("#{concurrentFactoryTest.plainFactoryGeneratedString}").getValue();
        }
-       session = null;
     }
     
-    @Name("concurrentFactoryTest.component")
-    static public class Component {
-       @In(value = "concurrentFactoryTest.slowlyCreatedString") String injectedString;
-       
-       public String getInjectedString() {
-          return injectedString;
+    @Name("concurrentFactoryTest.slowlyCreatedComponent")
+    static public class SlowlyCreatedComponent {
+       @Create
+       public void slowlyCreate() {
+          try {
+             Thread.sleep(1000);
+          } catch (InterruptedException e) {
+             e.printStackTrace();
+          }
        }
     }
+      
+    @Name("concurrentFactoryTest.dependentFactory")
+    static public class DependentFactory {
+        @Factory(value = "concurrentFactoryTest.dependentString", scope = APPLICATION, autoCreate = true)
+        public String createString() {
+           return (String) Expressions.instance().createValueExpression("#{concurrentFactoryTest.LockHoldingComponent.string}").getValue();
+        }
+    }
     
-    @Name("concurrentFactoryTest.SlowFactory")
-    static public class SlowFactory {
-        @Factory(value = "concurrentFactoryTest.slowlyCreatedString", scope = APPLICATION, autoCreate = true)
-        public String slowlyCreateString() {
-            try
-            {
-               Thread.sleep(1000);
-               return "slowly created String";
-            }
-            catch (InterruptedException e)
-            {
-               e.printStackTrace();
-               return null;
-            }
-        }        
+    @Name("concurrentFactoryTest.plainFactory")
+    static public class PlainFactory {
+        @Factory(value = "concurrentFactoryTest.plainFactoryGeneratedString", scope = APPLICATION, autoCreate = true)
+        public String createPlainString() {
+           return "TestString";
+        }
     }
     
-
-
+    @After
+    @Override
+    public void end()
+    {
+       // don't attempt to endSession, as it will block in the deadlocked org.jboss.seam.Component.getInstanceFromFactory lock
+    }
 }



More information about the seam-commits mailing list