Search in sources :

Example 51 with AtomicReference

use of java.util.concurrent.atomic.AtomicReference in project crate by crate.

the class PageDownstreamContextTest method testKillCallsDownstream.

@Test
public void testKillCallsDownstream() throws Throwable {
    TestingBatchConsumer batchConsumer = new TestingBatchConsumer();
    PageDownstreamContext ctx = getPageDownstreamContext(batchConsumer, PassThroughPagingIterator.oneShot(), 3);
    final AtomicReference<Throwable> throwable = new AtomicReference<>();
    ctx.completionFuture().whenComplete((r, t) -> {
        if (t != null) {
            assertTrue(throwable.compareAndSet(null, t));
        } else {
            fail("Expected exception");
        }
    });
    ctx.kill(null);
    assertThat(throwable.get(), instanceOf(InterruptedException.class));
    expectedException.expect(InterruptedException.class);
    batchConsumer.getResult();
}
Also used : TestingBatchConsumer(io.crate.testing.TestingBatchConsumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 52 with AtomicReference

use of java.util.concurrent.atomic.AtomicReference in project crate by crate.

the class ConcurrencyIntegrationTest method testInsertStatementsDoNotShareState.

@Test
public void testInsertStatementsDoNotShareState() throws Throwable {
    execute("create table t1 (id int primary key, x int) with (number_of_replicas = 0)");
    execute("create table t2 (id int primary key, x string) with (number_of_replicas = 0)");
    execute("create table t3 (x timestamp) with (number_of_replicas = 0)");
    execute("create table t4 (y string) with (number_of_replicas = 0)");
    final CountDownLatch latch = new CountDownLatch(1000);
    final AtomicReference<Throwable> lastThrowable = new AtomicReference<>();
    String[] statements = new String[] { "insert into t1 (id, x) values (1, 10) on duplicate key update x = x + 10 ", "insert into t2 (id, x) values (1, 'bar') on duplicate key update x = 'foo' ", "insert into t3 (x) values (current_timestamp) ", "insert into t4 (y) values ('foo') " };
    // run every statement 5 times, so all will fit into the executors pool
    for (final String statement : statements) {
        for (int i = 0; i < 5; i++) {
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        while (latch.getCount() > 0) {
                            execute(statement);
                            latch.countDown();
                        }
                    } catch (Throwable t) {
                        // retry might not succeed
                        if (!t.getMessage().contains("version conflict")) {
                            lastThrowable.set(t);
                        }
                    }
                }
            });
        }
    }
    latch.await();
    Throwable throwable = lastThrowable.get();
    if (throwable != null) {
        throw throwable;
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 53 with AtomicReference

use of java.util.concurrent.atomic.AtomicReference in project crate by crate.

the class CreateTableIntegrationTest method executeCreateTableThreaded.

private void executeCreateTableThreaded(final String statement) throws Throwable {
    ExecutorService executorService = Executors.newFixedThreadPool(20);
    final AtomicReference<Throwable> lastThrowable = new AtomicReference<>();
    for (int i = 0; i < 20; i++) {
        executorService.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    execute(statement);
                } catch (Throwable t) {
                    lastThrowable.set(t);
                }
            }
        });
    }
    executorService.shutdown();
    assertThat("executorservice did not shutdown within timeout", executorService.awaitTermination(3, TimeUnit.SECONDS), is(true));
    Throwable throwable = lastThrowable.get();
    if (throwable != null) {
        throw throwable;
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 54 with AtomicReference

use of java.util.concurrent.atomic.AtomicReference in project crate by crate.

the class InformationSchemaQueryTest method testConcurrentInformationSchemaQueries.

@Test
public void testConcurrentInformationSchemaQueries() throws Exception {
    final SQLResponse response = execute("select * from information_schema.columns " + "order by table_schema, table_name, column_name");
    final CountDownLatch latch = new CountDownLatch(40);
    final AtomicReference<AssertionError> lastAssertionError = new AtomicReference<>();
    for (int i = 0; i < 40; i++) {
        final Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                SQLResponse resp = execute("select * from information_schema.columns " + "order by table_schema, table_name, column_name");
                try {
                    assertThat(resp.rows(), Matchers.equalTo(response.rows()));
                } catch (AssertionError e) {
                    lastAssertionError.set(e);
                }
                latch.countDown();
            }
        });
        t.start();
    }
    latch.await();
    AssertionError assertionError = lastAssertionError.get();
    if (assertionError != null) {
        throw assertionError;
    }
}
Also used : SQLResponse(io.crate.testing.SQLResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 55 with AtomicReference

use of java.util.concurrent.atomic.AtomicReference in project crate by crate.

the class InformationSchemaQueryTest method testConcurrentUnassignedShardsReferenceResolver.

@Test
public void testConcurrentUnassignedShardsReferenceResolver() throws Exception {
    internalCluster().ensureAtMostNumDataNodes(1);
    execute("create table t1 (col1 integer) " + "clustered into 1 shards " + "with (number_of_replicas=8)");
    execute("create table t2 (col1 integer) " + "clustered into 1 shards " + "with (number_of_replicas=8)");
    ensureYellow();
    final SQLResponse response = execute("select * from sys.shards where table_name in ('t1', 't2') and state='UNASSIGNED' order by schema_name, table_name, id");
    final CountDownLatch latch = new CountDownLatch(40);
    final AtomicReference<AssertionError> lastAssertionError = new AtomicReference<>();
    for (int i = 0; i < 40; i++) {
        final Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                SQLResponse resp = execute("select * from sys.shards where table_name in ('t1', 't2') and state='UNASSIGNED' order by schema_name, table_name, id");
                try {
                    assertThat(resp.rows(), Matchers.equalTo(response.rows()));
                } catch (AssertionError e) {
                    lastAssertionError.set(e);
                }
                latch.countDown();
            }
        });
        t.start();
    }
    latch.await();
    AssertionError assertionError = lastAssertionError.get();
    if (assertionError != null) {
        throw assertionError;
    }
}
Also used : SQLResponse(io.crate.testing.SQLResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)1331 Test (org.junit.Test)668 CountDownLatch (java.util.concurrent.CountDownLatch)437 IOException (java.io.IOException)263 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)205 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)159 ArrayList (java.util.ArrayList)108 HashMap (java.util.HashMap)105 List (java.util.List)95 Map (java.util.Map)77 Test (org.testng.annotations.Test)76 File (java.io.File)64 ExecutionException (java.util.concurrent.ExecutionException)60 HashSet (java.util.HashSet)54 URI (java.net.URI)48 TimeoutException (java.util.concurrent.TimeoutException)48 HttpServletRequest (javax.servlet.http.HttpServletRequest)48 HttpServletResponse (javax.servlet.http.HttpServletResponse)46 MockResponse (okhttp3.mockwebserver.MockResponse)46 ByteBuffer (java.nio.ByteBuffer)44