]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncAsyncSyncTest2.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 / SyncAsyncSyncTest2.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.Listener;
26 import org.simantics.db.procedure.Procedure;
27 import org.simantics.db.request.AsyncRead;
28 import org.simantics.db.request.Read;
29 import org.simantics.db.testing.base.ExistingDatabaseTest;
30 import org.simantics.layer0.Layer0;
31
32 public class SyncAsyncSyncTest2 extends ExistingDatabaseTest {
33
34         @Test
35     public void test() throws Exception {
36                 
37                 Session session = getSession();
38                 
39                 final ConcurrentSkipListSet<String> names = new ConcurrentSkipListSet<String>();
40                 final ArrayList<Resource> resources = new ArrayList<Resource>();
41                 final Resource root = SyncAsyncSyncUtils.createNames(session, names, resources);
42
43                 session.syncRequest(new TestAsyncReadRequest() {
44
45             @Override
46             public void run(AsyncReadGraph graph) throws Throwable {
47                 
48                 final Layer0 b = Layer0.getInstance(graph);
49                 Collection<Resource> rs = graph.getObjects(root, b.ConsistsOf);
50                 int index = 0;
51                 for(final Resource r : rs) {
52
53                     final int i = index++;
54                     
55                     graph.asyncRequest(new AsyncRead<String>() {
56
57                         @Override
58                         public void perform(AsyncReadGraph graph, AsyncProcedure<String> procedure) {
59                             
60                             graph.asyncRequest(new Read<String>() {
61
62                                 @Override
63                                 public String perform(ReadGraph graph) throws DatabaseException {
64                                     return graph.getRelatedValue(r, b.HasName);
65                                 }
66                                 
67                                 @Override
68                                 public String toString() {
69                                     return "Read" + i;
70                                 }
71                                 
72                             }, procedure);
73                             
74                         }
75
76                         @Override
77                             public int threadHash() {
78                                 return hashCode();
79                             }
80
81                         @Override
82                         public int getFlags() {
83                             return 0;
84                         }
85
86                     }, new Procedure<String>() {
87
88                         @Override
89                         public void exception(Throwable t) {
90                             t.printStackTrace();
91                         }
92
93                         @Override
94                         public void execute(String result) {
95                             names.remove(result);
96                         }
97                         
98                     });
99                     
100                 }
101                 
102             }
103                     
104                 }, new Listener<Object>() {
105
106             @Override
107             public void exception(Throwable t) {
108                 t.printStackTrace();
109             }
110
111             @Override
112             public void execute(Object result) {
113             }
114
115             @Override
116             public boolean isDisposed() {
117                 return false;
118             }
119             
120         });
121                 
122                 assert(names.isEmpty());
123                 
124         }
125
126 }