]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/AsyncTransactionTest.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 / AsyncTransactionTest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 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 org.junit.Test;
15 import org.simantics.db.AsyncReadGraph;
16 import org.simantics.db.Session;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.request.AsyncReadRequest;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.procedure.AsyncListener;
22 import org.simantics.db.procedure.AsyncProcedure;
23 import org.simantics.db.request.AsyncRead;
24 import org.simantics.db.testing.base.ExistingDatabaseTest;
25
26 /**
27  * Tests that read and write locks are implemented correctly.
28  *
29  * @author Hannu Niemistö
30  */
31 public class AsyncTransactionTest extends ExistingDatabaseTest {
32
33     int counter = 100;
34
35     public void perform(AsyncReadGraph graph) {
36         graph.asyncRequest(new AsyncRead<Object>() {
37
38             @Override
39             public void perform(AsyncReadGraph graph,
40                     AsyncProcedure<Object> procedure) {
41                 try {
42                     Thread.sleep(10);
43                 } catch (InterruptedException e) {
44                     e.printStackTrace();
45                 }
46                 procedure.execute(graph, null);
47             }
48
49             @Override
50                     public int threadHash() {
51                         return hashCode();
52                     }
53
54             @Override
55             public int getFlags() {
56                 return 0;
57             }
58
59         }, new AsyncListener<Object>() {
60
61             @Override
62             public void exception(AsyncReadGraph graph,
63                     Throwable throwable) {
64                 throwable.printStackTrace();
65             }
66
67             @Override
68             public void execute(AsyncReadGraph graph, Object result) {
69                 try {
70                     Thread.sleep(10);
71                 } catch (InterruptedException e) {
72                     e.printStackTrace();
73                 }
74                 --counter;
75                 if(counter > 0)
76                     perform(graph);
77             }
78
79             @Override
80             public boolean isDisposed() {
81                 return false;
82             }
83
84         });
85     }
86
87         @Test
88     public void testAsyncTransactions() throws DatabaseException {
89         Session session = getSession();
90
91         session.asyncRequest(new AsyncReadRequest() {
92
93             @Override
94             public void run(AsyncReadGraph graph) {
95                 AsyncTransactionTest.this.perform(graph);
96             }
97
98         });
99         session.asyncRequest(new WriteRequest() {
100
101             @Override
102             public void perform(WriteGraph graph) throws DatabaseException {
103                 if(counter > 0)
104                     fail("Finished before read request was completed.");
105             }
106
107         });
108
109         try { // This should be done by the framework, but isn't.
110             int timeout = 101;
111             while (counter > 0 && --timeout > 0)
112                 Thread.sleep(100);
113         } catch (InterruptedException e) {
114             e.printStackTrace();
115         }
116     }
117
118 }