use of java.util.concurrent.CyclicBarrier in project robovm by robovm.
the class ThreadsTest method test_parkFor_3.
/** Test the case where the thread is preemptively unparked. */
public void test_parkFor_3() throws Exception {
CyclicBarrier barrier = new CyclicBarrier(1);
Parker parker = new Parker(barrier, false, 1000);
Thread parkerThread = new Thread(parker);
UNSAFE.unpark(parkerThread);
parkerThread.start();
parker.assertDurationIsInRange(0);
parkerThread.join();
}
use of java.util.concurrent.CyclicBarrier in project robovm by robovm.
the class ThreadsTest method test_parkUntil_2.
/** Test the case where the unpark happens before the timeout. */
public void test_parkUntil_2() throws Exception {
CyclicBarrier barrier = new CyclicBarrier(2);
Parker parker = new Parker(barrier, true, 1000);
Thread parkerThread = new Thread(parker);
Thread waiterThread = new Thread(new WaitAndUnpark(barrier, 300, parkerThread));
parkerThread.start();
waiterThread.start();
parker.assertDurationIsInRange(300);
waiterThread.join();
parkerThread.join();
}
use of java.util.concurrent.CyclicBarrier in project bazel by bazelbuild.
the class HttpConnectorMultiplexerTest method twoSuccessfulUrlsAndFirstWins_returnsFirstAndInterruptsSecond.
@Test
public void twoSuccessfulUrlsAndFirstWins_returnsFirstAndInterruptsSecond() throws Exception {
final CyclicBarrier barrier = new CyclicBarrier(2);
final AtomicBoolean wasInterrupted = new AtomicBoolean(true);
when(connector.connect(eq(URL1), any(ImmutableMap.class))).thenAnswer(new Answer<URLConnection>() {
@Override
public URLConnection answer(InvocationOnMock invocation) throws Throwable {
barrier.await();
return connection1;
}
});
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
barrier.await();
TimeUnit.MILLISECONDS.sleep(10000);
wasInterrupted.set(false);
return null;
}
}).when(sleeper).sleepMillis(anyLong());
assertThat(toByteArray(multiplexer.connect(asList(URL1, URL2), "abc"))).isEqualTo(data1);
assertThat(wasInterrupted.get()).isTrue();
}
use of java.util.concurrent.CyclicBarrier in project bazel by bazelbuild.
the class HttpConnectorMultiplexerTest method parentThreadGetsInterrupted_interruptsChildrenThenThrowsIe.
@Test
public void parentThreadGetsInterrupted_interruptsChildrenThenThrowsIe() throws Exception {
final CyclicBarrier barrier = new CyclicBarrier(3);
final AtomicBoolean wasInterrupted1 = new AtomicBoolean(true);
final AtomicBoolean wasInterrupted2 = new AtomicBoolean(true);
final AtomicBoolean wasInterrupted3 = new AtomicBoolean(true);
when(connector.connect(eq(URL1), any(ImmutableMap.class))).thenAnswer(new Answer<URLConnection>() {
@Override
public URLConnection answer(InvocationOnMock invocation) throws Throwable {
barrier.await();
TimeUnit.MILLISECONDS.sleep(10000);
wasInterrupted1.set(false);
throw new RuntimeException();
}
});
when(connector.connect(eq(URL2), any(ImmutableMap.class))).thenAnswer(new Answer<URLConnection>() {
@Override
public URLConnection answer(InvocationOnMock invocation) throws Throwable {
barrier.await();
TimeUnit.MILLISECONDS.sleep(10000);
wasInterrupted2.set(false);
throw new RuntimeException();
}
});
Thread task = new Thread(new Runnable() {
@Override
public void run() {
try {
multiplexer.connect(asList(URL1, URL2), "");
} catch (InterruptedIOException ignored) {
return;
} catch (Exception ignored) {
// ignored
}
wasInterrupted3.set(false);
}
});
task.start();
barrier.await();
task.interrupt();
task.join();
assertThat(wasInterrupted1.get()).isTrue();
assertThat(wasInterrupted2.get()).isTrue();
assertThat(wasInterrupted3.get()).isTrue();
}
use of java.util.concurrent.CyclicBarrier in project bazel by bazelbuild.
the class HttpConnectorMultiplexerIntegrationTest method captivePortal_isAvoided.
@Test
public void captivePortal_isAvoided() throws Exception {
final CyclicBarrier barrier = new CyclicBarrier(2);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
barrier.await();
return null;
}
}).when(sleeper).sleepMillis(anyLong());
try (final ServerSocket server1 = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"));
final ServerSocket server2 = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) {
@SuppressWarnings("unused") Future<?> possiblyIgnoredError = executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
try (Socket socket = server1.accept()) {
readHttpRequest(socket.getInputStream());
sendLines(socket, "HTTP/1.1 200 OK", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Warning: https://youtu.be/rJ6O5sTPn1k", "Connection: close", "", "Und wird die Welt auch in Flammen stehen", "Wir werden wieder auferstehen");
}
barrier.await();
return null;
}
});
@SuppressWarnings("unused") Future<?> possiblyIgnoredError1 = executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
try (Socket socket = server2.accept()) {
readHttpRequest(socket.getInputStream());
sendLines(socket, "HTTP/1.1 200 OK", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "", "hello");
}
return null;
}
});
try (HttpStream stream = multiplexer.connect(ImmutableList.of(new URL(String.format("http://127.0.0.1:%d", server1.getLocalPort())), new URL(String.format("http://127.0.0.1:%d", server2.getLocalPort()))), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")) {
assertThat(toByteArray(stream)).isEqualTo("hello".getBytes(US_ASCII));
}
}
}
Aggregations