Search in sources :

Example 26 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class TransformingChannelTest method testSendFilterWithTimeouts.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            // 0
            sync.receive();
            final Integer m1 = ch.receive(200, TimeUnit.MILLISECONDS);
            // 1
            sync.receive();
            final Integer m0 = ch.receive(10, TimeUnit.MILLISECONDS);
            final Integer m2 = ch.receive(190, TimeUnit.MILLISECONDS);
            final Integer m3 = ch.receive(30, TimeUnit.MILLISECONDS);
            assertThat(m1, equalTo(2));
            assertThat(m0, is(nullValue()));
            assertThat(m2, equalTo(4));
            assertThat(m3, is(nullValue()));
        }
    }).start();
    SendPort<Integer> ch1 = Channels.filterSend((SendPort<Integer>) ch, new Predicate<Integer>() {

        @Override
        public boolean apply(Integer input) {
            return input % 2 == 0;
        }
    });
    // 0
    sync.send(GO);
    Strand.sleep(50);
    // Discarded (at send side)
    ch1.send(1, 10, TimeUnit.SECONDS);
    ch1.send(2, 10, TimeUnit.SECONDS);
    // 1
    sync.send(GO);
    Strand.sleep(50);
    // Discarded (at send side)
    ch1.send(3, 10, TimeUnit.SECONDS);
    ch1.send(4, 10, TimeUnit.SECONDS);
    // Discarded (at send side)
    ch1.send(5, 10, TimeUnit.SECONDS);
    ch1.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 27 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class TransformingChannelTest method testFlatmapThreadToFiber.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            ReceivePort<Integer> ch = Channels.flatMap(ch1, 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);
                }
            });
            assertThat(ch.receive(), is(1));
            assertThat(ch.receive(), 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(), is(5));
            assertThat(ch.receive(), is(nullValue()));
            assertThat(ch.isClosed(), is(true));
        }
    }).start();
    Strand.sleep(50);
    ch1.send(1);
    ch1.send(2);
    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 28 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class TransformingChannelTest method testSendReduceInitThreadToFiber.

@Test
public void testSendReduceInitThreadToFiber() 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();
            assertThat(m1, equalTo(0));
            assertNull(m2);
        }
    }).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.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 29 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class TwoSidedTest method twoSidedTestWithProcessor.

@Test
public void twoSidedTestWithProcessor() throws Exception {
    // Publisher
    final Channel<Integer> publisherChannel = Channels.newChannel(random() ? 0 : 5, OverflowPolicy.BLOCK);
    final Strand publisherStrand = new Fiber<Void>(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            for (long i = 0; i < ELEMENTS; i++) publisherChannel.send((int) (i % 1000));
            publisherChannel.close();
        }
    }).start();
    final Publisher<Integer> publisher = ReactiveStreams.toPublisher(publisherChannel);
    // Processor
    final Processor<Integer, Integer> processor = ReactiveStreams.toProcessor(5, OverflowPolicy.BLOCK, new SuspendableAction2<ReceivePort<Integer>, SendPort<Integer>>() {

        @Override
        public void call(ReceivePort<Integer> in, SendPort<Integer> out) throws SuspendExecution, InterruptedException {
            long count = 0;
            for (Integer element; ((element = in.receive()) != null); count++) {
                out.send(element * 10);
                out.send(element * 100);
                // Fiber.sleep(1); // just for fun
                assertTrue(count < ELEMENTS);
            }
            assertEquals(ELEMENTS, count);
            out.close();
        }
    });
    publisher.subscribe(processor);
    // Subscriber
    final ReceivePort<Integer> subscriberChannel = ReactiveStreams.subscribe(buffer, overflowPolicy, processor);
    final Strand subscriberStrand = new Fiber<Void>(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            long count = 0;
            for (; ; ) {
                Integer x = subscriberChannel.receive();
                if (x == null)
                    break;
                assertTrue(x % 10 == 0);
                if (count % 2 != 0)
                    assertTrue(x % 100 == 0);
                count++;
            }
            subscriberChannel.close();
            assertEquals(ELEMENTS * 2, count);
        }
    }).start();
    subscriberStrand.join(5, TimeUnit.SECONDS);
    publisherStrand.join(5, TimeUnit.SECONDS);
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) ReceivePort(co.paralleluniverse.strands.channels.ReceivePort) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) SendPort(co.paralleluniverse.strands.channels.SendPort) Strand(co.paralleluniverse.strands.Strand)

Example 30 with SuspendableRunnable

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

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