Search in sources :

Example 21 with Fiber

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

the class TransformingChannelTest method testTakeThreadToFibers.

@Test
@SuppressWarnings("null")
public void testTakeThreadToFibers() throws Exception {
    // TODO Reorg to try with blocking channel at least meaningful parts
    assumeThat(mailboxSize, greaterThan(0));
    final Channel<Object> takeSourceCh = newChannel();
    // Test 2 fibers failing immediately on take 0 of 1
    final ReceivePort<Object> take0RP = Channels.take((ReceivePort<Object>) takeSourceCh, 0);
    final SuspendableRunnable take0SR = new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            assertThat(take0RP.receive(), is(nullValue()));
            assertThat(take0RP.tryReceive(), is(nullValue()));
            long start = System.nanoTime();
            assertThat(take0RP.receive(10, TimeUnit.SECONDS), is(nullValue()));
            long end = System.nanoTime();
            // Should be immediate
            assertThat(end - start, lessThan(new Long(5 * 1000 * 1000 * 1000)));
            start = System.nanoTime();
            assertThat(take0RP.receive(new Timeout(10, TimeUnit.SECONDS)), is(nullValue()));
            end = System.nanoTime();
            // Should be immediate
            assertThat(end - start, lessThan(new Long(5 * 1000 * 1000 * 1000)));
        }
    };
    final Fiber take0Of1Fiber1 = new Fiber("take-0-of-1_fiber1", scheduler, take0SR).start();
    final Fiber take0Of1Fiber2 = new Fiber("take-0-of-1_fiber2", scheduler, take0SR).start();
    takeSourceCh.send(new Object());
    take0Of1Fiber1.join();
    take0Of1Fiber2.join();
    // 1 left in source, check and cleanup
    assertThat(takeSourceCh.receive(), is(notNullValue()));
    // Test tryReceive failing immediately when fiber blocked in receive on take 1 of 2
    final ReceivePort<Object> take1Of2RP = Channels.take((ReceivePort<Object>) takeSourceCh, 1);
    final Fiber timeoutSucceedingTake1Of2 = new Fiber("take-1-of-2_timeout_success", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            final long start = System.nanoTime();
            assertThat(take1Of2RP.receive(1, TimeUnit.SECONDS), is(notNullValue()));
            final long end = System.nanoTime();
            assertThat(end - start, lessThan(new Long(500 * 1000 * 1000)));
        }
    }).start();
    // Let the fiber blocks in receive before starting the try
    Thread.sleep(100);
    final Fiber tryFailingTake1Of2 = new Fiber("take-1-of-2_try_fail", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            final long start = System.nanoTime();
            assertThat(take1Of2RP.tryReceive(), is(nullValue()));
            final long end = System.nanoTime();
            // Should be immediate
            assertThat(end - start, lessThan(new Long(500 * 1000 * 1000)));
        }
    }).start();
    Thread.sleep(100);
    // Make messages available
    takeSourceCh.send(new Object());
    takeSourceCh.send(new Object());
    timeoutSucceedingTake1Of2.join();
    tryFailingTake1Of2.join();
    // 1 left in source, check and cleanup
    assertThat(takeSourceCh.receive(), is(notNullValue()));
    // Comprehensive take + contention test:
    //
    // - 1 message available immediately, 2 messages available in a burst on the source after 1s
    // - take 2
    // - 5 fibers competing on the take source (1 in front)
    //
    // - one front fiber receiving with 200ms timeout => immediate success
    // - one more front fiber receiving with 200ms timeout => fail
    // - 3rd fiber taking over, receiving with 200ms timeout => fail
    // - 4th fiber taking over, receiving with 1s timeout => success
    // - 5th fiber asking untimed receive, waiting in monitor, will bail out because of take threshold
    final ReceivePort<Object> take2Of3RPComprehensive = Channels.take((ReceivePort<Object>) takeSourceCh, 2);
    final Function2<Long, Integer, Fiber> take1SRFun = new Function2<Long, Integer, Fiber>() {

        @Override
        public Fiber apply(final Long timeoutMS, final Integer position) {
            return new Fiber("take-1-of-2_comprehensive_receiver_" + (timeoutMS >= 0 ? timeoutMS : "unlimited") + "ms-" + position, scheduler, new SuspendableRunnable() {

                @Override
                public void run() throws SuspendExecution, InterruptedException {
                    final long start = System.nanoTime();
                    final Object res = (timeoutMS >= 0 ? take2Of3RPComprehensive.receive(timeoutMS, TimeUnit.MILLISECONDS) : take2Of3RPComprehensive.receive());
                    final long end = System.nanoTime();
                    switch(position) {
                        case 1:
                            assertThat(res, is(notNullValue()));
                            assertThat(end - start, lessThan(new Long(300 * 1000 * 1000)));
                            break;
                        case 2:
                            assertThat(res, is(nullValue()));
                            assertThat(end - start, greaterThan(new Long(300 * 1000 * 1000)));
                            break;
                        case 3:
                            assertThat(res, is(nullValue()));
                            assertThat(end - start, greaterThan(new Long(200 * 1000 * 1000)));
                            break;
                        case 4:
                            assertThat(res, is(notNullValue()));
                            assertThat(end - start, lessThan(new Long(1000 * 1000 * 1000)));
                            break;
                        case 5:
                            assertThat(res, is(nullValue()));
                            // Should be almost instantaneous
                            assertThat(end - start, lessThan(new Long(1000 * 1000 * 1000)));
                            break;
                        default:
                            fail();
                            break;
                    }
                }
            });
        }
    };
    final Fiber[] competing = new Fiber[5];
    // First front fiber winning first message
    competing[0] = take1SRFun.apply(300l, 1).start();
    // Make 1 message available immediately for the first front fiber to consume
    takeSourceCh.send(new Object());
    Thread.sleep(100);
    // Second front fiber losing (waiting too little for second message)
    competing[1] = take1SRFun.apply(300l, 2).start();
    Thread.sleep(100);
    // First waiter, will fail (not waiting enough)
    competing[2] = take1SRFun.apply(200l, 3).start();
    // First waiter takeover
    Thread.sleep(300);
    // Second waiter, will win second message (waiting enough)
    competing[3] = take1SRFun.apply(1000l, 4).start();
    // Second waiter takeover
    Thread.sleep(300);
    // Third waiter, will try after take threshold and will bail out
    competing[4] = take1SRFun.apply(-1l, 5).start();
    // Make 2 more messages available
    takeSourceCh.send(new Object());
    takeSourceCh.send(new Object());
    // Wait fibers to finsh
    for (final Fiber f : competing) f.join();
    // 1 left in source, check and cleanup
    assertThat(takeSourceCh.receive(), is(notNullValue()));
    // Explicit (and uncoupled from source) closing of TakeSP
    final ReceivePort<Object> take1Of0ExplicitClose = Channels.take((ReceivePort<Object>) takeSourceCh, 1);
    final SuspendableRunnable explicitCloseSR = new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            final long start = System.nanoTime();
            final Object ret = take1Of0ExplicitClose.receive();
            final long end = System.nanoTime();
            assertThat(ret, is(nullValue()));
            assertTrue(take1Of0ExplicitClose.isClosed());
            assertFalse(takeSourceCh.isClosed());
            assertThat(end - start, lessThan(new Long(500 * 1000 * 1000)));
        }
    };
    final Fiber explicitCloseF1 = new Fiber("take-explicit-close-1", scheduler, explicitCloseSR);
    final Fiber explicitCloseF2 = new Fiber("take-explicit-close-2", scheduler, explicitCloseSR);
    Thread.sleep(100);
    take1Of0ExplicitClose.close();
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Timeout(co.paralleluniverse.strands.Timeout) Fiber(co.paralleluniverse.fibers.Fiber) Function2(co.paralleluniverse.common.util.Function2) Test(org.junit.Test)

Example 22 with Fiber

use of co.paralleluniverse.fibers.Fiber 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 23 with Fiber

use of co.paralleluniverse.fibers.Fiber 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 24 with Fiber

use of co.paralleluniverse.fibers.Fiber 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 25 with Fiber

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

the class PeerTKB method spawnActor.

private <T extends Actor<Message, V>, Message, V> T spawnActor(T actor) {
    Fiber fiber = new Fiber(actor);
    fiber.setUncaughtExceptionHandler(new Strand.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Strand s, Throwable e) {
            e.printStackTrace();
            throw Exceptions.rethrow(e);
        }
    });
    fiber.start();
    return actor;
}
Also used : Fiber(co.paralleluniverse.fibers.Fiber) Strand(co.paralleluniverse.strands.Strand)

Aggregations

Fiber (co.paralleluniverse.fibers.Fiber)103 Test (org.junit.Test)86 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)73 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)19 Strand (co.paralleluniverse.strands.Strand)8 QueueCapacityExceededException (co.paralleluniverse.strands.queues.QueueCapacityExceededException)4 IntChannel (co.paralleluniverse.strands.channels.IntChannel)3 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 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 VerifyInstrumentationException (co.paralleluniverse.fibers.VerifyInstrumentationException)1