]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncRequestTest3.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 / misc / SyncRequestTest3.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncRequestTest3.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncRequestTest3.java
new file mode 100644 (file)
index 0000000..8a6de86
--- /dev/null
@@ -0,0 +1,92 @@
+/*******************************************************************************
+ * 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.misc;
+
+import java.util.HashSet;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Test;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Session;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.SyncListener;
+import org.simantics.db.request.Read;
+import org.simantics.db.testing.base.TestCommonPerf;
+import org.simantics.layer0.Layer0;
+
+public class SyncRequestTest3 extends TestCommonPerf {
+    
+    private static final int COUNT = 10000;
+
+    private static final int DEADLINE = 1000;
+    
+       @Test
+    public void test() throws Exception {
+
+        final Session session = getSession();
+        
+        final HashSet<Integer> check = new HashSet<Integer>();
+        for(int i=0;i<COUNT;i++) check.add(i);
+        
+        final Semaphore s = new Semaphore(0);
+        
+        long start = System.nanoTime();
+        
+        for(int i=0;i<COUNT;i++) {
+            
+            final int index = i;
+
+            session.asyncRequest(new Read<String>() {
+
+                @Override
+                public String perform(ReadGraph graph) throws DatabaseException {
+                    Layer0 l0 = Layer0.getInstance(graph);
+                    return graph.getPossibleRelatedValue(rl, l0.HasName);
+                }
+
+            }, new SyncListener<String>() {
+
+                @Override
+                public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
+                    throwable.printStackTrace();
+                }
+
+                @Override
+                public void execute(ReadGraph graph, String result) throws DatabaseException {
+                    synchronized(check) {
+                        check.remove(index);
+                        if(check.isEmpty()) s.release();
+                    }
+                }
+
+                @Override
+                public boolean isDisposed() {
+                    return false;
+                }
+
+            });
+
+        }
+        
+        boolean success = s.tryAcquire(DEADLINE, TimeUnit.MILLISECONDS);
+
+        long end = System.nanoTime();
+
+        String t = "SyncRequestTest3 finished in " + 1e-9*(double)(end-start) + " s."
+        + " Limit was < " + (double)DEADLINE / 1000.0 + " s.";
+        System.out.println(t);
+
+        if (!success)
+               fail(t);
+    }
+}