Search in sources :

Example 16 with SuspendExecution

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

the class Val method get.

/**
 * Returns the delayed value, blocking until it has been set, but no longer than the given timeout.
 *
 * @param timeout The maximum duration to block waiting for the value to be set.
 * @param unit    The time unit of the timeout value.
 * @return the value
 * @throws TimeoutException     if the timeout expires before the value is set.
 * @throws InterruptedException
 */
@Override
@Suspendable
public V get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
    try {
        final SimpleConditionSynchronizer s = sync;
        if (s != null) {
            Object token = s.register();
            try {
                final long start = System.nanoTime();
                long left = unit.toNanos(timeout);
                final long deadline = start + left;
                for (int i = 0; sync != null; i++) {
                    s.awaitNanos(i, left);
                    if (sync == null)
                        break;
                    left = deadline - System.nanoTime();
                    if (left <= 0)
                        throw new TimeoutException();
                }
            } finally {
                s.unregister(token);
            }
        }
        if (t != null)
            throw t instanceof CancellationException ? (CancellationException) t : new RuntimeExecutionException(t);
        return value;
    } catch (SuspendExecution e) {
        throw new AssertionError(e);
    }
}
Also used : SimpleConditionSynchronizer(co.paralleluniverse.strands.SimpleConditionSynchronizer) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) RuntimeExecutionException(co.paralleluniverse.fibers.RuntimeExecutionException) Suspendable(co.paralleluniverse.fibers.Suspendable)

Example 17 with SuspendExecution

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

the class VerificationTest method testVerifyUninstrumentedCallSiteDeclaringAndOwnerOK.

@Test
public final void testVerifyUninstrumentedCallSiteDeclaringAndOwnerOK() throws ExecutionException, InterruptedException, SuspendExecution {
    assumeTrue(SystemProperties.isEmptyOrTrue("co.paralleluniverse.fibers.verifyInstrumentation"));
    // From https://github.com/puniverse/quasar/issues/255
    final IntChannel intChannel = Channels.newIntChannel(1);
    try {
        new Fiber<>(new SuspendableCallable<Integer>() {

            @Override
            public Integer run() throws SuspendExecution, InterruptedException {
                return intChannel.receive();
            }
        }).start().join(100, TimeUnit.MILLISECONDS);
    } catch (final TimeoutException ignored) {
    }
// Should complete without verification exceptions
}
Also used : IntChannel(co.paralleluniverse.strands.channels.IntChannel) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) Fiber(co.paralleluniverse.fibers.Fiber) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 18 with SuspendExecution

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

the class GeneralSelectorTest method fanout.

<Message> Channel<Message>[] fanout(final ReceivePort<Message> in, final int n) {
    final Channel<Message>[] chans = new Channel[n];
    for (int i = 0; i < n; i++) chans[i] = newChannel();
    spawn(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            for (; ; ) {
                Message m = in.receive();
                // System.out.println("fanout: " + m);
                if (m == null) {
                    for (Channel<Message> c : chans) c.close();
                    break;
                } else {
                    List<SelectAction<Message>> as = new ArrayList<>(n);
                    for (Channel<Message> c : chans) as.add(send(c, m));
                    SelectAction<Message> sa = select(as);
                // System.out.println("Wrote to " + sa.index());
                }
            }
        // System.err.println("fanout done");
        }
    });
    return chans;
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) TimeoutChannel(co.paralleluniverse.strands.channels.TimeoutChannel) ArrayList(java.util.ArrayList) List(java.util.List)

Example 19 with SuspendExecution

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

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

the class ActorTest method testWatchGC.

@Test
public void testWatchGC() throws Exception {
    Assume.assumeFalse(Debug.isDebug());
    final Actor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>(mailboxConfig) {

        @Override
        protected Void doRun() throws SuspendExecution, InterruptedException {
            Fiber.sleep(120000);
            return null;
        }
    });
    System.out.println("actor1 is " + actor);
    WeakReference wrActor2 = new WeakReference(spawnActor(new BasicActor<Message, Void>(mailboxConfig) {

        @Override
        protected Void doRun() throws SuspendExecution, InterruptedException {
            Fiber.sleep(10);
            final Object watch = watch(actor.ref());
            // unwatch(actor, watch);
            return null;
        }
    }));
    System.out.println("actor2 is " + wrActor2.get());
    for (int i = 0; i < 10; i++) {
        Thread.sleep(10);
        System.gc();
    }
    Thread.sleep(2000);
    assertEquals(null, wrActor2.get());
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) WeakReference(java.lang.ref.WeakReference) 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