]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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.AsyncReadGraph;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Session;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.procedure.AsyncProcedure;
24 import org.simantics.db.procedure.SyncProcedure;
25 import org.simantics.db.request.AsyncRead;
26 import org.simantics.db.testing.annotation.Fails;
27 import org.simantics.db.testing.base.TestCommonPerf;
28 import org.simantics.layer0.Layer0;
29
30 public class SyncRequestTest extends TestCommonPerf {
31     
32     private static final int COUNT = 1000000;
33
34     private static final int DEADLINE = 10000;
35     
36         @Test(timeout=5000)
37         @Fails
38     public void test() throws Exception {
39
40         final Session session = getSession();
41         final Layer0 b = Layer0.getInstance(session);
42         
43         final HashSet<Integer> check = new HashSet<Integer>();
44         for(int i=0;i<COUNT;i++) check.add(i);
45         
46         final Semaphore s = new Semaphore(0);
47         
48         long start = System.nanoTime();
49         
50         session.asyncRequest(new AsyncRead<String>() {
51
52             @Override
53             public void perform(AsyncReadGraph graph, AsyncProcedure<String> procedure) {
54                 for(int i=0;i<COUNT;i++) {
55                     
56                     final int index = i;
57                     
58                     graph.forPossibleRelatedValue(rl, b.HasName, new SyncProcedure<String>() {
59
60                         @Override
61                         public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
62                             throwable.printStackTrace();
63                         }
64
65                         @Override
66                         public void execute(ReadGraph graph, String result) throws DatabaseException {
67                             synchronized(check) {
68                                 check.remove(index);
69                                 if(check.isEmpty()) s.release();
70                             }
71                         }
72                         
73                     });
74
75                 }
76                 
77             }
78
79             @Override
80                     public int threadHash() {
81                         return hashCode();
82                     }
83
84             @Override
85             public int getFlags() {
86                 return 0;
87             }
88
89         });
90         
91         boolean success = s.tryAcquire(DEADLINE, TimeUnit.MILLISECONDS);
92         assert(success);
93         
94         long end = System.nanoTime();
95         if (DEBUG)
96             System.out.println("success in " + 1e-9*(double)(end-start) + " s.");
97         
98     }
99 }