--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.api.request.misc;
+
+import org.junit.Test;
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.AsyncListener;
+import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.db.request.AsyncRead;
+import org.simantics.db.request.Read;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+
+/**
+ * Tests that read and write locks are implemented correctly.
+ *
+ * @author Hannu Niemistö
+ */
+public class AsyncTransactionTest extends ExistingDatabaseTest {
+
+ int counter = 100;
+
+ public void perform(AsyncReadGraph graph) {
+ graph.asyncRequest(new AsyncRead<Object>() {
+
+ @Override
+ public void perform(AsyncReadGraph graph,
+ AsyncProcedure<Object> procedure) {
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ procedure.execute(graph, null);
+ }
+
+ @Override
+ public int threadHash() {
+ return hashCode();
+ }
+
+ @Override
+ public int getFlags() {
+ return 0;
+ }
+
+ }, new AsyncListener<Object>() {
+
+ @Override
+ public void exception(AsyncReadGraph graph,
+ Throwable throwable) {
+ throwable.printStackTrace();
+ }
+
+ @Override
+ public void execute(AsyncReadGraph graph, Object result) {
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ --counter;
+ if(counter > 0)
+ perform(graph);
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return false;
+ }
+
+ });
+ }
+
+ @Test
+ public void testAsyncTransactions() throws DatabaseException {
+ Session session = getSession();
+
+ session.asyncRequest(new Read<Object>() {
+
+ @Override
+ public Object perform(ReadGraph graph) throws DatabaseException {
+ AsyncTransactionTest.this.perform(graph);
+ return null;
+ }
+
+ });
+ session.asyncRequest(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ if(counter > 0)
+ fail("Finished before read request was completed.");
+ }
+
+ });
+
+ try { // This should be done by the framework, but isn't.
+ int timeout = 101;
+ while (counter > 0 && --timeout > 0)
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+}