Search in sources :

Example 1 with TestFive

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestTwo(org.structr.web.entity.TestTwo) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) TestFive(org.structr.web.entity.TestFive) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Aggregations

ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1 FrameworkException (org.structr.common.error.FrameworkException)1 Tx (org.structr.core.graph.Tx)1 StructrUiTest (org.structr.web.StructrUiTest)1 TestFive (org.structr.web.entity.TestFive)1 TestTwo (org.structr.web.entity.TestTwo)1