Search in sources :

Example 16 with Timeout

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

the class IndexIT method createIndexesForDifferentLabelsConcurrently.

@Test
@Timeout(10)
void createIndexesForDifferentLabelsConcurrently() throws Throwable {
    TokenWrite tokenWrite = tokenWriteInNewTransaction();
    int label2 = tokenWrite.labelGetOrCreateForName("Label2");
    commit();
    LabelSchemaDescriptor anotherLabelDescriptor = SchemaDescriptor.forLabel(label2, propertyKeyId);
    schemaWriteInNewTransaction().indexCreate(anotherLabelDescriptor, "my index");
    Future<?> indexFuture = executorService.submit(createIndex(db, label(LABEL), PROPERTY_KEY));
    indexFuture.get();
    commit();
}
Also used : TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest) Timeout(org.junit.jupiter.api.Timeout)

Example 17 with Timeout

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

the class KernelTransactionTerminationTest method closeTransaction.

@Test
@Timeout(TEST_RUN_TIME_SECS * 20)
void closeTransaction() throws Throwable {
    BlockingQueue<Boolean> committerToTerminator = new LinkedBlockingQueue<>(1);
    BlockingQueue<TerminatorAction> terminatorToCommitter = new LinkedBlockingQueue<>(1);
    AtomicBoolean t1Done = new AtomicBoolean();
    runTwoThreads(() -> {
        committerToTerminator.clear();
        terminatorToCommitter.clear();
        t1Done.set(false);
    }, tx -> {
        Boolean terminatorShouldAct = committerToTerminator.poll();
        if (terminatorShouldAct != null && terminatorShouldAct) {
            TerminatorAction action = TerminatorAction.random();
            action.executeOn(tx);
            assertTrue(terminatorToCommitter.add(action));
        }
        t1Done.set(true);
    }, tx -> {
        CommitterAction committerAction = CommitterAction.random();
        if (committerToTerminator.offer(true)) {
            TerminatorAction terminatorAction = null;
            try {
                // This loop optimizes the wait instead of waiting potentially a long time for T1 when it would lose the race and not do anything
                while (!t1Done.get() && terminatorAction == null) {
                    terminatorAction = terminatorToCommitter.poll(10, MILLISECONDS);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return;
            }
            if (terminatorAction != null) {
                close(tx, committerAction, terminatorAction);
            }
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 18 with Timeout

use of org.junit.jupiter.api.Timeout in project spring-framework by spring-projects.

the class AbstractSockJsIntegrationTests method fallbackAfterConnectTimeout.

@Test
@Timeout(5)
public void fallbackAfterConnectTimeout() throws Exception {
    TestClientHandler clientHandler = new TestClientHandler();
    this.testFilter.sleepDelayMap.put("/xhr_streaming", 10000L);
    this.testFilter.sendErrorMap.put("/xhr_streaming", 503);
    initSockJsClient(createXhrTransport());
    this.sockJsClient.setConnectTimeoutScheduler(this.wac.getBean(ThreadPoolTaskScheduler.class));
    WebSocketSession clientSession = sockJsClient.doHandshake(clientHandler, this.baseUrl + "/echo").get();
    assertThat(clientSession.getClass()).as("Fallback didn't occur").isEqualTo(XhrClientSockJsSession.class);
    TextMessage message = new TextMessage("message1");
    clientSession.sendMessage(message);
    clientHandler.awaitMessage(message, 5000);
    clientSession.close();
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 19 with Timeout

use of org.junit.jupiter.api.Timeout in project kafka by apache.

the class UtilsTest method testFileAsStringNamedPipe.

/**
 * Test to read content of named pipe as string. As reading/writing to a pipe can block,
 * timeout test after a minute (test finishes within 100 ms normally).
 */
@Timeout(60)
@Test
public void testFileAsStringNamedPipe() throws Exception {
    // Create a temporary name for named pipe
    Random random = new Random();
    long n = random.nextLong();
    n = n == Long.MIN_VALUE ? 0 : Math.abs(n);
    // Use the name to create a FIFO in tmp directory
    String tmpDir = System.getProperty("java.io.tmpdir");
    String fifoName = "fifo-" + n + ".tmp";
    File fifo = new File(tmpDir, fifoName);
    Thread producerThread = null;
    try {
        Process mkFifoCommand = new ProcessBuilder("mkfifo", fifo.getCanonicalPath()).start();
        mkFifoCommand.waitFor();
        // Send some data to fifo and then read it back, but as FIFO blocks if the consumer isn't present,
        // we need to send data in a separate thread.
        final String testFileContent = "This is test";
        producerThread = new Thread(() -> {
            try {
                Files.write(fifo.toPath(), testFileContent.getBytes());
            } catch (IOException e) {
                fail("Error when producing to fifo : " + e.getMessage());
            }
        }, "FIFO-Producer");
        producerThread.start();
        assertEquals(testFileContent, Utils.readFileAsString(fifo.getCanonicalPath()));
    } finally {
        Files.deleteIfExists(fifo.toPath());
        if (producerThread != null) {
            // Wait for thread to terminate
            producerThread.join(30 * 1000);
            assertFalse(producerThread.isAlive());
        }
    }
}
Also used : Random(java.util.Random) IOException(java.io.IOException) File(java.io.File) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 20 with Timeout

use of org.junit.jupiter.api.Timeout in project kafka by apache.

the class SenderTest method testForceShutdownWithIncompleteTransaction.

@Timeout(10L)
@Test
public void testForceShutdownWithIncompleteTransaction() {
    // create a sender with retries = 1
    int maxRetries = 1;
    Metrics m = new Metrics();
    SenderMetricsRegistry senderMetrics = new SenderMetricsRegistry(m);
    try {
        TransactionManager txnManager = new TransactionManager(logContext, "testForceShutdownWithIncompleteTransaction", 6000, 100, apiVersions);
        Sender sender = new Sender(logContext, client, metadata, this.accumulator, false, MAX_REQUEST_SIZE, ACKS_ALL, maxRetries, senderMetrics, time, REQUEST_TIMEOUT, RETRY_BACKOFF_MS, txnManager, apiVersions);
        ProducerIdAndEpoch producerIdAndEpoch = new ProducerIdAndEpoch(123456L, (short) 0);
        TopicPartition tp = new TopicPartition("testForceShutdownWithIncompleteTransaction", 1);
        setupWithTransactionState(txnManager);
        doInitTransactions(txnManager, producerIdAndEpoch);
        txnManager.beginTransaction();
        txnManager.maybeAddPartition(tp);
        client.prepareResponse(new AddPartitionsToTxnResponse(0, Collections.singletonMap(tp, Errors.NONE)));
        sender.runOnce();
        // Try to commit the transaction but it won't happen as we'll forcefully close the sender
        TransactionalRequestResult commitResult = txnManager.beginCommit();
        sender.forceClose();
        sender.run();
        assertThrows(KafkaException.class, commitResult::await, "The test expected to throw a KafkaException for forcefully closing the sender");
    } finally {
        m.close();
    }
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) TopicPartition(org.apache.kafka.common.TopicPartition) AddPartitionsToTxnResponse(org.apache.kafka.common.requests.AddPartitionsToTxnResponse) ProducerIdAndEpoch(org.apache.kafka.common.utils.ProducerIdAndEpoch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

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