use of java.util.concurrent.CyclicBarrier in project druid by druid-io.
the class HdfsClasspathSetupTest method testConcurrentUpload.
@Test
public void testConcurrentUpload() throws IOException, InterruptedException, ExecutionException, TimeoutException {
final int concurrency = 10;
ListeningExecutorService pool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(concurrency));
// barrier ensures that all jobs try to add files to classpath at same time.
final CyclicBarrier barrier = new CyclicBarrier(concurrency);
final DistributedFileSystem fs = miniCluster.getFileSystem();
final Path expectedJarPath = new Path(finalClasspath, dummyJarFile.getName());
List<ListenableFuture<Boolean>> futures = new ArrayList<>();
for (int i = 0; i < concurrency; i++) {
futures.add(pool.submit(new Callable() {
@Override
public Boolean call() throws Exception {
int id = barrier.await();
Job job = Job.getInstance(conf, "test-job-" + id);
Path intermediatePathForJob = new Path(intermediatePath, "job-" + id);
JobHelper.addJarToClassPath(dummyJarFile, finalClasspath, intermediatePathForJob, fs, job);
// check file gets uploaded to final HDFS path
Assert.assertTrue(fs.exists(expectedJarPath));
// check that the intermediate file is not present
Assert.assertFalse(fs.exists(new Path(intermediatePathForJob, dummyJarFile.getName())));
// check file gets added to the classpath
Assert.assertEquals(expectedJarPath.toString(), job.getConfiguration().get(MRJobConfig.CLASSPATH_FILES));
return true;
}
}));
}
Futures.allAsList(futures).get(30, TimeUnit.SECONDS);
pool.shutdownNow();
}
use of java.util.concurrent.CyclicBarrier in project openhab1-addons by openhab.
the class LightwaveRfBindingFunctionalTest method receiveACommand.
private void receiveACommand(List<ItemConfigAndExpectedState> itemConfigAndExpectedStates, String messageToReceive) throws Exception {
// Set up sockets for testing
CyclicBarrier sendMessageBarrier = new CyclicBarrier(2);
CountDownLatch messageProcessedCountdownLatch = new CountDownLatch(itemConfigAndExpectedStates.size());
doAnswer(sendMessageOnceUnlatched(messageToReceive, sendMessageBarrier)).when(mockReceiveSocket).receive(any(DatagramPacket.class));
doAnswer(waitIndefinitely()).when(mockReceiveSocket2).receive(any(DatagramPacket.class));
doAnswer(waitIndefinitely()).when(mockTransmitSocket).send(any(DatagramPacket.class));
for (ItemConfigAndExpectedState t : itemConfigAndExpectedStates) {
doAnswer(unlatchWhenEventReceivedOrTimeout(messageProcessedCountdownLatch)).when(mockEventPublisher).postUpdate(t.getItem().getName(), t.getExpectedState());
}
// Setup Item config
for (ItemConfigAndExpectedState t : itemConfigAndExpectedStates) {
bindingProvider.processBindingConfiguration(CONTEXT, t.getItem(), t.getItemConfig());
}
binding.addBindingProvider(bindingProvider);
// Activate the binding ready for the test
binding.activateForTesting();
// Receive the command
sendMessageBarrier.await();
// Wait till the receive has been processes we use a timeout in case we don't receive anything
messageProcessedCountdownLatch.await(1000, TimeUnit.MILLISECONDS);
}
use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.
the class ManagedCursorConcurrencyTest method testAckAndClose.
@Test(timeOut = 30000)
public void testAckAndClose() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger_test_ack_and_close", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
final ManagedCursor cursor = ledger.openCursor("c1");
final List<Position> addedEntries = Lists.newArrayList();
for (int i = 0; i < 1000; i++) {
Position pos = ledger.addEntry("entry".getBytes());
addedEntries.add(pos);
}
final CyclicBarrier barrier = new CyclicBarrier(2);
final CountDownLatch counter = new CountDownLatch(2);
final AtomicBoolean gotException = new AtomicBoolean(false);
// Deleter thread
cachedExecutor.execute(() -> {
try {
barrier.await();
for (Position position : addedEntries) {
cursor.asyncDelete(position, deleteCallback, position);
}
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
});
// Reader thread
cachedExecutor.execute(() -> {
try {
barrier.await();
for (int i = 0; i < 1000; i++) {
cursor.readEntries(1).forEach(e -> e.release());
}
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
});
counter.await();
assertEquals(gotException.get(), false);
}
use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.
the class ManagedCursorConcurrencyTest method testMarkDeleteAndRead.
@Test
public void testMarkDeleteAndRead() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
final ManagedCursor cursor = ledger.openCursor("c1");
final List<Position> addedEntries = Lists.newArrayList();
for (int i = 0; i < 1000; i++) {
Position pos = ledger.addEntry("entry".getBytes());
addedEntries.add(pos);
}
final CyclicBarrier barrier = new CyclicBarrier(2);
final CountDownLatch counter = new CountDownLatch(2);
final AtomicBoolean gotException = new AtomicBoolean(false);
Thread deleter = new Thread() {
public void run() {
try {
barrier.await();
for (Position position : addedEntries) {
cursor.markDelete(position);
}
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
}
};
Thread reader = new Thread() {
public void run() {
try {
barrier.await();
for (int i = 0; i < 1000; i++) {
cursor.readEntries(1).forEach(e -> e.release());
}
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
}
};
deleter.start();
reader.start();
counter.await();
assertEquals(gotException.get(), false);
}
use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.
the class ManagedCursorConcurrencyTest method testConcurrentReadOfSameEntry.
@Test(timeOut = 30000)
public void testConcurrentReadOfSameEntry() throws Exception {
ManagedLedger ledger = factory.open("testConcurrentReadOfSameEntry", new ManagedLedgerConfig());
final int numCursors = 5;
final List<ManagedCursor> cursors = Lists.newArrayList();
for (int i = 0; i < numCursors; i++) {
final ManagedCursor cursor = ledger.openCursor("c" + i);
cursors.add(cursor);
}
final int N = 100;
for (int i = 0; i < N; i++) {
ledger.addEntry(("entry" + i).getBytes());
}
long currentLedger = ((PositionImpl) cursors.get(0).getMarkDeletedPosition()).getLedgerId();
// empty the cache
((ManagedLedgerImpl) ledger).entryCache.invalidateAllEntries(currentLedger);
final CyclicBarrier barrier = new CyclicBarrier(numCursors);
final CountDownLatch counter = new CountDownLatch(numCursors);
AtomicReference<String> result = new AtomicReference<String>();
for (int i = 0; i < numCursors; i++) {
final int cursorIndex = i;
final ManagedCursor cursor = cursors.get(cursorIndex);
cachedExecutor.execute(() -> {
try {
barrier.await();
for (int j = 0; j < N; j++) {
String expected = "entry" + j;
String data = new String(cursor.readEntries(1).get(0).getDataAndRelease());
if ((!expected.equals(data)) && result.get() == null) {
result.set("Mismatched entry in cursor " + (cursorIndex + 1) + " at position " + (j + 1) + "--- Expected: " + expected + ", Actual: " + data);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
counter.countDown();
}
});
}
counter.await();
assertNull(result.get());
}
Aggregations