]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/SimanticsBug1659Test1.java
768ebf73a8256dbb5629bd0acc432a6139edfca8
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / regression / bugs / SimanticsBug1659Test1.java
1 package org.simantics.db.tests.regression.bugs;
2
3 import org.junit.Test;
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;
19
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;
31     Session session;
32     Resource testRoot;
33     Resource type;
34     DataContainer<Integer> loopCount = new DataContainer<Integer>();
35     DataContainer<Integer> listenerCount = new DataContainer<Integer>();
36     
37     @Test
38     public void testSimanticsBug16591()
39     throws DatabaseException {
40         session = getSession();
41         session.syncRequest(new Init());
42         loopCount.set(-1);
43         listenerCount.set(0);
44         int oldCount = 0;
45         for (int i=0; i<LOOP_COUNT; ++i) {
46             loopCount.set(i);
47             if (DEBUG)
48                 System.out.println("Create " + i);
49             session.syncRequest(new Create());
50             if (DEBUG)
51                 System.out.println("Query " + i);
52             session.syncRequest(new Query());
53             if (DEBUG)
54                 System.out.println("Delete loop=" + i + " listeners=" + listenerCount.get());
55             loopCount.set(-1);
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);
61                 oldCount = count;
62             }
63             session.syncRequest(new Delete());
64             if (DEBUG)
65                 System.out.println("Done " + i);
66 //            if (DEBUG)
67 //                System.out.println("collect " + i);
68 //            try {
69 //                Thread.sleep(100);
70 //            } catch (InterruptedException e) {
71 //                e.printStackTrace();
72 //            }
73         }
74     }
75     class Init extends WriteRequest {
76         @Override
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);          
85         }
86     }
87     class Create extends WriteRequest {
88         @Override
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) {
95                 if (i % 10000 == 0) {
96                     if (DEBUG)
97                         System.out.println("flush");
98                     g.flushCluster();
99                 }
100                 Resource r = g.newResource();
101                 g.claim(r, l0.InstanceOf, type);
102                 g.claim(root, l0.ConsistsOf, r);
103             }
104         }
105     }
106     class Query extends ReadRequest {
107         @Override
108         public void run(ReadGraph g) throws DatabaseException {
109             Layer0 l0 = Layer0.getInstance(g);
110             for (Resource r : g.getObjects(testRoot, l0.ConsistsOf)) {
111                 if (DEBUG)
112                     System.out.println("Resource " + r);
113                 for (Resource rr : g.getObjects(r, l0.ConsistsOf)) {
114 //                    if (DEBUG)
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.");
120                         if (USE_LISTENER)
121                             g.forPossibleObject(rr, l0.InstanceOf, new SyncListener<Resource>() {
122         
123                                 @Override
124                                 public void execute(ReadGraph graph, Resource resource) throws DatabaseException {
125                                         if (DEBUG_LISTENER)
126                                                 System.out.println("change " + resource);
127                                 }
128         
129                                                 @Override
130                                                 public void exception(ReadGraph graph, Throwable t)
131                                                                 throws DatabaseException {
132                                                         t.printStackTrace();
133                                         fail("Listener got exception: " + t);
134                                                 }
135         
136                                                 @Override
137                                                 public boolean isDisposed() {
138                                                         if (DEBUG_LISTENER)
139                                                                 System.out.println("Asked if disposed.");
140                                                         return true;
141                                                 }
142                             });
143                     if (USE_LISTENER2) {
144                         g.forPossibleObject(rr, l0.InstanceOf, new Listener(loopCount, listenerCount));
145                     }
146                     if (USE_LISTENER3) {
147                        g.syncRequest(new PossibleObject(rr, l0.InstanceOf), new Listener(loopCount, listenerCount));
148                     }
149                 }
150             }
151         }
152     }
153     class Listener implements SyncListener<Resource> {
154         final DataContainer<Integer> loopCount;
155         final DataContainer<Integer> listenerCount;
156         final int me;
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();
164         }
165         @Override
166         public void execute(ReadGraph graph, Resource resource) throws DatabaseException {
167             if (DEBUG_LISTENER)
168                 System.out.println("change " + resource);
169         }
170
171         @Override
172         public void exception(ReadGraph graph, Throwable t)
173                 throws DatabaseException {
174             t.printStackTrace();
175             fail("Listener got exception: " + t);
176         }
177
178         @Override
179         public boolean isDisposed() {
180             if (DEBUG_LISTENER)
181                 System.out.println("Asked if disposed.");
182             if (disposed)
183                 return true;
184             disposed = loopCount.get() != me;
185             if (disposed) {
186                 int value = this.listenerCount.get() - 1;
187                 this.listenerCount.set(value);
188             }
189             return disposed;
190         }
191         
192     }
193     class Delete extends WriteRequest {
194         @Override
195         public void perform(WriteGraph g) throws DatabaseException {
196             Layer0 l0 = Layer0.getInstance(g);
197             for (Resource r : g.getObjects(testRoot, l0.ConsistsOf)) {
198                 if (DEBUG)
199                     System.out.println("Deny resource " + r);
200                 g.deny(testRoot, l0.ConsistsOf, r);
201             }
202         }
203     }
204 }