--- /dev/null
+package org.simantics.db.tests.regression.bugs;
+
+import java.util.UUID;
+
+import org.junit.Test;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.WriteOnlyGraph;
+import org.simantics.db.common.primitiverequest.PossibleObject;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.WriteOnlyRequest;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.SyncListener;
+import org.simantics.db.service.ClusterControl;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.db.testing.common.TestBase;
+import org.simantics.layer0.Layer0;
+import org.simantics.utils.DataContainer;
+
+public class SimanticsBug1659Test2 extends ExistingDatabaseTest {
+ static int LOOP_COUNT = 100;
+ static int CLUSTER_COUNT = 2;
+ static int RESOURCE_COUNT = 1000;
+ static boolean DEBUG = false;
+ static boolean DEBUG_LISTENER = false;
+ // Use transient listener.
+ static boolean USE_LISTENER = true;
+ // Use asynchronous listener.
+ static boolean USE_LISTENER2 = true;
+ // Use synchronous listener
+ static boolean USE_LISTENER3 = true;
+ Session session;
+ Resource testRoot;
+ Resource type;
+ DataContainer<Integer> loopCount = new DataContainer<Integer>();
+ DataContainer<Integer> listenerCount = new DataContainer<Integer>();
+
+ @Test
+ public void testSimanticsBug1659_2()
+ throws DatabaseException {
+ session = getSession();
+ session.syncRequest(new Init());
+ loopCount.set(-1);
+ listenerCount.set(0);
+ int oldCount = 0;
+ for (int i=0; i<LOOP_COUNT; ++i) {
+ loopCount.set(i);
+ if (DEBUG)
+ System.out.println("Create " + i);
+ session.syncRequest(new CreateWriteOnly());
+ if (DEBUG)
+ System.out.println("Query " + i);
+ session.syncRequest(new Query());
+ if (DEBUG)
+ System.out.println("Delete loop=" + i + " listeners=" + listenerCount.get());
+ loopCount.set(-1);
+ ClusterControl support = getSession().getService(ClusterControl.class);
+ support.collectClusters(Integer.MAX_VALUE);
+ int count = listenerCount.get();
+ if (oldCount != count) {
+// System.out.println("Listener count is " + count);
+ oldCount = count;
+ }
+ session.syncRequest(new Delete());
+ if (DEBUG)
+ System.out.println("Done " + i);
+ }
+ }
+ class Init extends WriteRequest {
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ Layer0 l0 = Layer0.getInstance(g);
+ Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
+ testRoot = g.newResource();
+ g.claim(testRoot, l0.InstanceOf, l0.Library);
+ g.claim(rl, l0.ConsistsOf, testRoot);
+// type = g.newResource();
+// g.claim(type, l0.Inherits, l0.Entity);
+ }
+ }
+ class CreateWriteOnly extends WriteOnlyRequest {
+ @Override
+ public void perform(WriteOnlyGraph g) throws DatabaseException {
+ Layer0 b = Layer0.getInstance(g.getService(Session.class));
+ for (int j=0; j<CLUSTER_COUNT; ++j) {
+ Resource root = g.newResource();
+ g.claim(testRoot, b.ConsistsOf, b.PartOf, root);
+ g.flushCluster();
+ for(int i=0; i<RESOURCE_COUNT; i++) {
+ Resource item = g.newResource();
+ g.claim(item, b.InstanceOf, null, b.String);
+ g.claimValue(item, UUID.randomUUID().toString(), Bindings.STRING);
+ g.claim(root, b.ConsistsOf, b.PartOf, item);
+ }
+ }
+ }
+ }
+ class Query extends ReadRequest {
+ @Override
+ public void run(ReadGraph g) throws DatabaseException {
+ Layer0 l0 = Layer0.getInstance(g);
+ for (Resource r : g.getObjects(testRoot, l0.ConsistsOf)) {
+ if (DEBUG)
+ System.out.println("Resource " + r);
+ for (Resource rr : g.getObjects(r, l0.ConsistsOf)) {
+ if (!g.isInstanceOf(rr, l0.String))
+ fail("Resource " + rr + " is not instance of String.");
+ if (!g.isInstanceOf(rr, l0.Entity))
+ fail("Resource " + rr + " is not instance of Entity.");
+ if (USE_LISTENER)
+ g.forPossibleObject(rr, l0.InstanceOf, new SyncListener<Resource>() {
+
+ @Override
+ public void execute(ReadGraph graph, Resource resource) throws DatabaseException {
+ if (DEBUG_LISTENER)
+ System.out.println("change " + resource);
+ }
+
+ @Override
+ public void exception(ReadGraph graph, Throwable t)
+ throws DatabaseException {
+ t.printStackTrace();
+ fail("Listener got exception: " + t);
+ }
+
+ @Override
+ public boolean isDisposed() {
+ if (DEBUG_LISTENER)
+ System.out.println("Asked if disposed.");
+ return true;
+ }
+ });
+ if (USE_LISTENER2) {
+ g.forPossibleObject(rr, l0.InstanceOf, new Listener(loopCount, listenerCount));
+ }
+ if (USE_LISTENER3) {
+ g.syncRequest(new PossibleObject(rr, l0.InstanceOf), new Listener(loopCount, listenerCount));
+ }
+ }
+ }
+ }
+ }
+ class Listener implements SyncListener<Resource> {
+ final DataContainer<Integer> loopCount;
+ final DataContainer<Integer> listenerCount;
+ final int me;
+ boolean disposed = false;
+ Listener(DataContainer<Integer> loopCount, DataContainer<Integer> listenerCount) {
+ this.loopCount = loopCount;
+ this.listenerCount = listenerCount;
+ int value = this.listenerCount.get() + 1;
+ this.listenerCount.set(value);
+ this.me = loopCount.get();
+ }
+ @Override
+ public void execute(ReadGraph graph, Resource resource) throws DatabaseException {
+ if (DEBUG_LISTENER)
+ System.out.println("change " + resource);
+ }
+
+ @Override
+ public void exception(ReadGraph graph, Throwable t)
+ throws DatabaseException {
+ t.printStackTrace();
+ fail("Listener got exception: " + t);
+ }
+
+ @Override
+ public boolean isDisposed() {
+ if (DEBUG_LISTENER)
+ System.out.println("Asked if disposed.");
+ if (disposed)
+ return true;
+ disposed = loopCount.get() != me;
+ if (disposed) {
+ int value = this.listenerCount.get() - 1;
+ this.listenerCount.set(value);
+ }
+ return disposed;
+ }
+
+ }
+ class Delete extends WriteRequest {
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ Layer0 l0 = Layer0.getInstance(g);
+ for (Resource r : g.getObjects(testRoot, l0.ConsistsOf)) {
+ if (DEBUG)
+ System.out.println("Deny resource " + r);
+ g.deny(testRoot, l0.ConsistsOf, r);
+ }
+ }
+ }
+}