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