Search in sources :

Example 11 with Fiber

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

the class TransferSelectorTest method testSelectSendWithClose1.

@Test
public void testSelectSendWithClose1() throws Exception {
    final Channel<String> channel1 = newChannel();
    final Channel<String> channel2 = newChannel();
    final Channel<String> channel3 = newChannel();
    Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            SelectAction<String> sa1 = select(send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
            SelectAction<String> sa2 = select(send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
            assertThat(sa1.index(), is(1));
            assertThat(sa2.index(), is(1));
        }
    }).start();
    Thread.sleep(200);
    channel2.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 12 with Fiber

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

the class TransferSelectorTest method testSelectReceive1.

@Test
public void testSelectReceive1() throws Exception {
    final Channel<String> channel1 = newChannel();
    final Channel<String> channel2 = newChannel();
    final Channel<String> channel3 = newChannel();
    Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            SelectAction<String> sa1 = select(receive(channel1), receive(channel2), receive(channel3));
            String m1 = sa1.message();
            SelectAction<String> sa2 = select(receive(channel1), receive(channel2), receive(channel3));
            String m2 = sa2.message();
            assertThat(sa1.index(), is(0));
            assertThat(m1, equalTo("hello"));
            assertThat(sa2.index(), is(2));
            assertThat(m2, equalTo("world!"));
        }
    }).start();
    Thread.sleep(200);
    channel1.send("hello");
    Thread.sleep(200);
    channel3.send("world!");
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 13 with Fiber

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

the class TransformingChannelTest method testZipWithTimeoutsThreadToFiber.

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

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

                @Override
                public String apply(String x1, Integer x2) {
                    return x1 + x2;
                }
            });
            // 0
            sync.receive();
            final String m1 = ch.receive(200, TimeUnit.MILLISECONDS);
            // 1
            sync.receive();
            final String m0 = ch.receive(10, TimeUnit.MILLISECONDS);
            final String m2 = ch.receive(190, TimeUnit.MILLISECONDS);
            // 2
            sync.receive();
            final String m3 = ch.receive(30, TimeUnit.MILLISECONDS);
            // 3
            sync.receive();
            final String m4 = ch.receive(30, TimeUnit.MILLISECONDS);
            assertThat(m1, equalTo("a1"));
            assertThat(m0, is(nullValue()));
            assertThat(m2, equalTo("b2"));
            assertThat(m3, equalTo("c3"));
            assertThat(m4, is(nullValue()));
        }
    }).start();
    // 0
    sync.send(GO);
    Strand.sleep(50);
    ch1.send("a", 10, TimeUnit.SECONDS);
    ch2.send(1, 10, TimeUnit.SECONDS);
    // 1
    sync.send(GO);
    ch1.send("b", 10, TimeUnit.SECONDS);
    Strand.sleep(100);
    ch2.send(2, 10, TimeUnit.SECONDS);
    // 2
    sync.send(GO);
    ch1.send("c", 10, TimeUnit.SECONDS);
    ch2.send(3, 10, TimeUnit.SECONDS);
    // 3
    sync.send(GO);
    ch1.send("foo", 10, TimeUnit.SECONDS);
    // Discards previous
    ch2.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 14 with Fiber

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

the class TransformingChannelTest method testSendReduceThreadToFiber.

@Test
public void testSendReduceThreadToFiber() throws Exception {
    final Channel<Integer> ch = newChannel();
    final 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(1));
            assertThat(m2, equalTo(3));
            assertThat(m3, equalTo(6));
            assertThat(m4, equalTo(10));
            assertThat(m5, equalTo(15));
            assertThat(m6, is(nullValue()));
        }
    }).start();
    final SendPort<Integer> ch1 = Channels.reduceSend((SendPort<Integer>) ch, new Function2<Integer, Integer, Integer>() {

        @Override
        public Integer apply(Integer accum, Integer input) {
            return accum + input;
        }
    }, 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 15 with Fiber

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

the class TransformingChannelTest method testSendFilterFiberToFiber.

@Test
public void testSendFilterFiberToFiber() throws Exception {
    final Channel<Integer> ch = newChannel();
    Fiber fib1 = 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();
    Fiber fib2 = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            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();
    fib1.join();
    fib2.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