Search in sources :

Example 1 with TransactionImpl

use of org.neo4j.kernel.impl.coreapi.TransactionImpl in project neo4j by neo4j.

the class TransactionImplTest method shouldReturnTerminationReason.

@Test
void shouldReturnTerminationReason() {
    KernelTransaction kernelTransaction = mock(KernelTransaction.class);
    when(kernelTransaction.getReasonIfTerminated()).thenReturn(Optional.empty()).thenReturn(Optional.of(Status.Transaction.Terminated));
    TransactionImpl tx = new TransactionImpl(tokenHolders, contextFactory, availabilityGuard, engine, kernelTransaction, null, null);
    Optional<Status> terminationReason1 = tx.terminationReason();
    Optional<Status> terminationReason2 = tx.terminationReason();
    assertFalse(terminationReason1.isPresent());
    assertTrue(terminationReason2.isPresent());
    assertEquals(Status.Transaction.Terminated, terminationReason2.get());
}
Also used : Status(org.neo4j.kernel.api.exceptions.Status) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl) Test(org.junit.jupiter.api.Test)

Example 2 with TransactionImpl

use of org.neo4j.kernel.impl.coreapi.TransactionImpl in project neo4j by neo4j.

the class TransactionImplTest method shouldThrowTransactionExceptionOnTransientKernelException.

@Test
void shouldThrowTransactionExceptionOnTransientKernelException() throws Exception {
    // GIVEN
    KernelTransaction kernelTransaction = mock(KernelTransaction.class);
    when(kernelTransaction.isOpen()).thenReturn(true);
    doThrow(new RuntimeException("Just a random failure")).when(kernelTransaction).close();
    TransactionImpl transaction = new TransactionImpl(tokenHolders, contextFactory, availabilityGuard, engine, kernelTransaction, null, null);
    // WHEN
    transaction.commit();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl) Test(org.junit.jupiter.api.Test)

Example 3 with TransactionImpl

use of org.neo4j.kernel.impl.coreapi.TransactionImpl in project neo4j by neo4j.

the class TransactionImplTest method fireCallbackOnClose.

@Test
void fireCallbackOnClose() {
    KernelTransaction kernelTransaction = mock(KernelTransaction.class);
    MutableLong calls = new MutableLong();
    // commit
    try (TransactionImpl tx = new TransactionImpl(tokenHolders, contextFactory, availabilityGuard, engine, kernelTransaction, null, null)) {
        tx.addCloseCallback(calls::increment);
        tx.commit();
    }
    // and rollback
    try (TransactionImpl tx = new TransactionImpl(tokenHolders, contextFactory, availabilityGuard, engine, kernelTransaction, null, null)) {
        tx.addCloseCallback(calls::increment);
        tx.rollback();
    }
    // and nothing
    try (TransactionImpl tx = new TransactionImpl(tokenHolders, contextFactory, availabilityGuard, engine, kernelTransaction, null, null)) {
        tx.addCloseCallback(calls::increment);
    }
    // should all invoke the callback
    assertEquals(3, calls.longValue());
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) MutableLong(org.apache.commons.lang3.mutable.MutableLong) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl) Test(org.junit.jupiter.api.Test)

Example 4 with TransactionImpl

use of org.neo4j.kernel.impl.coreapi.TransactionImpl in project neo4j by neo4j.

the class DenseNodeConcurrencyIT method assertNotBlocking.

private void assertNotBlocking(Consumer<Transaction> tx1, Consumer<Transaction> tx2) throws InterruptedException, ExecutionException {
    try (OtherThreadExecutor t2 = new OtherThreadExecutor("T2")) {
        Barrier.Control barrier = new Barrier.Control();
        Future<Object> t2Future = null;
        try {
            t2Future = t2.executeDontWait(command(() -> {
                try (Transaction tx = database.beginTx()) {
                    tx1.accept(tx);
                    ((TransactionImpl) tx).commit(barrier::reached);
                }
            }));
            barrier.await();
            try (Transaction tx = database.beginTx()) {
                tx2.accept(tx);
                tx.commit();
            }
        } finally {
            barrier.release();
            waitFor(t2Future);
        }
    }
}
Also used : OtherThreadExecutor(org.neo4j.test.OtherThreadExecutor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Barrier(org.neo4j.test.Barrier) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl)

Example 5 with TransactionImpl

use of org.neo4j.kernel.impl.coreapi.TransactionImpl in project neo4j by neo4j.

the class DenseNodeConcurrencyIT method assertBlocking.

private void assertBlocking(Consumer<Transaction> tx1, Consumer<Transaction> tx2, Predicate<OtherThreadExecutor.WaitDetails> waitDetailsPredicate) throws InterruptedException, ExecutionException, TimeoutException {
    try (OtherThreadExecutor t2 = new OtherThreadExecutor("T2");
        OtherThreadExecutor t3 = new OtherThreadExecutor("T3")) {
        Barrier.Control barrier = new Barrier.Control();
        Future<Object> t2Future = null;
        Future<Object> t3Future = null;
        try {
            t2Future = t2.executeDontWait(command(() -> {
                try (Transaction tx = database.beginTx()) {
                    tx1.accept(tx);
                    // ensure we upgrade to a write transaction to reach the barrier
                    tx.createNode();
                    ((TransactionImpl) tx).commit(barrier::reached);
                }
            }));
            barrier.await();
            t3Future = t3.executeDontWait(command(() -> {
                try (Transaction tx = database.beginTx()) {
                    tx2.accept(tx);
                    tx.commit();
                }
            }));
            t3.waitUntilWaiting(waitDetailsPredicate);
        } finally {
            barrier.release();
            waitFor(t2Future);
            waitFor(t3Future);
        }
    }
}
Also used : OtherThreadExecutor(org.neo4j.test.OtherThreadExecutor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Barrier(org.neo4j.test.Barrier) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl)

Aggregations

TransactionImpl (org.neo4j.kernel.impl.coreapi.TransactionImpl)12 Test (org.junit.jupiter.api.Test)8 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)8 Barrier (org.neo4j.test.Barrier)4 OtherThreadExecutor (org.neo4j.test.OtherThreadExecutor)4 Status (org.neo4j.kernel.api.exceptions.Status)2 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)2 MutableLong (org.apache.commons.lang3.mutable.MutableLong)1 TransactionTerminatedException (org.neo4j.graphdb.TransactionTerminatedException)1 TransientFailureException (org.neo4j.graphdb.TransientFailureException)1 TokenRead (org.neo4j.internal.kernel.api.TokenRead)1 TransactionFailureException (org.neo4j.internal.kernel.api.exceptions.TransactionFailureException)1