]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/external/ExternalRequestTest4.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / request / external / ExternalRequestTest4.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/external/ExternalRequestTest4.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/external/ExternalRequestTest4.java
new file mode 100644 (file)
index 0000000..f378c56
--- /dev/null
@@ -0,0 +1,149 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.api.request.external;
+
+import java.util.HashSet;
+import java.util.concurrent.Semaphore;
+
+import org.junit.Test;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Session;
+import org.simantics.db.common.request.SingletonPrimitiveRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.Listener;
+import org.simantics.db.request.Read;
+import org.simantics.db.testing.annotation.Fails;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.layer0.Layer0;
+
+public class ExternalRequestTest4 extends ExistingDatabaseTest {
+       
+       private Error exception;
+       
+       boolean listenerAlive = true;
+
+       int counter = 0;        
+       HashSet<Listener<String>> listeners = new HashSet<Listener<String>>();
+       
+    class Primitive extends SingletonPrimitiveRead<String> {
+
+        @Override
+        public void register(ReadGraph graph, Listener<String> procedure) {
+
+            listeners.add(procedure);
+            procedure.execute("Result" + counter);
+            
+        }
+        
+    }
+
+    public void fireNewPrimitive() {
+        counter++;
+        for(Listener<String> listener : listeners) listener.execute("Result" + counter);
+    }
+    
+       @Test
+       @Fails
+       public void test() throws Exception {
+               
+               Session session = getSession();
+               
+               final Semaphore s1 = new Semaphore(-6);
+        final Semaphore s2 = new Semaphore(-6);
+               
+               session.syncRequest(new Read<String>() {
+
+            @Override
+            public String perform(ReadGraph graph) throws DatabaseException {
+                
+                Layer0 b = Layer0.getInstance(graph);
+                String base = graph.getRelatedValue(graph.getRootLibrary(), b.HasName);
+                String appendix = graph.syncRequest(new Primitive());
+                return base + appendix;
+                
+            }
+                       
+               }, new Listener<String>() {
+
+            @Override
+            public void exception(Throwable t) {
+            }
+
+            @Override
+            public void execute(String result) {
+                s1.release();
+            }
+
+            @Override
+            public boolean isDisposed() {
+                return !listenerAlive;
+            }
+                   
+               });
+
+               fireNewPrimitive();
+        fireNewPrimitive();
+        fireNewPrimitive();
+
+        Thread.sleep(500);
+
+        session.syncRequest(new Read<String>() {
+
+            @Override
+            public String perform(ReadGraph graph) throws DatabaseException {
+                
+                Layer0 b = Layer0.getInstance(graph);
+                String base = graph.getRelatedValue(graph.getRootLibrary(), b.HasName);
+                String appendix = graph.syncRequest(new Primitive());
+                return base + appendix;
+                
+            }
+            
+        }, new Listener<String>() {
+
+            @Override
+            public void exception(Throwable t) {
+            }
+
+            @Override
+            public void execute(String result) {
+                s2.release();
+            }
+
+            @Override
+            public boolean isDisposed() {
+                return false;
+            }
+            
+        });
+
+        fireNewPrimitive();
+        fireNewPrimitive();
+        fireNewPrimitive();
+
+        Thread.sleep(500);
+        listenerAlive = false;
+
+        fireNewPrimitive();
+        fireNewPrimitive();
+        fireNewPrimitive();
+        
+        s1.acquire();
+        s2.acquire();
+        
+               if(exception != null) {
+                       fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through ");
+               }
+               
+       }
+
+}