Search in sources :

Example 61 with SuspendableRunnable

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

the class ChannelTest method testChannelCloseException.

@Test
public void testChannelCloseException() 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));
            }
            try {
                ch.receive();
                fail();
            } catch (ProducerException e) {
                assertThat(e.getCause().getMessage(), equalTo("foo"));
            }
            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 62 with SuspendableRunnable

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

the class TransformingChannelTest method testSendFilterThreadToFiber.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            Integer m1 = ch.receive();
            Integer m2 = ch.receive();
            Integer m3 = ch.receive();
            assertThat(m1, equalTo(2));
            assertThat(m2, equalTo(4));
            assertThat(m3, is(nullValue()));
        }
    }).start();
    SendPort<Integer> ch1 = Channels.filterSend((SendPort<Integer>) ch, new Predicate<Integer>() {

        @Override
        public boolean apply(Integer input) {
            return input % 2 == 0;
        }
    });
    Strand.sleep(50);
    ch1.send(1);
    ch1.send(2);
    Strand.sleep(50);
    ch1.send(3);
    ch1.send(4);
    ch1.send(5);
    ch1.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 63 with SuspendableRunnable

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

the class TransformingChannelTest method testReduceThreadToFiber.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            final ReceivePort<Integer> ch1 = Channels.reduce((ReceivePort<Integer>) ch, new Function2<Integer, Integer, Integer>() {

                @Override
                public Integer apply(Integer accum, Integer input) {
                    return accum + input;
                }
            }, 0);
            final Integer m1 = ch1.receive();
            final Integer m2 = ch1.receive();
            final Integer m3 = ch1.receive();
            final Integer m4 = ch1.receive();
            final Integer m5 = ch1.receive();
            final Integer m6 = ch1.receive();
            assertThat(m1, equalTo(1));
            assertThat(m2, equalTo(3));
            assertThat(m3, equalTo(6));
            assertThat(m4, equalTo(10));
            assertThat(m5, equalTo(15));
            assertThat(m6, is(nullValue()));
        }
    }).start();
    Strand.sleep(50);
    ch.send(1);
    ch.send(2);
    Strand.sleep(50);
    ch.send(3);
    ch.send(4);
    ch.send(5);
    ch.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 64 with SuspendableRunnable

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

the class TransformingChannelTest method testFilterFiberToFiber.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            ReceivePort<Integer> ch1 = Channels.filter((ReceivePort<Integer>) ch, new Predicate<Integer>() {

                @Override
                public boolean apply(Integer input) {
                    return input % 2 == 0;
                }
            });
            Integer m1 = ch1.receive();
            Integer m2 = ch1.receive();
            Integer m3 = ch1.receive();
            assertThat(m1, equalTo(2));
            assertThat(m2, equalTo(4));
            assertThat(m3, is(nullValue()));
        }
    }).start();
    Fiber fib2 = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            Strand.sleep(50);
            ch.send(1);
            ch.send(2);
            Strand.sleep(50);
            ch.send(3);
            ch.send(4);
            ch.send(5);
            ch.close();
        }
    }).start();
    fib1.join();
    fib2.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 65 with SuspendableRunnable

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

the class TransformingChannelTest method testFilterThreadToFiber.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            ReceivePort<Integer> ch1 = Channels.filter((ReceivePort<Integer>) ch, new Predicate<Integer>() {

                @Override
                public boolean apply(Integer input) {
                    return input % 2 == 0;
                }
            });
            Integer m1 = ch1.receive();
            Integer m2 = ch1.receive();
            Integer m3 = ch1.receive();
            assertThat(m1, equalTo(2));
            assertThat(m2, equalTo(4));
            assertThat(m3, is(nullValue()));
        }
    }).start();
    Strand.sleep(50);
    ch.send(1);
    ch.send(2);
    Strand.sleep(50);
    ch.send(3);
    ch.send(4);
    ch.send(5);
    ch.close();
    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