Search in sources :

Example 26 with Fiber

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

the class TransformingChannelTest method testSendSplitThreadToFiber.

@Test
public void testSendSplitThreadToFiber() throws Exception {
    final Channel<String> chF1 = newChannel();
    final Channel<String> chF2 = newChannel();
    final SendPort<String> splitSP = new SplitSendPort<String>() {

        @Override
        protected SendPort select(final String m) {
            if (m.equals("f1"))
                return chF1;
            else
                return chF2;
        }
    };
    final Fiber f1 = new Fiber("split-send-1", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            assertThat(chF1.receive(), is("f1"));
            assertThat(chF1.receive(100, TimeUnit.NANOSECONDS), is(nullValue()));
            assertThat(chF1.receive(new Timeout(100, TimeUnit.NANOSECONDS)), is(nullValue()));
            assertThat(chF1.receive(), is(nullValue()));
            assertTrue(chF1.isClosed());
        }
    }).start();
    final Fiber f2 = new Fiber("split-send-2", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            assertThat(chF2.receive(), is(not("f1")));
            assertThat(chF2.receive(100, TimeUnit.NANOSECONDS), is(nullValue()));
            assertThat(chF2.receive(new Timeout(100, TimeUnit.NANOSECONDS)), is(nullValue()));
            assertThat(chF2.receive(), is(nullValue()));
            assertTrue(chF2.isClosed());
        }
    }).start();
    splitSP.send("f1");
    splitSP.send("f2");
    splitSP.close();
    assertFalse(chF1.isClosed());
    assertFalse(chF2.isClosed());
    Thread.sleep(100);
    chF1.close();
    chF2.close();
    f1.join();
    f2.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Timeout(co.paralleluniverse.strands.Timeout) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 27 with Fiber

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

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

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

the class TransformingChannelTest method testFlatmapSendWithTimeoutsThreadToFiber.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            assertThat(ch.receive(), is(1));
            assertThat(ch.receive(30, TimeUnit.MILLISECONDS), is(nullValue()));
            assertThat(ch.receive(40, TimeUnit.MILLISECONDS), is(20));
            assertThat(ch.receive(), is(200));
            assertThat(ch.receive(), is(2000));
            assertThat(ch.receive(), is(40));
            assertThat(ch.receive(), is(400));
            assertThat(ch.receive(), is(4000));
            assertThat(ch.receive(30, TimeUnit.MILLISECONDS), is(nullValue()));
            assertThat(ch.receive(40, TimeUnit.MILLISECONDS), is(5));
            assertThat(ch.receive(), is(nullValue()));
            assertThat(ch.isClosed(), is(true));
        }
    }).start();
    SendPort<Integer> ch1 = Channels.flatMapSend(Channels.<Integer>newChannel(1), ch, new Function<Integer, ReceivePort<Integer>>() {

        @Override
        public ReceivePort<Integer> apply(Integer x) {
            if (x == 3)
                return null;
            if (x % 2 == 0)
                return Channels.toReceivePort(Arrays.asList(new Integer[] { x * 10, x * 100, x * 1000 }));
            else
                return Channels.singletonReceivePort(x);
        }
    });
    Strand.sleep(50);
    ch1.send(1);
    Strand.sleep(50);
    ch1.send(2);
    ch1.send(3);
    ch1.send(4);
    Strand.sleep(50);
    ch1.send(5);
    ch1.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber)

Example 30 with Fiber

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

Aggregations

Fiber (co.paralleluniverse.fibers.Fiber)103 Test (org.junit.Test)86 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)73 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)19 Strand (co.paralleluniverse.strands.Strand)8 QueueCapacityExceededException (co.paralleluniverse.strands.queues.QueueCapacityExceededException)4 IntChannel (co.paralleluniverse.strands.channels.IntChannel)3 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 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 VerifyInstrumentationException (co.paralleluniverse.fibers.VerifyInstrumentationException)1