Search in sources :

Example 16 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project Smack by igniterealtime.

the class ResultSyncPointTest method testResultSyncPoint.

@Test
public void testResultSyncPoint() throws InterruptedException, TimeoutException, Exception {
    final String result = "Hip Hip Hurrary!!111!";
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final ResultSyncPoint<String, Exception> rsp = new ResultSyncPoint<>();
    Async.go(new Async.ThrowingRunnable() {

        @Override
        public void runOrThrow() throws InterruptedException, BrokenBarrierException {
            barrier.await();
            rsp.signal(result);
        }
    });
    barrier.await();
    String receivedResult = rsp.waitForResult(60 * 1000);
    assertEquals(result, receivedResult);
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Async(org.jivesoftware.smack.util.Async) TimeoutException(java.util.concurrent.TimeoutException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 17 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project Smack by igniterealtime.

the class ResultSyncPointTest method exceptionTestResultSyncPoint.

@Test(expected = TestException.class)
public void exceptionTestResultSyncPoint() throws InterruptedException, TimeoutException, Exception {
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final ResultSyncPoint<String, TestException> rsp = new ResultSyncPoint<>();
    Async.go(new Async.ThrowingRunnable() {

        @Override
        public void runOrThrow() throws InterruptedException, BrokenBarrierException {
            barrier.await();
            rsp.signal(new TestException());
        }
    });
    barrier.await();
    rsp.waitForResult(60 * 1000);
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Async(org.jivesoftware.smack.util.Async) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 18 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project neo4j by neo4j.

the class ConcurrentInstanceStartupIT method concurrentStartupShouldWork.

@Test
public void concurrentStartupShouldWork() throws Exception {
    // Ensures that the instances don't race to create the test's base directory and only care about their own.
    testDirectory.directory("nothingToSeeHereMoveAlong");
    StringBuffer initialHostsBuffer = new StringBuffer("127.0.0.1:5001");
    for (int i = 2; i <= INSTANCE_COUNT; i++) {
        initialHostsBuffer.append(",127.0.0.1:500" + i);
    }
    final String initialHosts = initialHostsBuffer.toString();
    final CyclicBarrier barrier = new CyclicBarrier(INSTANCE_COUNT);
    final List<Thread> daThreads = new ArrayList<Thread>(INSTANCE_COUNT);
    final HighlyAvailableGraphDatabase[] dbs = new HighlyAvailableGraphDatabase[INSTANCE_COUNT];
    for (int i = 1; i <= INSTANCE_COUNT; i++) {
        final int finalI = i;
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    barrier.await();
                    dbs[finalI - 1] = startDbAtBase(finalI, initialHosts);
                } catch (InterruptedException | BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        daThreads.add(t);
        t.start();
    }
    for (Thread daThread : daThreads) {
        daThread.join();
    }
    boolean masterDone = false;
    for (HighlyAvailableGraphDatabase db : dbs) {
        if (db.isMaster()) {
            if (masterDone) {
                throw new Exception("Two masters discovered");
            }
            masterDone = true;
        }
        try (Transaction tx = db.beginTx()) {
            db.createNode();
            tx.success();
        }
    }
    assertTrue(masterDone);
    for (HighlyAvailableGraphDatabase db : dbs) {
        db.shutdown();
    }
}
Also used : ArrayList(java.util.ArrayList) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Example 19 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project presto by prestodb.

the class TestHttpPageBufferClient method testCloseDuringPendingRequest.

@Test
public void testCloseDuringPendingRequest() throws Exception {
    CyclicBarrier beforeRequest = new CyclicBarrier(2);
    CyclicBarrier afterRequest = new CyclicBarrier(2);
    StaticRequestProcessor processor = new StaticRequestProcessor(beforeRequest, afterRequest);
    processor.setResponse(new TestingResponse(HttpStatus.NO_CONTENT, ImmutableListMultimap.of(), new byte[0]));
    CyclicBarrier requestComplete = new CyclicBarrier(2);
    TestingClientCallback callback = new TestingClientCallback(requestComplete);
    URI location = URI.create("http://localhost:8080");
    HttpPageBufferClient client = new HttpPageBufferClient(new TestingHttpClient(processor, executor), new DataSize(10, Unit.MEGABYTE), new Duration(1, TimeUnit.MINUTES), new Duration(1, TimeUnit.MINUTES), location, callback, executor);
    assertStatus(client, location, "queued", 0, 0, 0, 0, "not scheduled");
    // send request
    client.scheduleRequest();
    beforeRequest.await(10, TimeUnit.SECONDS);
    assertStatus(client, location, "running", 0, 1, 0, 0, "PROCESSING_REQUEST");
    assertEquals(client.isRunning(), true);
    // request is pending, now close it
    client.close();
    try {
        requestComplete.await(10, TimeUnit.SECONDS);
    } catch (BrokenBarrierException ignored) {
    }
    try {
        afterRequest.await(10, TimeUnit.SECONDS);
    } catch (BrokenBarrierException ignored) {
        afterRequest.reset();
    }
    // client.close() triggers a DELETE request, so wait for it to finish
    beforeRequest.await(10, TimeUnit.SECONDS);
    afterRequest.await(10, TimeUnit.SECONDS);
    requestComplete.await(10, TimeUnit.SECONDS);
    assertStatus(client, location, "closed", 0, 1, 2, 1, "not scheduled");
}
Also used : TestingResponse(io.airlift.http.client.testing.TestingResponse) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) DataSize(io.airlift.units.DataSize) Duration(io.airlift.units.Duration) URI(java.net.URI) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 20 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project quasar by puniverse.

the class CyclicBarrier method dowait.

/**
     * Main barrier code, covering the various policies.
     */
@Suspendable
private int dowait(boolean timed, long nanos) throws InterruptedException, BrokenBarrierException, TimeoutException {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        final Generation g = generation;
        if (g.broken)
            throw new BrokenBarrierException();
        if (Strand.interrupted()) {
            breakBarrier();
            throw new InterruptedException();
        }
        int index = --count;
        if (index == 0) {
            // tripped
            boolean ranAction = false;
            try {
                final Runnable command = barrierCommand;
                if (command != null)
                    command.run();
                ranAction = true;
                nextGeneration();
                return 0;
            } finally {
                if (!ranAction)
                    breakBarrier();
            }
        }
        // loop until tripped, broken, interrupted, or timed out
        for (; ; ) {
            try {
                if (!timed)
                    trip.await();
                else if (nanos > 0L)
                    nanos = trip.awaitNanos(nanos);
            } catch (InterruptedException ie) {
                if (g == generation && !g.broken) {
                    breakBarrier();
                    throw ie;
                } else {
                    // We're about to finish waiting even if we had not
                    // been interrupted, so this interrupt is deemed to
                    // "belong" to subsequent execution.
                    Strand.currentStrand().interrupt();
                }
            }
            if (g.broken)
                throw new BrokenBarrierException();
            if (g != generation)
                return index;
            if (timed && nanos <= 0L) {
                breakBarrier();
                throw new TimeoutException();
            }
        }
    } finally {
        lock.unlock();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TimeoutException(java.util.concurrent.TimeoutException) Suspendable(co.paralleluniverse.fibers.Suspendable)

Aggregations

BrokenBarrierException (java.util.concurrent.BrokenBarrierException)62 CyclicBarrier (java.util.concurrent.CyclicBarrier)53 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 IOException (java.io.IOException)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 ExecutionException (java.util.concurrent.ExecutionException)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 List (java.util.List)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 TimeoutException (java.util.concurrent.TimeoutException)8 Random (java.util.Random)7 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)7 Set (java.util.Set)6 TimeUnit (java.util.concurrent.TimeUnit)6