Search in sources :

Example 41 with Fiber

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

the class TransferSelectorTest method testSelectReceiveWithTimeoutChannel.

@Test
public void testSelectReceiveWithTimeoutChannel() throws Exception {
    final Channel<String> channel1 = newChannel();
    final Channel<String> channel2 = newChannel();
    final Channel<String> channel3 = newChannel();
    Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            SelectAction<String> sa1 = select(receive(channel1), receive(channel2), receive(channel3), receive(TimeoutChannel.<String>timeout(1, TimeUnit.MILLISECONDS)));
            SelectAction<String> sa2 = select(receive(channel1), receive(channel2), receive(channel3), receive(TimeoutChannel.<String>timeout(300, TimeUnit.MILLISECONDS)));
            String m2 = sa2.message();
            assertThat(sa1.index(), is(3));
            assertThat(sa2.index(), is(0));
            assertThat(m2, equalTo("hello"));
        }
    }).start();
    Thread.sleep(200);
    channel1.send("hello");
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 42 with Fiber

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

the class TransferSelectorTest method testSelectSendWithTimeoutChannel.

@Test
public void testSelectSendWithTimeoutChannel() throws Exception {
    final Channel<String> channel1 = newChannel();
    final Channel<String> channel2 = newChannel();
    final Channel<String> channel3 = newChannel();
    Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            SelectAction<String> sa1 = select(send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"), receive(TimeoutChannel.<String>timeout(1, TimeUnit.MILLISECONDS)));
            SelectAction<String> sa2 = select(send(channel1, "bye1"), send(channel2, "bye2"), send(channel3, "bye3"), receive(TimeoutChannel.<String>timeout(300, TimeUnit.MILLISECONDS)));
            assertThat(sa1.index(), is(3));
            assertThat(sa2.index(), is(2));
        }
    }).start();
    Thread.sleep(200);
    String m1 = channel3.receive();
    // the first send is cancelled
    assertThat(m1, equalTo("bye3"));
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 43 with Fiber

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

Example 44 with Fiber

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

the class Merge3Test method testMerge3.

@Test
public void testMerge3() {
    Fiber c = new Fiber((String) null, null, new Merge3Test());
    TestsHelper.exec(c);
}
Also used : Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 45 with Fiber

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

the class ReflectionInvokeTest method testCatch.

@Test
public void testCatch() {
    results.clear();
    try {
        Fiber co = new Fiber((String) null, null, new Callable1());
        exec(co);
        results.add("B");
        exec(co);
        results.add("D");
        exec(co);
    } finally {
        System.out.println(results);
    }
    assertEquals(5, results.size());
    assertEquals(Arrays.asList("A", "B", "C", "D", "E"), results);
}
Also used : Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Aggregations

Fiber (co.paralleluniverse.fibers.Fiber)105 Test (org.junit.Test)88 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)73 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)20 Strand (co.paralleluniverse.strands.Strand)8 IntChannel (co.paralleluniverse.strands.channels.IntChannel)4 QueueCapacityExceededException (co.paralleluniverse.strands.queues.QueueCapacityExceededException)4 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 TimeoutException (java.util.concurrent.TimeoutException)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