use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.
the class ManagedLedgerBkTest method verifyConcurrentUsage.
@Test
public void verifyConcurrentUsage() throws Exception {
ManagedLedgerFactoryConfig config = new ManagedLedgerFactoryConfig();
config.setMaxCacheSize(100 * 1024 * 1024);
ManagedLedgerFactoryImpl factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle(), config);
EntryCacheManager cacheManager = factory.getEntryCacheManager();
ManagedLedgerConfig conf = new ManagedLedgerConfig();
conf.setEnsembleSize(2).setAckQuorumSize(2).setMetadataEnsembleSize(2);
final ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my-ledger", conf);
int NumProducers = 1;
int NumConsumers = 1;
final AtomicBoolean done = new AtomicBoolean();
final CyclicBarrier barrier = new CyclicBarrier(NumProducers + NumConsumers + 1);
List<Future<?>> futures = Lists.newArrayList();
for (int i = 0; i < NumProducers; i++) {
futures.add(executor.submit(() -> {
try {
barrier.await();
while (!done.get()) {
ledger.addEntry("entry".getBytes());
Thread.sleep(1);
}
} catch (Exception e) {
e.printStackTrace();
}
}));
}
for (int i = 0; i < NumConsumers; i++) {
final int idx = i;
futures.add(executor.submit(() -> {
try {
barrier.await();
ManagedCursor cursor = ledger.openCursor("my-cursor-" + idx);
while (!done.get()) {
List<Entry> entries = cursor.readEntries(1);
if (!entries.isEmpty()) {
cursor.markDelete(entries.get(0).getPosition());
}
entries.forEach(e -> e.release());
Thread.sleep(2);
}
} catch (Exception e) {
e.printStackTrace();
}
}));
}
barrier.await();
Thread.sleep(1 * 1000);
done.set(true);
for (Future<?> future : futures) {
future.get();
}
cacheManager.mlFactoryMBean.refreshStats(1, TimeUnit.SECONDS);
assertTrue(cacheManager.mlFactoryMBean.getCacheHitsRate() > 0.0);
assertEquals(cacheManager.mlFactoryMBean.getCacheMissesRate(), 0.0);
assertTrue(cacheManager.mlFactoryMBean.getCacheHitsThroughput() > 0.0);
assertEquals(cacheManager.mlFactoryMBean.getNumberOfCacheEvictions(), 0);
factory.shutdown();
}
use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.
the class ManagedLedgerTest method testConcurrentOpenCursor.
@Test
public void testConcurrentOpenCursor() throws Exception {
ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("testConcurrentOpenCursor");
final AtomicReference<ManagedCursor> cursor1 = new AtomicReference<>(null);
final AtomicReference<ManagedCursor> cursor2 = new AtomicReference<>(null);
final CyclicBarrier barrier = new CyclicBarrier(2);
final CountDownLatch latch = new CountDownLatch(2);
cachedExecutor.execute(() -> {
try {
barrier.await();
} catch (Exception e) {
}
ledger.asyncOpenCursor("c1", new OpenCursorCallback() {
@Override
public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
latch.countDown();
}
@Override
public void openCursorComplete(ManagedCursor cursor, Object ctx) {
cursor1.set(cursor);
latch.countDown();
}
}, null);
});
cachedExecutor.execute(() -> {
try {
barrier.await();
} catch (Exception e) {
}
ledger.asyncOpenCursor("c1", new OpenCursorCallback() {
@Override
public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
latch.countDown();
}
@Override
public void openCursorComplete(ManagedCursor cursor, Object ctx) {
cursor2.set(cursor);
latch.countDown();
}
}, null);
});
latch.await();
assertNotNull(cursor1.get());
assertNotNull(cursor2.get());
assertEquals(cursor1.get(), cursor2.get());
ledger.close();
}
use of java.util.concurrent.CyclicBarrier in project jdk8u_jdk by JetBrains.
the class SiblingIOEHandle method performA.
private static void performA(boolean fileOut) {
try {
stopC.delete();
ProcessBuilder builderB = new ProcessBuilder(getCommandArray(APP.B.name()));
File outB = null;
if (fileOut) {
outB = new File("outB.txt");
builderB.redirectOutput(outB);
}
builderB.redirectErrorStream(true);
final CyclicBarrier barrier = new CyclicBarrier(2);
//Create process C in a new thread
ProcessC processC = new ProcessC(barrier);
Thread procCRunner = new Thread(processC);
procCRunner.start();
if (!waitBarrier(barrier)) {
throw new RuntimeException("Catastrophe in process A! Synchronization failed.");
}
// Run process B first.
Process processB = builderB.start();
while (true) try {
procCRunner.join();
break;
} catch (InterruptedException ex) {
continue;
}
if (!procClaunched) {
throw new RuntimeException("Catastrophe in process A! C was not launched.");
}
processB.getOutputStream().close();
processB.getErrorStream().close();
if (fileOut) {
try {
processB.waitFor();
} catch (InterruptedException ex) {
throw new RuntimeException("Catastrophe in process B! B hung up.");
}
System.err.println("Trying to delete [outB.txt].");
if (!outB.delete()) {
throw new RuntimeException("Greedy brother C deadlock! File share.");
}
System.err.println("Succeeded in delete [outB.txt].");
} else {
System.err.println("Read stream start.");
boolean isSignalReceived = false;
try (BufferedReader in = new BufferedReader(new InputStreamReader(processB.getInputStream(), "utf-8"))) {
String result;
while ((result = in.readLine()) != null) {
if (SIGNAL.equals(result)) {
isSignalReceived = true;
} else {
throw new RuntimeException("Catastrophe in process B! Bad output.");
}
}
}
if (!isSignalReceived) {
throw new RuntimeException("Signal from B was not received");
}
System.err.println("Read stream finished.");
}
// If JDK-6921885 is not fixed that point is unreachable.
// Test timeout exception.
// write signal file to stop C process.
stopC.createNewFile();
processC.waitFor();
} catch (IOException ex) {
throw new RuntimeException("Catastrophe in process A!", ex);
} catch (InterruptedException ex) {
throw new RuntimeException("Process A was interrupted while waiting for C", ex);
}
}
use of java.util.concurrent.CyclicBarrier in project jdk8u_jdk by JetBrains.
the class ChatTest method performTestUseName.
private static void performTestUseName() throws Exception {
final CyclicBarrier barrier1 = new CyclicBarrier(2);
final CyclicBarrier barrier2 = new CyclicBarrier(2);
final CyclicBarrier barrier3 = new CyclicBarrier(2);
final List<Exception> exceptions = new ArrayList<Exception>();
ChatConnection chatConnection = new ChatConnection() {
@Override
public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception {
String string = readAvailableString(reader);
if (!"Name: ".equals(string)) {
exceptions.add(new RuntimeException("Expected Name: "));
}
writer.write("testClient1\n");
waitForJoin(reader, "testClient1");
barrier1.await();
barrier2.await();
string = readAvailableString(reader);
if (!"testClient2: Hello world!\n".equals(string)) {
exceptions.add(new RuntimeException("testClient2: Hello world!\n"));
}
barrier3.await();
}
};
Thread client2 = new Thread(new ChatConnection() {
@Override
public void run(Socket socket, BufferedReader reader, Writer writer) throws Exception {
String string = readAvailableString(reader);
if (!"Name: ".equals(string)) {
exceptions.add(new RuntimeException("Expected Name: "));
}
writer.write("testClient2\n");
waitForJoin(reader, "testClient2");
barrier1.await();
writer.write("Hello world!\n");
barrier2.await();
barrier3.await();
}
});
client2.start();
chatConnection.run();
if (!exceptions.isEmpty()) {
throw exceptions.get(0);
}
}
use of java.util.concurrent.CyclicBarrier in project android by JetBrains.
the class RenderSecurityManagerTest method testThread2.
@Test
public void testThread2() throws Exception {
// Check that when a new thread is created simultaneously from an unrelated
// thread during rendering, that new thread does not pick up the security manager.
//
final CyclicBarrier barrier1 = new CyclicBarrier(2);
final CyclicBarrier barrier2 = new CyclicBarrier(2);
final CyclicBarrier barrier3 = new CyclicBarrier(4);
final CyclicBarrier barrier4 = new CyclicBarrier(4);
final CyclicBarrier barrier5 = new CyclicBarrier(4);
final CyclicBarrier barrier6 = new CyclicBarrier(4);
// First the threads reach barrier1. Then from barrier1 to barrier2, thread1
// installs the security manager. Then from barrier2 to barrier3, thread2
// checks that it does not have any security restrictions, and creates thread3.
// Thread1 will ensure that the security manager is working there, and it will
// create thread4. Then after barrier3 (where thread3 and thread4 are now also
// participating) thread3 will ensure that it too has no security restrictions,
// and thread4 will ensure that it does. At barrier4 the security manager gets
// uninstalled, and at barrier5 all threads will check that there are no more
// restrictions. At barrier6 all threads are done.
final Thread thread1 = new Thread("render") {
@Override
public void run() {
try {
barrier1.await();
assertNull(RenderSecurityManager.getCurrent());
RenderSecurityManager manager = new RenderSecurityManager(null, null);
manager.setActive(true, myCredential);
barrier2.await();
Thread thread4 = new Thread() {
@Override
public void run() {
try {
barrier3.await();
try {
System.getProperties();
fail("Should have thrown security exception");
} catch (SecurityException e) {
// pass
}
barrier4.await();
barrier5.await();
assertNull(RenderSecurityManager.getCurrent());
assertNull(System.getSecurityManager());
barrier6.await();
} catch (InterruptedException e) {
fail(e.toString());
} catch (BrokenBarrierException e) {
fail(e.toString());
}
}
};
thread4.start();
try {
System.getProperties();
fail("Should have thrown security exception");
} catch (SecurityException e) {
// expected
}
barrier3.await();
barrier4.await();
manager.dispose(myCredential);
assertNull(RenderSecurityManager.getCurrent());
assertNull(System.getSecurityManager());
barrier5.await();
barrier6.await();
} catch (InterruptedException e) {
fail(e.toString());
} catch (BrokenBarrierException e) {
fail(e.toString());
}
}
};
final Thread thread2 = new Thread("unrelated") {
@Override
public void run() {
try {
barrier1.await();
assertNull(RenderSecurityManager.getCurrent());
barrier2.await();
assertNull(RenderSecurityManager.getCurrent());
assertNotNull(System.getSecurityManager());
try {
System.getProperties();
} catch (SecurityException e) {
fail("Should not have been affected by security manager");
}
Thread thread3 = new Thread() {
@Override
public void run() {
try {
barrier3.await();
try {
System.getProperties();
} catch (SecurityException e) {
fail("Should not have been affected by security manager");
}
barrier4.await();
barrier5.await();
assertNull(RenderSecurityManager.getCurrent());
assertNull(System.getSecurityManager());
barrier6.await();
} catch (InterruptedException e) {
fail(e.toString());
} catch (BrokenBarrierException e) {
fail(e.toString());
}
}
};
thread3.start();
barrier3.await();
barrier4.await();
barrier5.await();
assertNull(RenderSecurityManager.getCurrent());
assertNull(System.getSecurityManager());
barrier6.await();
} catch (InterruptedException e) {
fail(e.toString());
} catch (BrokenBarrierException e) {
fail(e.toString());
}
}
};
thread1.start();
thread2.start();
thread1.join();
thread2.join();
}
Aggregations