]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/external/ExternalRequestTest.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 / ExternalRequestTest.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.concurrent.Executors;
15 import java.util.concurrent.ScheduledExecutorService;
16 import java.util.concurrent.Semaphore;
17 import java.util.concurrent.TimeUnit;
18
19 import org.junit.Test;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Session;
22 import org.simantics.db.procedure.Listener;
23 import org.simantics.db.request.ExternalRead;
24 import org.simantics.db.testing.annotation.Fails;
25 import org.simantics.db.testing.base.ExistingDatabaseTest;
26
27 public class ExternalRequestTest extends ExistingDatabaseTest {
28         
29         private Error exception;
30         
31         int counter = 0;
32         
33         @Test
34         public void test() throws Exception {
35                 
36                 Session session = getSession();
37                 
38                 final ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
39
40                 final Semaphore s = new Semaphore(-9);
41                 
42                 session.syncRequest(new ExternalRead<String>() {
43
44             @Override
45             public void register(ReadGraph graph, final Listener<String> procedure) {
46
47                 procedure.execute("String" + (counter++));
48
49                 for(int i=1;i<10;i++) {
50                     exec.schedule(new Runnable() {
51     
52                         @Override
53                         public void run() {
54                             procedure.execute("String" + (counter++));
55                         }
56                         
57                     }, i*500, TimeUnit.MILLISECONDS);
58                 }
59                 
60             }
61             
62             @Override
63             public void unregistered() {
64             }
65                         
66                 }, new Listener<String>() {
67
68             @Override
69             public void exception(Throwable t) {
70             }
71
72             @Override
73             public void execute(String result) {
74                 s.release();
75             }
76
77             @Override
78             public boolean isDisposed() {
79                 return false;
80             }
81                     
82                 });
83                 
84                 s.acquire();
85                 
86                 if(exception != null) {
87                         fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through ");
88                 }
89                 
90         }
91
92 }