Search in sources :

Example 1 with Latch

use of jetbrains.exodus.core.execution.locks.Latch in project xodus by JetBrains.

the class EnvironmentTest method testClearWithTransaction_XD_457.

@Test
@TestFor(issues = "XD-457")
public void testClearWithTransaction_XD_457() throws InterruptedException {
    final Latch latch = Latch.create();
    env.executeInTransaction(new TransactionalExecutable() {

        @Override
        public void execute(@NotNull final Transaction txn) {
            final StoreImpl store = env.openStore("store", StoreConfig.WITHOUT_DUPLICATES, txn);
            store.put(txn, StringBinding.stringToEntry("0"), StringBinding.stringToEntry("0"));
            assertTrue(store.exists(txn, StringBinding.stringToEntry("0"), StringBinding.stringToEntry("0")));
            final Throwable[] th = { null };
            // asynchronously clear the environment
            try {
                latch.acquire();
                runParallelRunnable(new Runnable() {

                    @Override
                    public void run() {
                        latch.release();
                        try {
                            env.clear();
                        } catch (Throwable t) {
                            th[0] = t;
                        }
                        latch.release();
                    }
                });
                latch.acquire();
            } catch (InterruptedException ignore) {
                Thread.currentThread().interrupt();
                assertTrue(false);
            }
            Assert.assertNull(th[0]);
            assertTrue(store.exists(txn, StringBinding.stringToEntry("0"), StringBinding.stringToEntry("0")));
        }
    });
    latch.acquire();
    env.executeInExclusiveTransaction(new TransactionalExecutable() {

        @Override
        public void execute(@NotNull Transaction txn) {
            final StoreImpl store = env.openStore("store", StoreConfig.WITHOUT_DUPLICATES, txn);
            Assert.assertFalse(store.exists(txn, StringBinding.stringToEntry("0"), StringBinding.stringToEntry("0")));
        }
    });
}
Also used : Latch(jetbrains.exodus.core.execution.locks.Latch) Test(org.junit.Test)

Example 2 with Latch

use of jetbrains.exodus.core.execution.locks.Latch in project xodus by JetBrains.

the class EnvironmentTestsBase method executeParallelTransaction.

protected void executeParallelTransaction(@NotNull final TransactionalExecutable runnable) {
    try {
        final Latch sync = Latch.create();
        sync.acquire();
        processor.queue(new Job() {

            @Override
            protected void execute() throws Throwable {
                env.executeInTransaction(runnable);
                sync.release();
            }
        });
        sync.acquire();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    // ignore
    }
}
Also used : Latch(jetbrains.exodus.core.execution.locks.Latch) Job(jetbrains.exodus.core.execution.Job)

Aggregations

Latch (jetbrains.exodus.core.execution.locks.Latch)2 Job (jetbrains.exodus.core.execution.Job)1 Test (org.junit.Test)1