]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/external/ExternalRequestTest4.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 / external / ExternalRequestTest4.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.external;
13
14 import java.util.HashSet;
15 import java.util.concurrent.Semaphore;
16
17 import org.junit.Test;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Session;
20 import org.simantics.db.common.request.SingletonPrimitiveRead;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.procedure.Listener;
23 import org.simantics.db.request.Read;
24 import org.simantics.db.testing.annotation.Fails;
25 import org.simantics.db.testing.base.ExistingDatabaseTest;
26 import org.simantics.layer0.Layer0;
27
28 public class ExternalRequestTest4 extends ExistingDatabaseTest {
29         
30         private Error exception;
31         
32         boolean listenerAlive = true;
33
34         int counter = 0;        
35         HashSet<Listener<String>> listeners = new HashSet<Listener<String>>();
36         
37     class Primitive extends SingletonPrimitiveRead<String> {
38
39         @Override
40         public void register(ReadGraph graph, Listener<String> procedure) {
41
42             listeners.add(procedure);
43             procedure.execute("Result" + counter);
44             
45         }
46         
47     }
48
49     public void fireNewPrimitive() {
50         counter++;
51         for(Listener<String> listener : listeners) listener.execute("Result" + counter);
52     }
53     
54         @Test
55         @Fails
56         public void test() throws Exception {
57                 
58                 Session session = getSession();
59                 
60                 final Semaphore s1 = new Semaphore(-6);
61         final Semaphore s2 = new Semaphore(-6);
62                 
63                 session.syncRequest(new Read<String>() {
64
65             @Override
66             public String perform(ReadGraph graph) throws DatabaseException {
67                 
68                 Layer0 b = Layer0.getInstance(graph);
69                 String base = graph.getRelatedValue(graph.getRootLibrary(), b.HasName);
70                 String appendix = graph.syncRequest(new Primitive());
71                 return base + appendix;
72                 
73             }
74                         
75                 }, new Listener<String>() {
76
77             @Override
78             public void exception(Throwable t) {
79             }
80
81             @Override
82             public void execute(String result) {
83                 s1.release();
84             }
85
86             @Override
87             public boolean isDisposed() {
88                 return !listenerAlive;
89             }
90                     
91                 });
92
93                 fireNewPrimitive();
94         fireNewPrimitive();
95         fireNewPrimitive();
96
97         Thread.sleep(500);
98
99         session.syncRequest(new Read<String>() {
100
101             @Override
102             public String perform(ReadGraph graph) throws DatabaseException {
103                 
104                 Layer0 b = Layer0.getInstance(graph);
105                 String base = graph.getRelatedValue(graph.getRootLibrary(), b.HasName);
106                 String appendix = graph.syncRequest(new Primitive());
107                 return base + appendix;
108                 
109             }
110             
111         }, new Listener<String>() {
112
113             @Override
114             public void exception(Throwable t) {
115             }
116
117             @Override
118             public void execute(String result) {
119                 s2.release();
120             }
121
122             @Override
123             public boolean isDisposed() {
124                 return false;
125             }
126             
127         });
128
129         fireNewPrimitive();
130         fireNewPrimitive();
131         fireNewPrimitive();
132
133         Thread.sleep(500);
134         listenerAlive = false;
135
136         fireNewPrimitive();
137         fireNewPrimitive();
138         fireNewPrimitive();
139         
140         s1.acquire();
141         s2.acquire();
142         
143                 if(exception != null) {
144                         fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through ");
145                 }
146                 
147         }
148
149 }