--- /dev/null
+package org.simantics.db.tests.regression.bugs;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.Test;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.Listener;
+import org.simantics.db.service.ClusterControl;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.db.testing.common.TestBase;
+
+public class SimanticsBug1893Test1 extends ExistingDatabaseTest {
+ static int THREAD_COUNT = 2;
+ Session session;
+ Resource testRoot;
+ ClusterControl clusterControl;
+ TestThread[] threads = new TestThread[THREAD_COUNT];
+ AtomicInteger exitCount = new AtomicInteger(0);
+ @Test
+ public void testSimanticsBug1893()
+ throws DatabaseException {
+ session = getSession();
+ for (int i=0; i<THREAD_COUNT; ++i)
+ threads[i] = new TestThread(i);
+ for (int i=0; i<THREAD_COUNT; ++i)
+ threads[i].start();
+ while (exitCount.get() < THREAD_COUNT) {
+ try {
+ Thread.sleep(100); // milliseconds
+ } catch (InterruptedException e) {
+ }
+ }
+ for (int i=0; i<THREAD_COUNT; ++i)
+ if (null != threads[i].exception)
+ throw threads[i].exception;
+ for (int i=0; i<THREAD_COUNT; ++i)
+ while (threads[i].isAlive())
+ try {
+ Thread.sleep(100); // milliseconds
+ } catch (InterruptedException e) {
+ }
+ }
+ class TestThread extends Thread {
+ TestThread(int i) {
+ super("Test Thread " + i);
+ }
+ DatabaseException exception = null;
+ @Override
+ public void run() {
+ try {
+ session.syncRequest(new Query());
+ } catch (DatabaseException e) {
+ exception = e;
+ } finally {
+ exitCount.incrementAndGet();
+ }
+ }
+ }
+ class MyResourceRead extends ResourceRead<String> {
+ MyResourceRead(Resource resource) {
+ super(resource);
+ }
+ @Override
+ public String perform(ReadGraph g) throws DatabaseException {
+ if (DEBUG)
+ System.out.println("MyResourceRead");
+ g.getResource("huuhaa");
+ return "huuhaa";
+ }
+ @Override
+ public boolean equals(Object object) {
+ if (this == object)
+ return true;
+ else
+ return false;
+ }
+ }
+ class MyListener implements Listener<String> {
+
+ @Override
+ public void execute(String result) {
+ if (DEBUG)
+ System.out.println("execute=" + result);
+ }
+
+ @Override
+ public void exception(Throwable t) {
+ if (DEBUG)
+ System.out.println("exception=" + t.getMessage());
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return false;
+ }
+
+ }
+ class TestRequest extends WriteRequest {
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ for (int i=0; i<THREAD_COUNT; ++i)
+ threads[i] = new TestThread(i);
+ for (int i=0; i<THREAD_COUNT; ++i)
+ threads[i].start();
+ while (exitCount.get() < THREAD_COUNT) {
+ try {
+ Thread.sleep(100); // milliseconds
+ } catch (InterruptedException e) {
+ }
+ }
+ for (int i=0; i<THREAD_COUNT; ++i)
+ if (null != threads[i].exception)
+ throw threads[i].exception;
+ }
+ }
+ class TestRequest2 extends ReadRequest {
+ @Override
+ public void run(ReadGraph g) throws DatabaseException {
+ for (int i=0; i<THREAD_COUNT; ++i)
+ threads[i] = new TestThread(i);
+ for (int i=0; i<THREAD_COUNT; ++i)
+ threads[i].start();
+ while (exitCount.get() < THREAD_COUNT) {
+ try {
+ Thread.sleep(100); // milliseconds
+ } catch (InterruptedException e) {
+ }
+ }
+ for (int i=0; i<THREAD_COUNT; ++i)
+ if (null != threads[i].exception)
+ throw threads[i].exception;
+ }
+ }
+ class Query extends ReadRequest {
+ @Override
+ public void run(ReadGraph g) throws DatabaseException {
+ Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
+ if (null == rl)
+ fail("Root library resource not found by URI.");
+ if (DEBUG)
+ System.out.println("async request");
+ session.asyncRequest(new MyResourceRead(rl), new MyListener());
+ }
+ }
+}