]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncRequestTest2.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 / SyncRequestTest2.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.SyncListener;
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 SyncRequestTest2 extends TestCommonPerf {
31     
32     private static final int COUNT = 10000;
33
34     private static final int DEADLINE = 1000;
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                 
55                 for(int i=0;i<COUNT;i++) {
56                     
57                     final int index = i;
58                     
59                     graph.forPossibleRelatedValue(rl, b.HasName, new SyncListener<String>() {
60
61                         @Override
62                         public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
63                             throwable.printStackTrace();
64                         }
65
66                         @Override
67                         public void execute(ReadGraph graph, String result) throws DatabaseException {
68                             synchronized(check) {
69                                 check.remove(index);
70                                 if(check.isEmpty()) s.release();
71                             }
72                         }
73
74                         @Override
75                         public boolean isDisposed() {
76                             return false;
77                         }
78                         
79                     });
80
81                 }
82                 
83             }
84
85             @Override
86                     public int threadHash() {
87                         return hashCode();
88                     }
89
90             @Override
91             public int getFlags() {
92                 return 0;
93             }
94
95         });
96         
97         boolean success = s.tryAcquire(DEADLINE, TimeUnit.MILLISECONDS);
98
99         long end = System.nanoTime();
100
101         System.out.println("success or failure in " + 1e-9*(double)(end-start) + " s.");
102         
103         assert(success);
104         
105         
106     }
107 }