Search in sources :

Example 11 with Timeout

use of org.junit.jupiter.api.Timeout in project neo4j by neo4j.

the class CentralJobSchedulerTest method scheduledTasksThatThrowsPropagateDoNotPropagateExceptionAfterSubsequentExecution.

@Test
@Timeout(60)
void scheduledTasksThatThrowsPropagateDoNotPropagateExceptionAfterSubsequentExecution() throws InterruptedException {
    life.start();
    RuntimeException boom = new RuntimeException("boom");
    AtomicBoolean throwException = new AtomicBoolean();
    CountDownLatch startCounter = new CountDownLatch(10);
    AtomicBoolean canceled = new AtomicBoolean();
    Runnable job = () -> {
        try {
            if (throwException.compareAndSet(false, true)) {
                throw boom;
            }
        } finally {
            startCounter.countDown();
        }
    };
    JobHandle<?> handle = scheduler.scheduleRecurring(Group.INDEX_POPULATION, NOT_MONITORED, job, 1, TimeUnit.MILLISECONDS);
    handle.registerCancelListener(() -> canceled.set(true));
    startCounter.await();
    handle.cancel();
    assertTrue(canceled.get());
    assertThrows(CancellationException.class, handle::waitTermination);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 12 with Timeout

use of org.junit.jupiter.api.Timeout in project neo4j by neo4j.

the class InternalTransactionCommitProcessIT method commitDuringContinuousCheckpointing.

@Test
@Timeout(value = 5, unit = TimeUnit.MINUTES)
void commitDuringContinuousCheckpointing() throws Exception {
    final AtomicBoolean done = new AtomicBoolean();
    Workers<Runnable> workers = new Workers<>(getClass().getSimpleName());
    try {
        for (int i = 0; i < TOTAL_ACTIVE_THREADS; i++) {
            workers.start(new Runnable() {

                private final ThreadLocalRandom random = ThreadLocalRandom.current();

                @Override
                public void run() {
                    while (!done.get()) {
                        try (Transaction tx = db.beginTx()) {
                            tx.createNode();
                            tx.commit();
                        }
                        randomSleep();
                    }
                }

                private void randomSleep() {
                    try {
                        Thread.sleep(random.nextInt(50));
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }
        Thread.sleep(SECONDS.toMillis(2));
    } finally {
        done.set(true);
    }
    workers.awaitAndThrowOnError();
    assertThat(countsStore.txId()).as("Count store should be rotated once at least").isGreaterThan(0L);
    long lastRotationTx = checkPointer.forceCheckPoint(new SimpleTriggerInfo("test"));
    assertEquals(transactionIdStore.getLastClosedTransactionId(), lastRotationTx, "NeoStore last closed transaction id should be equal last count store rotation transaction id.");
    assertEquals(transactionIdStore.getLastClosedTransactionId(), countsStore.txId(), "Last closed transaction should be last rotated tx in count store");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SimpleTriggerInfo(org.neo4j.kernel.impl.transaction.log.checkpoint.SimpleTriggerInfo) Transaction(org.neo4j.graphdb.Transaction) Workers(org.neo4j.internal.batchimport.cache.idmapping.string.Workers) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 13 with Timeout

use of org.junit.jupiter.api.Timeout in project neo4j by neo4j.

the class KernelTransactionTimeoutMonitorIT method terminatingTransactionMustEagerlyReleaseTheirLocks.

@Test
@Timeout(30)
void terminatingTransactionMustEagerlyReleaseTheirLocks() throws Exception {
    AtomicBoolean nodeLockAcquired = new AtomicBoolean();
    AtomicBoolean lockerDone = new AtomicBoolean();
    BinaryLatch lockerPause = new BinaryLatch();
    long nodeId;
    try (Transaction tx = database.beginTx()) {
        nodeId = tx.createNode().getId();
        tx.commit();
    }
    Future<?> locker = executor.submit(() -> {
        try (Transaction tx = database.beginTx()) {
            Node node = tx.getNodeById(nodeId);
            tx.acquireReadLock(node);
            nodeLockAcquired.set(true);
            lockerPause.await();
        }
        lockerDone.set(true);
    });
    boolean proceed;
    do {
        proceed = nodeLockAcquired.get();
    } while (!proceed);
    terminateOngoingTransaction();
    // but the thread should still be blocked on the latch
    assertFalse(lockerDone.get());
    // Yet we should be able to proceed and grab the locks they once held
    try (Transaction tx = database.beginTx()) {
        // Write-locking is only possible if their shared lock was released
        tx.acquireWriteLock(tx.getNodeById(nodeId));
        tx.commit();
    }
    // No exception from our lock client being stopped (e.g. we ended up blocked for too long) or from timeout
    lockerPause.release();
    locker.get();
    assertTrue(lockerDone.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 14 with Timeout

use of org.junit.jupiter.api.Timeout in project neo4j by neo4j.

the class RecordStorageEngineTest method shutdownRecordStorageEngineAfterFailedTransaction.

@Test
@Timeout(30)
void shutdownRecordStorageEngineAfterFailedTransaction() throws Exception {
    RecordStorageEngine engine = buildRecordStorageEngine();
    Exception applicationError = executeFailingTransaction(engine);
    assertNotNull(applicationError);
}
Also used : UnderlyingStorageException(org.neo4j.exceptions.UnderlyingStorageException) IOException(java.io.IOException) KernelException(org.neo4j.exceptions.KernelException) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 15 with Timeout

use of org.junit.jupiter.api.Timeout in project neo4j by neo4j.

the class TransactionIT method begin__execute__rollback_concurrently.

@ParameterizedTest
@MethodSource("argumentsProvider")
@Timeout(30)
public void begin__execute__rollback_concurrently(String txUri) throws Exception {
    this.txUri = txUri;
    // begin
    final Response begin = POST(txUri);
    assertThat(begin.status()).isEqualTo(201);
    assertHasTxLocation(begin, txUri);
    Label sharedLockLabel = Label.label("sharedLock");
    POST(transactionCommitUri(), quotedJson("{ 'statements': [ { 'statement': 'CREATE (n:" + sharedLockLabel + ")' } ] }"));
    CountDownLatch nodeLockLatch = new CountDownLatch(1);
    CountDownLatch nodeReleaseLatch = new CountDownLatch(1);
    Future<?> lockerFuture = executors.submit(() -> lockNodeWithLabel(sharedLockLabel, nodeLockLatch, nodeReleaseLatch));
    nodeLockLatch.await();
    // execute
    final String executeResource = begin.location();
    final String statement = "MATCH (n:" + sharedLockLabel + ") DELETE n RETURN count(n)";
    final Future<Response> executeFuture = executors.submit(() -> {
        HTTP.Builder requestBuilder = HTTP.withBaseUri(container().getBaseUri());
        Response response = requestBuilder.POST(executeResource, quotedJson("{ 'statements': [ { 'statement': '" + statement + "' } ] }"));
        assertThat(response.status()).isEqualTo(200);
        return response;
    });
    // terminate
    final Future<Response> interruptFuture = executors.submit(() -> {
        waitForStatementExecution(statement);
        Response response = DELETE(executeResource);
        assertThat(response.status()).as(response.toString()).isEqualTo(200);
        nodeReleaseLatch.countDown();
        return response;
    });
    interruptFuture.get();
    lockerFuture.get();
    Response execute = executeFuture.get();
    assertThat(execute).satisfies(hasErrors(Status.Statement.Statement.ExecutionFailed));
    Response execute2 = POST(executeResource, quotedJson("{ 'statements': [ { 'statement': 'CREATE (n)' } ] }"));
    assertThat(execute2.status()).isEqualTo(404);
    assertThat(execute2).satisfies(hasErrors(Status.Transaction.TransactionNotFound));
}
Also used : Response(org.neo4j.test.server.HTTP.Response) Label(org.neo4j.graphdb.Label) HTTP(org.neo4j.test.server.HTTP) CountDownLatch(java.util.concurrent.CountDownLatch) Timeout(org.junit.jupiter.api.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Timeout (org.junit.jupiter.api.Timeout)291 Test (org.junit.jupiter.api.Test)235 CountDownLatch (java.util.concurrent.CountDownLatch)71 ZooKeeper (org.apache.zookeeper.ZooKeeper)33 AtomicReference (java.util.concurrent.atomic.AtomicReference)32 ArrayList (java.util.ArrayList)31 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29 RepeatedTest (org.junit.jupiter.api.RepeatedTest)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 LocalChannel (io.netty.channel.local.LocalChannel)27 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)26 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)25 IOException (java.io.IOException)25 Bootstrap (io.netty.bootstrap.Bootstrap)24 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 Channel (io.netty.channel.Channel)23 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)21 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)19 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)19