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);
}
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);
}
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();
}
}
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");
}
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();
}
}
Aggregations