1 package org.simantics.db.tests.regression.bugs;
3 import java.util.concurrent.atomic.AtomicInteger;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.Session;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.common.request.ReadRequest;
11 import org.simantics.db.common.request.ResourceRead;
12 import org.simantics.db.common.request.WriteRequest;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.procedure.Listener;
15 import org.simantics.db.service.ClusterControl;
16 import org.simantics.db.testing.base.ExistingDatabaseTest;
17 import org.simantics.db.testing.common.TestBase;
19 public class SimanticsBug1893Test1 extends ExistingDatabaseTest {
20 static int THREAD_COUNT = 2;
23 ClusterControl clusterControl;
24 TestThread[] threads = new TestThread[THREAD_COUNT];
25 AtomicInteger exitCount = new AtomicInteger(0);
27 public void testSimanticsBug1893()
28 throws DatabaseException {
29 session = getSession();
30 for (int i=0; i<THREAD_COUNT; ++i)
31 threads[i] = new TestThread(i);
32 for (int i=0; i<THREAD_COUNT; ++i)
34 while (exitCount.get() < THREAD_COUNT) {
36 Thread.sleep(100); // milliseconds
37 } catch (InterruptedException e) {
40 for (int i=0; i<THREAD_COUNT; ++i)
41 if (null != threads[i].exception)
42 throw threads[i].exception;
43 for (int i=0; i<THREAD_COUNT; ++i)
44 while (threads[i].isAlive())
46 Thread.sleep(100); // milliseconds
47 } catch (InterruptedException e) {
50 class TestThread extends Thread {
52 super("Test Thread " + i);
54 DatabaseException exception = null;
58 session.syncRequest(new Query());
59 } catch (DatabaseException e) {
62 exitCount.incrementAndGet();
66 class MyResourceRead extends ResourceRead<String> {
67 MyResourceRead(Resource resource) {
71 public String perform(ReadGraph g) throws DatabaseException {
73 System.out.println("MyResourceRead");
74 g.getResource("huuhaa");
78 public boolean equals(Object object) {
85 class MyListener implements Listener<String> {
88 public void execute(String result) {
90 System.out.println("execute=" + result);
94 public void exception(Throwable t) {
96 System.out.println("exception=" + t.getMessage());
100 public boolean isDisposed() {
105 class TestRequest extends WriteRequest {
107 public void perform(WriteGraph g) throws DatabaseException {
108 for (int i=0; i<THREAD_COUNT; ++i)
109 threads[i] = new TestThread(i);
110 for (int i=0; i<THREAD_COUNT; ++i)
112 while (exitCount.get() < THREAD_COUNT) {
114 Thread.sleep(100); // milliseconds
115 } catch (InterruptedException e) {
118 for (int i=0; i<THREAD_COUNT; ++i)
119 if (null != threads[i].exception)
120 throw threads[i].exception;
123 class TestRequest2 extends ReadRequest {
125 public void run(ReadGraph g) throws DatabaseException {
126 for (int i=0; i<THREAD_COUNT; ++i)
127 threads[i] = new TestThread(i);
128 for (int i=0; i<THREAD_COUNT; ++i)
130 while (exitCount.get() < THREAD_COUNT) {
132 Thread.sleep(100); // milliseconds
133 } catch (InterruptedException e) {
136 for (int i=0; i<THREAD_COUNT; ++i)
137 if (null != threads[i].exception)
138 throw threads[i].exception;
141 class Query extends ReadRequest {
143 public void run(ReadGraph g) throws DatabaseException {
144 Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
146 fail("Root library resource not found by URI.");
148 System.out.println("async request");
149 session.asyncRequest(new MyResourceRead(rl), new MyListener());