Search in sources :

Example 41 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class ChannelTest method whenChannelClosedExceptionThenBlockedSendsComplete.

@Test
public void whenChannelClosedExceptionThenBlockedSendsComplete() throws Exception {
    assumeThat(policy, is(OverflowPolicy.BLOCK));
    final Channel<Integer> ch = newChannel();
    final SuspendableRunnable r = new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            for (int i = 1; i <= 100; i++) {
                ch.send(i);
            }
        }
    };
    Fiber fib1 = new Fiber("fiber", scheduler, r).start();
    Fiber fib2 = new Fiber("fiber", scheduler, r).start();
    Thread.sleep(500);
    ch.close(new Exception("foo"));
    fib1.join();
    fib2.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) QueueCapacityExceededException(co.paralleluniverse.strands.queues.QueueCapacityExceededException) Test(org.junit.Test)

Example 42 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class ChannelTest method testChannelCloseWithSleep.

@Test
public void testChannelCloseWithSleep() throws Exception {
    final Channel<Integer> ch = newChannel();
    Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            for (int i = 1; i <= 5; i++) {
                Integer m = ch.receive();
                assertThat(m, equalTo(i));
            }
            Integer m = ch.receive();
            assertThat(m, nullValue());
            assertTrue(ch.isClosed());
        }
    }).start();
    Thread.sleep(50);
    ch.send(1);
    ch.send(2);
    ch.send(3);
    ch.send(4);
    ch.send(5);
    Thread.sleep(50);
    ch.close();
    ch.send(6);
    ch.send(7);
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 43 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class ChannelTest method testPrimitiveChannelCloseException.

@Test
public void testPrimitiveChannelCloseException() throws Exception {
    assumeThat(mailboxSize, not(equalTo(0)));
    final IntChannel ch = Channels.newIntChannel(mailboxSize, policy);
    Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            try {
                for (int i = 1; i <= 5; i++) {
                    int m = ch.receiveInt();
                    assertThat(m, is(i));
                }
            } catch (QueueChannel.EOFException e) {
                fail();
            }
            try {
                int m = ch.receiveInt();
                fail("m = " + m);
            } catch (ProducerException e) {
                assertThat(e.getCause().getMessage(), equalTo("foo"));
            } catch (ReceivePort.EOFException e) {
                fail();
            }
            assertTrue(ch.isClosed());
        }
    }).start();
    Thread.sleep(50);
    ch.send(1);
    ch.send(2);
    ch.send(3);
    ch.send(4);
    ch.send(5);
    ch.close(new Exception("foo"));
    ch.send(6);
    ch.send(7);
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) QueueCapacityExceededException(co.paralleluniverse.strands.queues.QueueCapacityExceededException) Test(org.junit.Test)

Example 44 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class ChannelTest method whenReceiveNotCalledFromOwnerThenThrowException3.

@Ignore
@Test
public void whenReceiveNotCalledFromOwnerThenThrowException3() throws Exception {
    assumeTrue(Debug.isAssertionsEnabled());
    final Channel<String> ch = newChannel();
    Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            Fiber.sleep(100);
            ch.send("a message");
            boolean thrown = false;
            try {
                ch.receive();
            } catch (Throwable e) {
                thrown = true;
            }
            assertTrue(thrown);
        }
    }).start();
    String m = ch.receive();
    assertThat(m, equalTo("a message"));
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 45 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class ChannelTest method testChannelClose.

@Test
public void testChannelClose() throws Exception {
    final Channel<Integer> ch = newChannel();
    Fiber fib = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            for (int i = 1; i <= 5; i++) {
                Integer m = ch.receive();
                assertThat(m, equalTo(i));
            }
            Integer m = ch.receive();
            assertThat(m, nullValue());
            assertTrue(ch.isClosed());
        }
    }).start();
    Thread.sleep(50);
    ch.send(1);
    ch.send(2);
    ch.send(3);
    ch.send(4);
    ch.send(5);
    ch.close();
    ch.send(6);
    ch.send(7);
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Aggregations

SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)90 Test (org.junit.Test)82 Fiber (co.paralleluniverse.fibers.Fiber)73 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Strand (co.paralleluniverse.strands.Strand)4 QueueCapacityExceededException (co.paralleluniverse.strands.queues.QueueCapacityExceededException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Ignore (org.junit.Ignore)3 Condition (co.paralleluniverse.strands.Condition)2 SimpleConditionSynchronizer (co.paralleluniverse.strands.SimpleConditionSynchronizer)2 Timeout (co.paralleluniverse.strands.Timeout)2 Channel (co.paralleluniverse.strands.channels.Channel)2 IntChannel (co.paralleluniverse.strands.channels.IntChannel)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CheckedCallable (co.paralleluniverse.common.util.CheckedCallable)1 Function2 (co.paralleluniverse.common.util.Function2)1 Pair (co.paralleluniverse.common.util.Pair)1 VerifyInstrumentationException (co.paralleluniverse.fibers.VerifyInstrumentationException)1