use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.
the class DistributedIdGeneratorTest method concurrent.
/**
* Use multiple threads to generate many Id. Ensure no holes and no dups in the sequence
*/
@Test
public void concurrent() throws Exception {
int Threads = 10;
int Iterations = 100;
CyclicBarrier barrier = new CyclicBarrier(Threads);
CountDownLatch counter = new CountDownLatch(Threads);
ExecutorService executor = Executors.newCachedThreadPool();
List<String> results = Collections.synchronizedList(Lists.newArrayList());
for (int i = 0; i < Threads; i++) {
executor.execute(() -> {
try {
DistributedIdGenerator gen = new DistributedIdGenerator(zkc, "/my/test/concurrent", "prefix");
barrier.await();
for (int j = 0; j < Iterations; j++) {
results.add(gen.getNextId());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
counter.countDown();
}
});
}
counter.await();
assertEquals(results.size(), Threads * Iterations);
// Check the list contains no duplicates
Set<String> set = Sets.newHashSet(results);
assertEquals(set.size(), results.size());
executor.shutdown();
}
use of java.util.concurrent.CyclicBarrier in project hazelcast by hazelcast.
the class OperationExecutorImpl_BasicTest method test_interruptAllPartitionThreads.
@Test
public void test_interruptAllPartitionThreads() throws Exception {
initExecutor();
int threadCount = executor.getPartitionThreadCount();
final CyclicBarrier barrier = new CyclicBarrier(threadCount + 1);
executor.executeOnPartitionThreads(new Runnable() {
@Override
public void run() {
// current thread must be a PartitionOperationThread
if (Thread.currentThread() instanceof PartitionOperationThread) {
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException ignored) {
} finally {
try {
awaitBarrier(barrier);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
});
executor.interruptPartitionThreads();
awaitBarrier(barrier);
}
use of java.util.concurrent.CyclicBarrier in project hadoop by apache.
the class TestRPC method testRPCInterrupted.
@Test(timeout = 30000)
public void testRPCInterrupted() throws Exception {
Server server;
RPC.Builder builder = newServerBuilder(conf).setNumHandlers(5).setVerbose(true).setSecretManager(null);
server = setupTestServer(builder);
int numConcurrentRPC = 200;
final CyclicBarrier barrier = new CyclicBarrier(numConcurrentRPC);
final CountDownLatch latch = new CountDownLatch(numConcurrentRPC);
final AtomicBoolean leaderRunning = new AtomicBoolean(true);
final AtomicReference<Throwable> error = new AtomicReference<>();
Thread leaderThread = null;
try {
for (int i = 0; i < numConcurrentRPC; i++) {
final int num = i;
final TestRpcService proxy = getClient(addr, conf);
Thread rpcThread = new Thread(new Runnable() {
@Override
public void run() {
try {
barrier.await();
while (num == 0 || leaderRunning.get()) {
proxy.slowPing(null, newSlowPingRequest(false));
}
proxy.slowPing(null, newSlowPingRequest(false));
} catch (Exception e) {
if (num == 0) {
leaderRunning.set(false);
} else {
error.set(e);
}
LOG.error("thread " + num, e);
} finally {
latch.countDown();
}
}
});
rpcThread.start();
if (leaderThread == null) {
leaderThread = rpcThread;
}
}
// let threads get past the barrier
Thread.sleep(1000);
// stop a single thread
while (leaderRunning.get()) {
leaderThread.interrupt();
}
latch.await();
// should not cause any other thread to get an error
assertTrue("rpc got exception " + error.get(), error.get() == null);
} finally {
server.stop();
}
}
use of java.util.concurrent.CyclicBarrier in project hadoop by apache.
the class TestRPC method testExternalCall.
@Test(timeout = 30000)
public void testExternalCall() throws Exception {
final UserGroupInformation ugi = UserGroupInformation.createUserForTesting("user123", new String[0]);
final IOException expectedIOE = new IOException("boom");
// use 1 handler so the callq can be plugged
final Server server = setupTestServer(conf, 1);
try {
final AtomicBoolean result = new AtomicBoolean();
ExternalCall<String> remoteUserCall = newExtCall(ugi, new PrivilegedExceptionAction<String>() {
@Override
public String run() throws Exception {
return UserGroupInformation.getCurrentUser().getUserName();
}
});
ExternalCall<String> exceptionCall = newExtCall(ugi, new PrivilegedExceptionAction<String>() {
@Override
public String run() throws Exception {
throw expectedIOE;
}
});
final CountDownLatch latch = new CountDownLatch(1);
final CyclicBarrier barrier = new CyclicBarrier(2);
ExternalCall<Void> barrierCall = newExtCall(ugi, new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
// notify we are in a handler and then wait to keep the callq
// plugged up
latch.countDown();
barrier.await();
return null;
}
});
server.queueCall(barrierCall);
server.queueCall(exceptionCall);
server.queueCall(remoteUserCall);
// wait for barrier call to enter the handler, check that the other 2
// calls are actually queued
latch.await();
assertEquals(2, server.getCallQueueLen());
// unplug the callq
barrier.await();
barrierCall.get();
// verify correct ugi is used
String answer = remoteUserCall.get();
assertEquals(ugi.getUserName(), answer);
try {
exceptionCall.get();
fail("didn't throw");
} catch (ExecutionException ee) {
assertTrue((ee.getCause()) instanceof IOException);
assertEquals(expectedIOE.getMessage(), ee.getCause().getMessage());
}
} finally {
server.stop();
}
}
use of java.util.concurrent.CyclicBarrier in project questdb by bluestreak01.
the class HttpServerTest method assertConcurrentDownload.
private void assertConcurrentDownload(MimeTypes mimeTypes, HttpServer server, final String proto) throws InterruptedException, IOException {
try {
// ssl
final File actual1 = new File(temp.getRoot(), "get.html");
final File actual2 = new File(temp.getRoot(), "post.html");
final File actual3 = new File(temp.getRoot(), "upload.html");
final CyclicBarrier barrier = new CyclicBarrier(3);
final CountDownLatch haltLatch = new CountDownLatch(3);
final AtomicInteger counter = new AtomicInteger(0);
new Thread(() -> {
try {
barrier.await();
HttpTestUtils.download(clientBuilder("https".equals(proto)), proto + "://localhost:9000/get.html", actual1);
} catch (Exception e) {
counter.incrementAndGet();
e.printStackTrace();
} finally {
haltLatch.countDown();
}
}).start();
new Thread(() -> {
try {
barrier.await();
HttpTestUtils.download(clientBuilder("https".equals(proto)), proto + "://localhost:9000/post.html", actual2);
} catch (Exception e) {
counter.incrementAndGet();
e.printStackTrace();
} finally {
haltLatch.countDown();
}
}).start();
new Thread(() -> {
try {
barrier.await();
HttpTestUtils.download(clientBuilder("https".equals(proto)), proto + "://localhost:9000/upload.html", actual3);
} catch (Exception e) {
counter.incrementAndGet();
e.printStackTrace();
} finally {
haltLatch.countDown();
}
}).start();
haltLatch.await();
Assert.assertEquals(0, counter.get());
TestUtils.assertEquals(new File(HttpServerTest.class.getResource("/site/public/get.html").getPath()), actual1);
TestUtils.assertEquals(new File(HttpServerTest.class.getResource("/site/public/post.html").getPath()), actual2);
TestUtils.assertEquals(new File(HttpServerTest.class.getResource("/site/public/upload.html").getPath()), actual3);
} finally {
server.halt();
}
}
Aggregations