]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/SimanticsBug1659Test2.java
5c44a694c843d09740d4de1e5c5e0ca7847f503b
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / regression / bugs / SimanticsBug1659Test2.java
1 package org.simantics.db.tests.regression.bugs;
2
3 import java.util.UUID;
4
5 import org.junit.Test;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.Session;
10 import org.simantics.db.WriteGraph;
11 import org.simantics.db.WriteOnlyGraph;
12 import org.simantics.db.common.primitiverequest.PossibleObject;
13 import org.simantics.db.common.request.ReadRequest;
14 import org.simantics.db.common.request.WriteOnlyRequest;
15 import org.simantics.db.common.request.WriteRequest;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.procedure.SyncListener;
18 import org.simantics.db.service.ClusterControl;
19 import org.simantics.db.testing.base.ExistingDatabaseTest;
20 import org.simantics.db.testing.common.TestBase;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.utils.DataContainer;
23
24 public class SimanticsBug1659Test2 extends ExistingDatabaseTest {
25     static int LOOP_COUNT = 100;
26     static int CLUSTER_COUNT = 2;
27     static int RESOURCE_COUNT = 1000;
28     static boolean DEBUG = false;
29     static boolean DEBUG_LISTENER = false;
30     // Use transient listener.
31     static boolean USE_LISTENER = true;
32     // Use asynchronous listener.
33     static boolean USE_LISTENER2 = true;
34     // Use synchronous listener
35     static boolean USE_LISTENER3 = true;
36     Session session;
37     Resource testRoot;
38     Resource type;
39     DataContainer<Integer> loopCount = new DataContainer<Integer>();
40     DataContainer<Integer> listenerCount = new DataContainer<Integer>();
41     
42     @Test
43     public void testSimanticsBug1659_2()
44     throws DatabaseException {
45         session = getSession();
46         session.syncRequest(new Init());
47         loopCount.set(-1);
48         listenerCount.set(0);
49         int oldCount = 0;
50         for (int i=0; i<LOOP_COUNT; ++i) {
51             loopCount.set(i);
52             if (DEBUG)
53                 System.out.println("Create " + i);
54             session.syncRequest(new CreateWriteOnly());
55             if (DEBUG)
56                 System.out.println("Query " + i);
57             session.syncRequest(new Query());
58             if (DEBUG)
59                 System.out.println("Delete loop=" + i + " listeners=" + listenerCount.get());
60             loopCount.set(-1);
61             ClusterControl support = getSession().getService(ClusterControl.class);
62             support.collectClusters(Integer.MAX_VALUE);
63             int count = listenerCount.get();
64             if (oldCount != count) {
65 //                System.out.println("Listener count is " + count);
66                 oldCount = count;
67             }
68             session.syncRequest(new Delete());
69             if (DEBUG)
70                 System.out.println("Done " + i);
71         }
72     }
73     class Init extends WriteRequest {
74         @Override
75         public void perform(WriteGraph g) throws DatabaseException {
76             Layer0 l0 = Layer0.getInstance(g);
77             Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
78             testRoot = g.newResource();
79             g.claim(testRoot, l0.InstanceOf, l0.Library);          
80             g.claim(rl, l0.ConsistsOf, testRoot);
81 //            type = g.newResource();
82 //            g.claim(type, l0.Inherits, l0.Entity);          
83         }
84     }
85     class CreateWriteOnly extends WriteOnlyRequest {
86         @Override
87         public void perform(WriteOnlyGraph g) throws DatabaseException {
88             Layer0 b = Layer0.getInstance(g.getService(Session.class));
89             for (int j=0; j<CLUSTER_COUNT; ++j) {
90                 Resource root = g.newResource();
91                 g.claim(testRoot, b.ConsistsOf, b.PartOf, root);
92                 g.flushCluster();
93                 for(int i=0; i<RESOURCE_COUNT; i++) {
94                     Resource item = g.newResource();
95                     g.claim(item, b.InstanceOf, null, b.String);
96                     g.claimValue(item, UUID.randomUUID().toString(), Bindings.STRING);
97                     g.claim(root, b.ConsistsOf, b.PartOf, item);
98                 }
99             }
100         }
101     }
102     class Query extends ReadRequest {
103         @Override
104         public void run(ReadGraph g) throws DatabaseException {
105             Layer0 l0 = Layer0.getInstance(g);
106             for (Resource r : g.getObjects(testRoot, l0.ConsistsOf)) {
107                 if (DEBUG)
108                     System.out.println("Resource " + r);
109                 for (Resource rr : g.getObjects(r, l0.ConsistsOf)) {
110                         if (!g.isInstanceOf(rr, l0.String))
111                                 fail("Resource " + rr + " is not instance of String.");
112                         if (!g.isInstanceOf(rr, l0.Entity))
113                                 fail("Resource " + rr + " is not instance of Entity.");
114                         if (USE_LISTENER)
115                             g.forPossibleObject(rr, l0.InstanceOf, new SyncListener<Resource>() {
116         
117                                 @Override
118                                 public void execute(ReadGraph graph, Resource resource) throws DatabaseException {
119                                         if (DEBUG_LISTENER)
120                                                 System.out.println("change " + resource);
121                                 }
122         
123                                                 @Override
124                                                 public void exception(ReadGraph graph, Throwable t)
125                                                                 throws DatabaseException {
126                                                         t.printStackTrace();
127                                         fail("Listener got exception: " + t);
128                                                 }
129         
130                                                 @Override
131                                                 public boolean isDisposed() {
132                                                         if (DEBUG_LISTENER)
133                                                                 System.out.println("Asked if disposed.");
134                                                         return true;
135                                                 }
136                             });
137                     if (USE_LISTENER2) {
138                         g.forPossibleObject(rr, l0.InstanceOf, new Listener(loopCount, listenerCount));
139                     }
140                     if (USE_LISTENER3) {
141                        g.syncRequest(new PossibleObject(rr, l0.InstanceOf), new Listener(loopCount, listenerCount));
142                     }
143                 }
144             }
145         }
146     }
147     class Listener implements SyncListener<Resource> {
148         final DataContainer<Integer> loopCount;
149         final DataContainer<Integer> listenerCount;
150         final int me;
151         boolean disposed = false;
152         Listener(DataContainer<Integer> loopCount, DataContainer<Integer> listenerCount) {
153             this.loopCount = loopCount;
154             this.listenerCount = listenerCount;
155             int value = this.listenerCount.get() + 1;
156             this.listenerCount.set(value);
157             this.me = loopCount.get();
158         }
159         @Override
160         public void execute(ReadGraph graph, Resource resource) throws DatabaseException {
161             if (DEBUG_LISTENER)
162                 System.out.println("change " + resource);
163         }
164
165         @Override
166         public void exception(ReadGraph graph, Throwable t)
167                 throws DatabaseException {
168             t.printStackTrace();
169             fail("Listener got exception: " + t);
170         }
171
172         @Override
173         public boolean isDisposed() {
174             if (DEBUG_LISTENER)
175                 System.out.println("Asked if disposed.");
176             if (disposed)
177                 return true;
178             disposed = loopCount.get() != me;
179             if (disposed) {
180                 int value = this.listenerCount.get() - 1;
181                 this.listenerCount.set(value);
182             }
183             return disposed;
184         }
185         
186     }
187     class Delete extends WriteRequest {
188         @Override
189         public void perform(WriteGraph g) throws DatabaseException {
190             Layer0 l0 = Layer0.getInstance(g);
191             for (Resource r : g.getObjects(testRoot, l0.ConsistsOf)) {
192                 if (DEBUG)
193                     System.out.println("Deny resource " + r);
194                 g.deny(testRoot, l0.ConsistsOf, r);
195             }
196         }
197     }
198 }