Search in sources :

Example 11 with SuspendExecution

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

the class InstrumentationOptimizerTest method testSkipForwardsToSuspendableDouble.

@Test
public void testSkipForwardsToSuspendableDouble() throws InterruptedException, SuspendExecution, ExecutionException {
    new Fiber(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            skipForwardsToSuspendableDouble();
        }
    }).start().join();
    assertTrue(isOptimized("skipForwardsToSuspendableDouble"));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 12 with SuspendExecution

use of co.paralleluniverse.fibers.SuspendExecution 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 13 with SuspendExecution

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

the class TransformingChannelTest method testFiberTransform1.

@Test
public void testFiberTransform1() throws Exception {
    final Channel<Integer> in = newChannel();
    final Channel<Integer> out = newChannel();
    Channels.fiberTransform(in, out, new SuspendableAction2<ReceivePort<Integer>, SendPort<Integer>>() {

        @Override
        public void call(ReceivePort<Integer> in, SendPort<Integer> out) throws SuspendExecution, InterruptedException {
            Integer x;
            while ((x = in.receive()) != null) {
                if (x % 2 == 0)
                    out.send(x * 10);
            }
            out.send(1234);
            out.close();
        }
    });
    Fiber fib1 = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            assertThat(out.receive(), equalTo(20));
            assertThat(out.receive(), equalTo(40));
            assertThat(out.receive(), equalTo(1234));
            assertThat(out.receive(), is(nullValue()));
        }
    }).start();
    Fiber fib2 = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            Strand.sleep(50);
            in.send(1);
            in.send(2);
            Strand.sleep(50);
            in.send(3);
            in.send(4);
            in.send(5);
            in.close();
        }
    }).start();
    fib1.join();
    fib2.join();
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 14 with SuspendExecution

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

the class GlxRemoteChannel method submitSend.

private static void submitSend(final Object message, final GlxGlobalChannelId id) {
    LOG.debug("sending: " + message);
    sendThreadPool.submit(new Runnable() {

        @Override
        public void run() {
            try {
                send(message, id);
            } catch (SuspendExecution e) {
                throw new AssertionError(e);
            }
        }
    });
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable)

Example 15 with SuspendExecution

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

the class InstrumentationOptimizerTest method testDontSkipForwardsWithMethodBefore.

@Test
public void testDontSkipForwardsWithMethodBefore() throws InterruptedException, SuspendExecution, ExecutionException {
    new Fiber(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            dontSkipForwardsWithMethodBefore();
        }
    }).start().join();
    assertFalse(isOptimized("skipForwardsWithMethodBefore"));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Aggregations

SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)40 Test (org.junit.Test)30 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)24 Fiber (co.paralleluniverse.fibers.Fiber)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Suspendable (co.paralleluniverse.fibers.Suspendable)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Strand (co.paralleluniverse.strands.Strand)3 ArrayList (java.util.ArrayList)3 TimeoutException (java.util.concurrent.TimeoutException)3 Channel (co.paralleluniverse.strands.channels.Channel)2 ExecutionException (java.util.concurrent.ExecutionException)2 Matchers.anyObject (org.mockito.Matchers.anyObject)2 Actor (co.paralleluniverse.actors.Actor)1 ActorRef (co.paralleluniverse.actors.ActorRef)1 BasicActor (co.paralleluniverse.actors.BasicActor)1 LocalActor (co.paralleluniverse.actors.LocalActor)1 MailboxConfig (co.paralleluniverse.actors.MailboxConfig)1 MessageProcessor (co.paralleluniverse.actors.MessageProcessor)1 AbstractServerHandler (co.paralleluniverse.actors.behaviors.AbstractServerHandler)1