Search in sources :

Example 26 with SuspendExecution

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

the class ChannelTest method whenReceiveNotCalledFromOwnerThenThrowException4.

@Ignore
@Test
public void whenReceiveNotCalledFromOwnerThenThrowException4() throws Exception {
    assumeTrue(Debug.isAssertionsEnabled());
    final Channel<String> ch = newChannel();
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                ch.receive();
            } catch (InterruptedException ex) {
                throw new AssertionError(ex);
            } catch (SuspendExecution e) {
                throw new AssertionError(e);
            }
        }
    });
    thread.start();
    Thread.sleep(100);
    ch.send("a message");
    boolean thrown = false;
    try {
        ch.receive();
    } catch (Throwable e) {
        thrown = true;
    }
    assertTrue(thrown);
    thread.join();
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 27 with SuspendExecution

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

the class AbstractFuture method get.

@Override
@Suspendable
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    try {
        if (done)
            return getValue();
        long left = unit.toNanos(timeout);
        final long deadline = System.nanoTime() + left;
        Object token = sync.register();
        try {
            for (int i = 0; !done; i++) {
                sync.await(i, left, TimeUnit.NANOSECONDS);
                left = deadline - System.nanoTime();
                if (left <= 0)
                    throw new TimeoutException();
            }
        } finally {
            sync.unregister(token);
        }
        return getValue();
    } catch (SuspendExecution e) {
        throw new AssertionError(e);
    }
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) TimeoutException(java.util.concurrent.TimeoutException) Suspendable(co.paralleluniverse.fibers.Suspendable)

Example 28 with SuspendExecution

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

the class InstrumentationOptimizerTest method testDontSkipForwardsWithLoopBefore.

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

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

Example 29 with SuspendExecution

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

the class InstrumentationOptimizerTest method testSkipForwardsWithLoopAfter.

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

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

Example 30 with SuspendExecution

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

the class InstrumentationOptimizerTest method testSkipForwardsToSuspendableLong.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            skipForwardsToSuspendableLong();
        }
    }).start().join();
    assertTrue(isOptimized("skipForwardsToSuspendableLong"));
}
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