1 package org.simantics.db.tests.regression.bugs;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.Session;
7 import org.simantics.db.WriteGraph;
8 import org.simantics.db.common.primitiverequest.PossibleObject;
9 import org.simantics.db.common.request.ReadRequest;
10 import org.simantics.db.common.request.WriteRequest;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.procedure.SyncListener;
13 import org.simantics.db.service.ClusterControl;
14 import org.simantics.db.testing.base.ExistingDatabaseTest;
15 import org.simantics.db.testing.common.TestBase;
16 import org.simantics.db.tests.common.Configuration;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.utils.DataContainer;
20 public class SimanticsBug1659Test1 extends ExistingDatabaseTest {
21 static int LOOP_COUNT = Configuration.get().i1659LoopCount;
22 static int RESOURCE_COUNT = 1000;
23 static boolean DEBUG = false;
24 static boolean DEBUG_LISTENER = false;
25 // Use transient listener.
26 static boolean USE_LISTENER = true;
27 // Use asynchronous listener.
28 static boolean USE_LISTENER2 = true;
29 // Use synchronous listener
30 static boolean USE_LISTENER3 = true;
34 DataContainer<Integer> loopCount = new DataContainer<Integer>();
35 DataContainer<Integer> listenerCount = new DataContainer<Integer>();
38 public void testSimanticsBug16591()
39 throws DatabaseException {
40 session = getSession();
41 session.syncRequest(new Init());
45 for (int i=0; i<LOOP_COUNT; ++i) {
48 System.out.println("Create " + i);
49 session.syncRequest(new Create());
51 System.out.println("Query " + i);
52 session.syncRequest(new Query());
54 System.out.println("Delete loop=" + i + " listeners=" + listenerCount.get());
56 ClusterControl support = getSession().getService(ClusterControl.class);
57 support.collectClusters(Integer.MAX_VALUE);
58 int count = listenerCount.get();
59 if (oldCount != count) {
60 // System.out.println("Listener count is " + count);
63 session.syncRequest(new Delete());
65 System.out.println("Done " + i);
67 // System.out.println("collect " + i);
70 // } catch (InterruptedException e) {
71 // e.printStackTrace();
75 class Init extends WriteRequest {
77 public void perform(WriteGraph g) throws DatabaseException {
78 Layer0 l0 = Layer0.getInstance(g);
79 Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
80 testRoot = g.newResource();
81 g.claim(testRoot, l0.InstanceOf, l0.Library);
82 g.claim(rl, l0.ConsistsOf, testRoot);
83 type = g.newResource();
84 g.claim(type, l0.Inherits, l0.Entity);
87 class Create extends WriteRequest {
89 public void perform(WriteGraph g) throws DatabaseException {
90 Resource root = g.newResource();
91 Layer0 l0 = Layer0.getInstance(g);
92 g.claim(root, l0.InstanceOf, l0.Library);
93 g.claim(testRoot, l0.ConsistsOf, root);
94 for (int i=0; i<RESOURCE_COUNT; ++i) {
97 System.out.println("flush");
100 Resource r = g.newResource();
101 g.claim(r, l0.InstanceOf, type);
102 g.claim(root, l0.ConsistsOf, r);
106 class Query extends ReadRequest {
108 public void run(ReadGraph g) throws DatabaseException {
109 Layer0 l0 = Layer0.getInstance(g);
110 for (Resource r : g.getObjects(testRoot, l0.ConsistsOf)) {
112 System.out.println("Resource " + r);
113 for (Resource rr : g.getObjects(r, l0.ConsistsOf)) {
115 // System.out.println("Resource " + rr);
116 if (!g.isInstanceOf(rr, type))
117 fail("Resource " + rr + " is not instance of type.");
118 if (!g.isInstanceOf(rr, l0.Entity))
119 fail("Resource " + rr + " is not instance of Entity.");
121 g.forPossibleObject(rr, l0.InstanceOf, new SyncListener<Resource>() {
124 public void execute(ReadGraph graph, Resource resource) throws DatabaseException {
126 System.out.println("change " + resource);
130 public void exception(ReadGraph graph, Throwable t)
131 throws DatabaseException {
133 fail("Listener got exception: " + t);
137 public boolean isDisposed() {
139 System.out.println("Asked if disposed.");
144 g.forPossibleObject(rr, l0.InstanceOf, new Listener(loopCount, listenerCount));
147 g.syncRequest(new PossibleObject(rr, l0.InstanceOf), new Listener(loopCount, listenerCount));
153 class Listener implements SyncListener<Resource> {
154 final DataContainer<Integer> loopCount;
155 final DataContainer<Integer> listenerCount;
157 boolean disposed = false;
158 Listener(DataContainer<Integer> loopCount, DataContainer<Integer> listenerCount) {
159 this.loopCount = loopCount;
160 this.listenerCount = listenerCount;
161 int value = this.listenerCount.get() + 1;
162 this.listenerCount.set(value);
163 this.me = loopCount.get();
166 public void execute(ReadGraph graph, Resource resource) throws DatabaseException {
168 System.out.println("change " + resource);
172 public void exception(ReadGraph graph, Throwable t)
173 throws DatabaseException {
175 fail("Listener got exception: " + t);
179 public boolean isDisposed() {
181 System.out.println("Asked if disposed.");
184 disposed = loopCount.get() != me;
186 int value = this.listenerCount.get() - 1;
187 this.listenerCount.set(value);
193 class Delete extends WriteRequest {
195 public void perform(WriteGraph g) throws DatabaseException {
196 Layer0 l0 = Layer0.getInstance(g);
197 for (Resource r : g.getObjects(testRoot, l0.ConsistsOf)) {
199 System.out.println("Deny resource " + r);
200 g.deny(testRoot, l0.ConsistsOf, r);