1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.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;
28 * Tests that read and write locks are implemented correctly.
30 * @author Hannu Niemistö
32 public class AsyncTransactionTest extends ExistingDatabaseTest {
36 public void perform(AsyncReadGraph graph) {
37 graph.asyncRequest(new AsyncRead<Object>() {
40 public void perform(AsyncReadGraph graph,
41 AsyncProcedure<Object> procedure) {
44 } catch (InterruptedException e) {
47 procedure.execute(graph, null);
51 public int threadHash() {
56 public int getFlags() {
60 }, new AsyncListener<Object>() {
63 public void exception(AsyncReadGraph graph,
64 Throwable throwable) {
65 throwable.printStackTrace();
69 public void execute(AsyncReadGraph graph, Object result) {
72 } catch (InterruptedException e) {
81 public boolean isDisposed() {
89 public void testAsyncTransactions() throws DatabaseException {
90 Session session = getSession();
92 session.asyncRequest(new Read<Object>() {
95 public Object perform(ReadGraph graph) throws DatabaseException {
96 AsyncTransactionTest.this.perform(graph);
101 session.asyncRequest(new WriteRequest() {
104 public void perform(WriteGraph graph) throws DatabaseException {
106 fail("Finished before read request was completed.");
111 try { // This should be done by the framework, but isn't.
113 while (counter > 0 && --timeout > 0)
115 } catch (InterruptedException e) {