Search in sources :

Example 1 with ConnectionPool

use of com.cinchapi.concourse.ConnectionPool in project concourse by cinchapi.

the class CON669 method testConsistencyOfWideReadsWithConcurrentWrites.

@Test
public void testConsistencyOfWideReadsWithConcurrentWrites() throws Exception {
    int threads = 10;
    ConnectionPool connections = ConnectionPool.newCachedConnectionPool(SERVER_HOST, SERVER_PORT, "admin", "admin");
    try {
        client.set("count", 1L, 1);
        AtomicBoolean done = new AtomicBoolean(false);
        AtomicBoolean passed = new AtomicBoolean(true);
        Thread reader = new Thread(() -> {
            while (!done.get()) {
                Concourse con = connections.request();
                try {
                    Assert.assertFalse(con.select(1).get("count").isEmpty());
                } catch (Exception e) {
                    e.printStackTrace();
                    passed.set(false);
                } finally {
                    connections.release(con);
                }
            }
        });
        reader.start();
        for (int i = 0; i < threads; ++i) {
            Thread t = new Thread(() -> {
                while (!done.get()) {
                    Concourse con = connections.request();
                    try {
                        long expected = (long) con.select(1).get("count").iterator().next();
                        con.verifyAndSwap("count", expected, 1, Time.now());
                    } catch (Exception e) {
                        e.printStackTrace();
                        passed.set(false);
                    } finally {
                        connections.release(con);
                    }
                }
            });
            t.start();
        }
        Threads.sleep(3000);
        done.set(true);
        Assert.assertTrue(passed.get());
    } finally {
        while (!connections.isClosed()) {
            try {
                connections.close();
            } catch (IllegalStateException e) {
                Threads.sleep(100);
            }
        }
    }
}
Also used : ConnectionPool(com.cinchapi.concourse.ConnectionPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Concourse(com.cinchapi.concourse.Concourse) ConcourseIntegrationTest(com.cinchapi.concourse.test.ConcourseIntegrationTest) Test(org.junit.Test)

Aggregations

Concourse (com.cinchapi.concourse.Concourse)1 ConnectionPool (com.cinchapi.concourse.ConnectionPool)1 ConcourseIntegrationTest (com.cinchapi.concourse.test.ConcourseIntegrationTest)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1