]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncAsyncSyncTest.java
Merge "Multiple reader thread support for db client"
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / request / misc / SyncAsyncSyncTest.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.ArrayList;
15 import java.util.Collection;
16 import java.util.concurrent.ConcurrentSkipListSet;
17
18 import org.junit.Test;
19 import org.simantics.db.AsyncReadGraph;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Session;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.procedure.AsyncProcedure;
25 import org.simantics.db.procedure.Procedure;
26 import org.simantics.db.request.AsyncRead;
27 import org.simantics.db.request.Read;
28 import org.simantics.db.testing.base.ExistingDatabaseTest;
29 import org.simantics.layer0.Layer0;
30
31 public class SyncAsyncSyncTest extends ExistingDatabaseTest {
32         
33         @Test
34     public void test() throws Exception {
35                 
36                 Session session = getSession();
37                 
38                 final ConcurrentSkipListSet<String> names = new ConcurrentSkipListSet<String>();
39                 final ArrayList<Resource> resources = new ArrayList<Resource>();
40                 final Resource root = SyncAsyncSyncUtils.createNames(session, names, resources);
41
42                 session.syncRequest(new TestAsyncReadRequest() {
43
44             @Override
45             public void run(AsyncReadGraph graph) throws Throwable {
46                 
47                 final Layer0 b = Layer0.getInstance(graph);
48                 Collection<Resource> rs = graph.getObjects(root, b.ConsistsOf);
49                 for(final Resource r : rs) {
50
51                     graph.asyncRequest(new AsyncRead<String>() {
52
53                         @Override
54                         public void perform(AsyncReadGraph graph, AsyncProcedure<String> procedure) {
55                             
56                             graph.asyncRequest(new Read<String>() {
57
58                                 @Override
59                                 public String perform(ReadGraph graph) throws DatabaseException {
60                                     return graph.getRelatedValue(r, b.HasName);
61                                 }
62                                 
63                             }, procedure);
64                             
65                         }
66
67                         @Override
68                             public int threadHash() {
69                                 return hashCode();
70                             }
71
72                         @Override
73                         public int getFlags() {
74                             return 0;
75                         }
76       
77                     }, new Procedure<String>() {
78
79                         @Override
80                         public void exception(Throwable t) {
81                             t.printStackTrace();
82                         }
83
84                         @Override
85                         public void execute(String result) {
86                             names.remove(result);
87                         }
88                         
89                     });
90                     
91                 }
92                 
93             }
94                     
95                 });
96                 
97                 assert(names.isEmpty());
98                 
99         }
100
101 }