use of org.structr.web.entity.TestFive in project structr by structr.
the class CacheTest method testCaching.
@Test
public void testCaching() {
try {
Thread.sleep(2000);
} catch (Throwable t) {
}
final ExecutorService service = Executors.newCachedThreadPool();
final Queue<TestTwo> queue = new ConcurrentLinkedQueue<>();
final AtomicBoolean doRun = new AtomicBoolean(true);
service.submit(() -> {
// caching layer.
while (doRun.get()) {
final TestTwo obj = queue.poll();
if (obj != null) {
try (final Tx tx = app.tx()) {
obj.getProperty(TestTwo.testFives);
tx.success();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
});
// create an object in a transaction and hand it over
// to a different thread right afterwards, and add
// relationships in the original thread
final int num = 10;
final int count = 10;
for (int i = 0; i < num; i++) {
TestTwo obj = null;
try (final Tx tx = app.tx()) {
// create test object
obj = app.create(TestTwo.class, "testTwo" + i);
// add related nodes
for (int j = 0; j < count; j++) {
final TestFive tmp = app.create(TestFive.class, new NodeAttribute<>(TestFive.testTwo, obj));
}
queue.add(obj);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
try (final Tx tx = app.tx()) {
final List<TestFive> testFives = obj.getProperty(TestTwo.testFives);
final int size = testFives.size();
if (size != count) {
System.out.println("Leaking cache detected, collection has wrong number of elements: " + count + " vs. " + size);
}
// do not fail this test for now
// assertEquals("Leaking cache detected, collection has wrong number of elements", count, size);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
}
// stop worker thread
doRun.set(false);
service.shutdown();
}
Aggregations