]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.api.request.misc;
13
14 import java.util.HashSet;
15 import java.util.concurrent.Semaphore;
16 import java.util.concurrent.TimeUnit;
17
18 import org.junit.Test;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Session;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.procedure.SyncListener;
23 import org.simantics.db.request.Read;
24 import org.simantics.db.testing.base.TestCommonPerf;
25 import org.simantics.layer0.Layer0;
26
27 public class SyncRequestTest3 extends TestCommonPerf {
28     
29     private static final int COUNT = 10000;
30
31     private static final int DEADLINE = 1000;
32     
33         @Test
34     public void test() throws Exception {
35
36         final Session session = getSession();
37         
38         final HashSet<Integer> check = new HashSet<Integer>();
39         for(int i=0;i<COUNT;i++) check.add(i);
40         
41         final Semaphore s = new Semaphore(0);
42         
43         long start = System.nanoTime();
44         
45         for(int i=0;i<COUNT;i++) {
46             
47             final int index = i;
48
49             session.asyncRequest(new Read<String>() {
50
51                 @Override
52                 public String perform(ReadGraph graph) throws DatabaseException {
53                     Layer0 l0 = Layer0.getInstance(graph);
54                     return graph.getPossibleRelatedValue(rl, l0.HasName);
55                 }
56
57             }, new SyncListener<String>() {
58
59                 @Override
60                 public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
61                     throwable.printStackTrace();
62                 }
63
64                 @Override
65                 public void execute(ReadGraph graph, String result) throws DatabaseException {
66                     synchronized(check) {
67                         check.remove(index);
68                         if(check.isEmpty()) s.release();
69                     }
70                 }
71
72                 @Override
73                 public boolean isDisposed() {
74                     return false;
75                 }
76
77             });
78
79         }
80         
81         boolean success = s.tryAcquire(DEADLINE, TimeUnit.MILLISECONDS);
82
83         long end = System.nanoTime();
84
85         String t = "SyncRequestTest3 finished in " + 1e-9*(double)(end-start) + " s."
86         + " Limit was < " + (double)DEADLINE / 1000.0 + " s.";
87         System.out.println(t);
88
89         if (!success)
90                 fail(t);
91     }
92 }