Search in sources :

Example 81 with CyclicBarrier

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();
}
Also used : CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 82 with CyclicBarrier

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();
}
Also used : CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 83 with CyclicBarrier

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ImmutableMap(com.google.common.collect.ImmutableMap) URLConnection(java.net.URLConnection) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 84 with CyclicBarrier

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();
}
Also used : InterruptedIOException(java.io.InterruptedIOException) ImmutableMap(com.google.common.collect.ImmutableMap) URLConnection(java.net.URLConnection) InterruptedIOException(java.io.InterruptedIOException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 85 with CyclicBarrier

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));
        }
    }
}
Also used : ServerSocket(java.net.ServerSocket) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) URL(java.net.URL) CyclicBarrier(java.util.concurrent.CyclicBarrier) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

CyclicBarrier (java.util.concurrent.CyclicBarrier)650 Test (org.junit.Test)315 CountDownLatch (java.util.concurrent.CountDownLatch)169 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)142 ArrayList (java.util.ArrayList)126 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)124 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)121 IOException (java.io.IOException)97 ExecutorService (java.util.concurrent.ExecutorService)84 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)67 AtomicReference (java.util.concurrent.atomic.AtomicReference)66 Ignite (org.apache.ignite.Ignite)64 List (java.util.List)53 Test (org.testng.annotations.Test)52 TimeoutException (java.util.concurrent.TimeoutException)48 Transaction (org.apache.ignite.transactions.Transaction)48 IgniteException (org.apache.ignite.IgniteException)47 ExecutionException (java.util.concurrent.ExecutionException)40 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)40 IgniteCache (org.apache.ignite.IgniteCache)37