Search in sources :

Example 66 with Fiber

use of co.paralleluniverse.fibers.Fiber 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 67 with Fiber

use of co.paralleluniverse.fibers.Fiber 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 68 with Fiber

use of co.paralleluniverse.fibers.Fiber 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 69 with Fiber

use of co.paralleluniverse.fibers.Fiber 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)

Example 70 with Fiber

use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.

the class TransformingChannelTest method testSendMapThreadToFiber.

@Test
public void testSendMapThreadToFiber() 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();
            Integer m4 = ch.receive();
            Integer m5 = ch.receive();
            Integer m6 = ch.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();
    SendPort<Integer> ch1 = Channels.mapSend((SendPort<Integer>) ch, new Function<Integer, Integer>() {

        @Override
        public Integer apply(Integer input) {
            return input + 10;
        }
    });
    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)

Aggregations

Fiber (co.paralleluniverse.fibers.Fiber)105 Test (org.junit.Test)88 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)73 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)20 Strand (co.paralleluniverse.strands.Strand)8 IntChannel (co.paralleluniverse.strands.channels.IntChannel)4 QueueCapacityExceededException (co.paralleluniverse.strands.queues.QueueCapacityExceededException)4 ExecutionException (java.util.concurrent.ExecutionException)3 Ignore (org.junit.Ignore)3 Timeout (co.paralleluniverse.strands.Timeout)2 Channel (co.paralleluniverse.strands.channels.Channel)2 ReceivePort (co.paralleluniverse.strands.channels.ReceivePort)2 TimeoutException (java.util.concurrent.TimeoutException)2 ActorRef (co.paralleluniverse.actors.ActorRef)1 AbstractServerHandler (co.paralleluniverse.actors.behaviors.AbstractServerHandler)1 ServerActor (co.paralleluniverse.actors.behaviors.ServerActor)1 Function2 (co.paralleluniverse.common.util.Function2)1 Pair (co.paralleluniverse.common.util.Pair)1 FiberScheduler (co.paralleluniverse.fibers.FiberScheduler)1 FiberWriter (co.paralleluniverse.fibers.FiberWriter)1