Search in sources :

Example 76 with Fiber

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

the class Server method main.

public static void main(String[] args) throws ExecutionException, InterruptedException {
    System.setProperty("galaxy.nodeId", Integer.toString(nodeId));
    System.setProperty("galaxy.port", Integer.toString(7050 + nodeId));
    System.setProperty("galaxy.slave_port", Integer.toString(8050 + nodeId));
    new Fiber(new ServerActor(new AbstractServerHandler<SumRequest, Integer, SumRequest>() {

        @Override
        public void init() throws SuspendExecution {
            super.init();
            Actor.currentActor().register("myServer");
            System.out.println(this.toString() + " is ready");
        }

        @Override
        public Integer handleCall(ActorRef<?> from, Object id, SumRequest m) {
            System.out.println(this.toString() + " is handling " + m);
            if (m.a == 0 && m.b == 0)
                ServerActor.currentServerActor().shutdown();
            return m.a + m.b;
        }
    })).start().join();
    System.exit(0);
}
Also used : ServerActor(co.paralleluniverse.actors.behaviors.ServerActor) ActorRef(co.paralleluniverse.actors.ActorRef) Fiber(co.paralleluniverse.fibers.Fiber) AbstractServerHandler(co.paralleluniverse.actors.behaviors.AbstractServerHandler)

Example 77 with Fiber

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

the class TransferSelectorTest method testSelectReceiveTimeout.

@Test
public void testSelectReceiveTimeout() 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(1, TimeUnit.MILLISECONDS, receive(channel1), receive(channel2), receive(channel3));
            SelectAction<String> sa2 = select(300, TimeUnit.MILLISECONDS, receive(channel1), receive(channel2), receive(channel3));
            String m2 = sa2.message();
            assertThat(sa1, is(nullValue()));
            assertThat(sa2.index(), is(0));
            assertThat(m2, equalTo("hello"));
        }
    }).start();
    Thread.sleep(200);
    channel1.send("hello");
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 78 with Fiber

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

the class TransferSelectorTest method testSelectReceiveWithClose1.

@Test
public void testSelectReceiveWithClose1() 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(2));
            assertThat(m1, nullValue());
            assertThat(m2, nullValue());
        }
    }).start();
    Thread.sleep(200);
    channel3.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 79 with Fiber

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

the class TransformingChannelTest method testFlatmapWithTimeoutsThreadToFiber.

@Test
public void testFlatmapWithTimeoutsThreadToFiber() throws Exception {
    final Channel<Integer> ch1 = 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<Integer> ch = Channels.flatMap(ch1, new Function<Integer, ReceivePort<Integer>>() {

                @Override
                public ReceivePort<Integer> apply(Integer x) {
                    if (x == 3)
                        // Discard
                        return null;
                    if (x % 2 == 0)
                        return Channels.toReceivePort(Arrays.asList(new Integer[] { x * 10, x * 100, x * 1000 }));
                    else
                        return Channels.singletonReceivePort(x);
                }
            });
            // 0
            sync.receive();
            assertThat(ch.receive(200, TimeUnit.MILLISECONDS), is(1));
            // 1
            sync.receive();
            assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(nullValue()));
            assertThat(ch.receive(190, TimeUnit.MILLISECONDS), is(20));
            assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(200));
            assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(2000));
            // 2
            sync.receive();
            assertThat(ch.receive(200, TimeUnit.MILLISECONDS), is(40));
            assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(400));
            assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(4000));
            // 3
            sync.receive();
            assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(nullValue()));
            assertThat(ch.receive(190, TimeUnit.MILLISECONDS), is(5));
            // 4
            sync.receive();
            assertThat(ch.receive(30, TimeUnit.MILLISECONDS), is(nullValue()));
            assertThat(ch.isClosed(), is(true));
        }
    }).start();
    // 0
    sync.send(GO);
    Strand.sleep(50);
    ch1.send(1, 10, TimeUnit.SECONDS);
    // 1
    sync.send(GO);
    Strand.sleep(100);
    ch1.send(2, 10, TimeUnit.SECONDS);
    // 2
    sync.send(GO);
    // Discarded
    ch1.send(3, 10, TimeUnit.SECONDS);
    ch1.send(4, 10, TimeUnit.SECONDS);
    // 3
    sync.send(GO);
    Strand.sleep(50);
    ch1.send(5, 10, TimeUnit.SECONDS);
    // 4
    sync.send(GO);
    ch1.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 80 with Fiber

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

the class ArrayTest method testArray.

@Test
public void testArray() {
    Fiber co = new Fiber((String) null, null, this);
    TestsHelper.exec(co);
    assertEquals(42, l1.i);
}
Also used : 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