1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.api.request.misc;
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;
27 * Tests that read and write locks are implemented correctly.
29 * @author Hannu Niemistö
31 public class AsyncTransactionTest extends ExistingDatabaseTest {
35 public void perform(AsyncReadGraph graph) {
36 graph.asyncRequest(new AsyncRead<Object>() {
39 public void perform(AsyncReadGraph graph,
40 AsyncProcedure<Object> procedure) {
43 } catch (InterruptedException e) {
46 procedure.execute(graph, null);
50 public int threadHash() {
55 public int getFlags() {
59 }, new AsyncListener<Object>() {
62 public void exception(AsyncReadGraph graph,
63 Throwable throwable) {
64 throwable.printStackTrace();
68 public void execute(AsyncReadGraph graph, Object result) {
71 } catch (InterruptedException e) {
80 public boolean isDisposed() {
88 public void testAsyncTransactions() throws DatabaseException {
89 Session session = getSession();
91 session.asyncRequest(new AsyncReadRequest() {
94 public void run(AsyncReadGraph graph) {
95 AsyncTransactionTest.this.perform(graph);
99 session.asyncRequest(new WriteRequest() {
102 public void perform(WriteGraph graph) throws DatabaseException {
104 fail("Finished before read request was completed.");
109 try { // This should be done by the framework, but isn't.
111 while (counter > 0 && --timeout > 0)
113 } catch (InterruptedException e) {