]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/SimanticsBug1893Test1.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / regression / bugs / SimanticsBug1893Test1.java
1 package org.simantics.db.tests.regression.bugs;
2
3 import java.util.concurrent.atomic.AtomicInteger;
4
5 import org.junit.Test;
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;
18
19 public class SimanticsBug1893Test1 extends ExistingDatabaseTest {
20     static int THREAD_COUNT = 2;
21     Session session;
22     Resource testRoot;
23     ClusterControl clusterControl;
24     TestThread[] threads = new TestThread[THREAD_COUNT];
25     AtomicInteger exitCount = new AtomicInteger(0);
26     @Test
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)
33             threads[i].start();
34         while (exitCount.get() < THREAD_COUNT) {
35             try {
36                 Thread.sleep(100); // milliseconds
37             } catch (InterruptedException e) {
38             }
39         }
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())
45                 try {
46                     Thread.sleep(100); // milliseconds
47                 } catch (InterruptedException e) {
48                 }
49     }
50     class TestThread extends Thread {
51         TestThread(int i) {
52             super("Test Thread " + i);
53         }
54         DatabaseException exception = null;
55         @Override
56         public void run() {
57             try  {
58                 session.syncRequest(new Query());
59             } catch (DatabaseException e) {
60                 exception = e;
61             } finally {
62                 exitCount.incrementAndGet();
63             }
64         }
65     }
66     class MyResourceRead extends ResourceRead<String> {
67         MyResourceRead(Resource resource) {
68             super(resource);
69         }
70         @Override
71         public String perform(ReadGraph g) throws DatabaseException {
72             if (DEBUG)
73                 System.out.println("MyResourceRead");
74             g.getResource("huuhaa");
75             return "huuhaa";
76         }
77         @Override
78         public boolean equals(Object object) {
79             if (this == object)
80                 return true;
81             else
82                 return false;
83         }
84     }
85     class MyListener implements Listener<String> {
86
87         @Override
88         public void execute(String result) {
89             if (DEBUG)
90                 System.out.println("execute=" + result);
91         }
92
93         @Override
94         public void exception(Throwable t) {
95             if (DEBUG)
96                 System.out.println("exception=" + t.getMessage());
97         }
98
99         @Override
100         public boolean isDisposed() {
101             return false;
102         }
103         
104     }
105     class TestRequest extends WriteRequest {
106         @Override
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)
111                 threads[i].start();
112             while (exitCount.get() < THREAD_COUNT) {
113                 try {
114                     Thread.sleep(100); // milliseconds
115                 } catch (InterruptedException e) {
116                 }
117             }
118             for (int i=0; i<THREAD_COUNT; ++i)
119                 if (null != threads[i].exception)
120                     throw threads[i].exception;
121         }
122     }
123     class TestRequest2 extends ReadRequest {
124         @Override
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)
129                 threads[i].start();
130             while (exitCount.get() < THREAD_COUNT) {
131                 try {
132                     Thread.sleep(100); // milliseconds
133                 } catch (InterruptedException e) {
134                 }
135             }
136             for (int i=0; i<THREAD_COUNT; ++i)
137                 if (null != threads[i].exception)
138                     throw threads[i].exception;
139         }
140     }
141     class Query extends ReadRequest {
142         @Override
143         public void run(ReadGraph g) throws DatabaseException {
144             Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
145             if (null == rl)
146                 fail("Root library resource not found by URI.");
147             if (DEBUG)
148                 System.out.println("async request");
149             session.asyncRequest(new MyResourceRead(rl), new MyListener());
150         }
151     }
152 }