]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncRequestTest.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 / SyncRequestTest.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncRequestTest.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncRequestTest.java
new file mode 100644 (file)
index 0000000..c16604d
--- /dev/null
@@ -0,0 +1,99 @@
+/*******************************************************************************
+ * 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.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Session;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.db.procedure.SyncProcedure;
+import org.simantics.db.request.AsyncRead;
+import org.simantics.db.testing.annotation.Fails;
+import org.simantics.db.testing.base.TestCommonPerf;
+import org.simantics.layer0.Layer0;
+
+public class SyncRequestTest extends TestCommonPerf {
+    
+    private static final int COUNT = 1000000;
+
+    private static final int DEADLINE = 10000;
+    
+       @Test(timeout=5000)
+       @Fails
+    public void test() throws Exception {
+
+        final Session session = getSession();
+        final Layer0 b = Layer0.getInstance(session);
+        
+        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();
+        
+        session.asyncRequest(new AsyncRead<String>() {
+
+            @Override
+            public void perform(AsyncReadGraph graph, AsyncProcedure<String> procedure) {
+                for(int i=0;i<COUNT;i++) {
+                    
+                    final int index = i;
+                    
+                    graph.forPossibleRelatedValue(rl, b.HasName, new SyncProcedure<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 int threadHash() {
+                       return hashCode();
+                   }
+
+            @Override
+            public int getFlags() {
+                return 0;
+            }
+
+        });
+        
+        boolean success = s.tryAcquire(DEADLINE, TimeUnit.MILLISECONDS);
+        assert(success);
+        
+        long end = System.nanoTime();
+        if (DEBUG)
+            System.out.println("success in " + 1e-9*(double)(end-start) + " s.");
+        
+    }
+}