Search in sources :

Example 31 with SuspendableRunnable

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

the class TransformingChannelTest method testFilterFiberToThread.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            Fiber.sleep(100);
            Strand.sleep(50);
            ch.send(1);
            ch.send(2);
            Strand.sleep(50);
            ch.send(3);
            ch.send(4);
            ch.send(5);
            ch.close();
        }
    }).start();
    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()));
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 32 with SuspendableRunnable

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

the class TransformingChannelTest method testMapThreadToFiber.

@Test
public void testMapThreadToFiber() 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.map((ReceivePort<Integer>) ch, new Function<Integer, Integer>() {

                @Override
                public Integer apply(Integer input) {
                    return input + 10;
                }
            });
            Integer m1 = ch1.receive();
            Integer m2 = ch1.receive();
            Integer m3 = ch1.receive();
            Integer m4 = ch1.receive();
            Integer m5 = ch1.receive();
            Integer m6 = ch1.receive();
            assertThat(m1, equalTo(11));
            assertThat(m2, equalTo(12));
            assertThat(m3, equalTo(13));
            assertThat(m4, equalTo(14));
            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 33 with SuspendableRunnable

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

the class TransformingChannelTest method testFiberTransform1.

@Test
public void testFiberTransform1() throws Exception {
    final Channel<Integer> in = newChannel();
    final Channel<Integer> out = newChannel();
    Channels.fiberTransform(in, out, new SuspendableAction2<ReceivePort<Integer>, SendPort<Integer>>() {

        @Override
        public void call(ReceivePort<Integer> in, SendPort<Integer> out) throws SuspendExecution, InterruptedException {
            Integer x;
            while ((x = in.receive()) != null) {
                if (x % 2 == 0)
                    out.send(x * 10);
            }
            out.send(1234);
            out.close();
        }
    });
    Fiber fib1 = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            assertThat(out.receive(), equalTo(20));
            assertThat(out.receive(), equalTo(40));
            assertThat(out.receive(), equalTo(1234));
            assertThat(out.receive(), is(nullValue()));
        }
    }).start();
    Fiber fib2 = new Fiber("fiber", scheduler, new SuspendableRunnable() {

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

Example 34 with SuspendableRunnable

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

the class TransformingChannelTest method testZipThreadToFiber.

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

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

                @Override
                public String apply(String x1, Integer x2) {
                    return x1 + x2;
                }
            });
            String m1 = ch.receive();
            String m2 = ch.receive();
            String m3 = ch.receive();
            String m4 = ch.receive();
            assertThat(m1, equalTo("a1"));
            assertThat(m2, equalTo("b2"));
            assertThat(m3, equalTo("c3"));
            assertThat(m4, is(nullValue()));
        }
    }).start();
    Strand.sleep(50);
    ch1.send("a");
    ch2.send(1);
    ch1.send("b");
    ch2.send(2);
    ch1.send("c");
    ch2.send(3);
    ch1.send("foo");
    ch2.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 35 with SuspendableRunnable

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

the class TransformingChannelTest method testSendFilterFiberToThread.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            Fiber.sleep(100);
            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();
        }
    }).start();
    Integer m1 = ch.receive();
    Integer m2 = ch.receive();
    Integer m3 = ch.receive();
    assertThat(m1, equalTo(2));
    assertThat(m2, equalTo(4));
    assertThat(m3, is(nullValue()));
    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