Search in sources :

Example 66 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable 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)

Example 67 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable 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 68 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable 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 69 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable 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 70 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable 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)

Aggregations

SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)90 Test (org.junit.Test)82 Fiber (co.paralleluniverse.fibers.Fiber)73 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Strand (co.paralleluniverse.strands.Strand)4 QueueCapacityExceededException (co.paralleluniverse.strands.queues.QueueCapacityExceededException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Ignore (org.junit.Ignore)3 Condition (co.paralleluniverse.strands.Condition)2 SimpleConditionSynchronizer (co.paralleluniverse.strands.SimpleConditionSynchronizer)2 Timeout (co.paralleluniverse.strands.Timeout)2 Channel (co.paralleluniverse.strands.channels.Channel)2 IntChannel (co.paralleluniverse.strands.channels.IntChannel)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CheckedCallable (co.paralleluniverse.common.util.CheckedCallable)1 Function2 (co.paralleluniverse.common.util.Function2)1 Pair (co.paralleluniverse.common.util.Pair)1 VerifyInstrumentationException (co.paralleluniverse.fibers.VerifyInstrumentationException)1