Search in sources :

Example 71 with Fiber

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

the class PipelineTest method testPipeline.

@Test
public void testPipeline() throws Exception {
    final Channel<Integer> i = newChannel();
    final Channel<Integer> o = newChannel();
    final Pipeline<Integer, Integer> p = new Pipeline<>(i, o, new SuspendableAction2<Integer, Channel<Integer>>() {

        @Override
        public void call(final Integer i, final Channel<Integer> out) throws SuspendExecution, InterruptedException {
            out.send(i + 1);
            out.close();
        }
    }, parallelism);
    final Fiber<Long> pf = new Fiber("pipeline", scheduler, p).start();
    final Fiber receiver = new Fiber("receiver", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            final Integer m1 = o.receive();
            final Integer m2 = o.receive();
            final Integer m3 = o.receive();
            final Integer m4 = o.receive();
            assertThat(m1, notNullValue());
            assertThat(m2, notNullValue());
            assertThat(m3, notNullValue());
            assertThat(m4, notNullValue());
            assertThat(ImmutableSet.of(m1, m2, m3, m4), equalTo(ImmutableSet.of(2, 3, 4, 5)));
            try {
                pf.join();
            } catch (ExecutionException ex) {
                // It should never happen
                throw new AssertionError(ex);
            }
            // This is needed, else `isClosed` could return false
            assertNull(o.tryReceive());
            // Can be used reliably only in owner (receiver)
            assertTrue(o.isClosed());
        }
    }).start();
    i.send(1);
    i.send(2);
    i.send(3);
    i.send(4);
    i.close();
    // Join pipeline
    long transferred = pf.get();
    assertThat(transferred, equalTo(p.getTransferred()));
    assertThat(transferred, equalTo(4l));
    receiver.join();
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Channel(co.paralleluniverse.strands.channels.Channel) Fiber(co.paralleluniverse.fibers.Fiber) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 72 with Fiber

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

the class ChannelTest method whenChannelClosedThenBlockedSendsComplete.

@Test
public void whenChannelClosedThenBlockedSendsComplete() throws Exception {
    assumeThat(policy, is(OverflowPolicy.BLOCK));
    final Channel<Integer> ch = newChannel();
    final SuspendableRunnable r = new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            for (int i = 1; i <= 100; i++) {
                ch.send(i);
            }
        }
    };
    Fiber fib1 = new Fiber("fiber", scheduler, r).start();
    Fiber fib2 = new Fiber("fiber", scheduler, r).start();
    Thread.sleep(500);
    ch.close();
    fib1.join();
    fib2.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 73 with Fiber

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

the class ChannelTest method testChannelGroupReceive.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            String m1 = group.receive();
            String m2 = channel2.receive();
            assertThat(m1, equalTo("hello"));
            assertThat(m2, equalTo("world!"));
        }
    }).start();
    Thread.sleep(100);
    channel3.send("hello");
    Thread.sleep(100);
    if (policy != OverflowPolicy.BLOCK) {
        // TransferChannel will block here
        channel1.send("goodbye");
        Thread.sleep(100);
    }
    channel2.send("world!");
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 74 with Fiber

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

the class ChannelTest method sendMessageFromThreadToFiber.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            String m = ch.receive();
            assertThat(m, equalTo("a message"));
        }
    }).start();
    Thread.sleep(50);
    ch.send("a message");
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 75 with Fiber

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

the class TransferSelectorTest method testSelectSendTimeout.

@Test
public void testSelectSendTimeout() 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, send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
            SelectAction<String> sa2 = select(300, TimeUnit.MILLISECONDS, send(channel1, "bye1"), send(channel2, "bye2"), send(channel3, "bye3"));
            assertThat(sa1, is(nullValue()));
            assertThat(sa2.index(), is(2));
        }
    }).start();
    Thread.sleep(200);
    String m1 = channel3.receive();
    // the first send is cancelled
    assertThat(m1, equalTo("bye3"));
    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