Search in sources :

Example 61 with Fiber

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

the class ChannelTest method testChannelCloseExceptionWithSleep.

@Test
public void testChannelCloseExceptionWithSleep() throws Exception {
    final Channel<Integer> ch = newChannel();
    Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            for (int i = 1; i <= 5; i++) {
                Integer m = ch.receive();
                assertThat(m, equalTo(i));
            }
            try {
                Integer m = ch.receive();
                fail("m = " + m);
            } catch (ProducerException e) {
                assertThat(e.getCause().getMessage(), equalTo("foo"));
            }
            assertTrue(ch.isClosed());
        }
    }).start();
    Thread.sleep(50);
    ch.send(1);
    ch.send(2);
    ch.send(3);
    ch.send(4);
    ch.send(5);
    Thread.sleep(50);
    ch.close(new Exception("foo"));
    ch.send(6);
    ch.send(7);
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) QueueCapacityExceededException(co.paralleluniverse.strands.queues.QueueCapacityExceededException) Test(org.junit.Test)

Example 62 with Fiber

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

the class ChannelTest method whenReceiveNotCalledFromOwnerThenThrowException1.

@Ignore
@Test
public void whenReceiveNotCalledFromOwnerThenThrowException1() throws Exception {
    assumeTrue(Debug.isAssertionsEnabled());
    final Channel<String> ch = newChannel();
    Fiber fib1 = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            String m = ch.receive();
            assertThat(m, equalTo("a message"));
        }
    }).start();
    Fiber fib2 = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            Fiber.sleep(50);
            ch.send("a message");
            boolean thrown = false;
            try {
                ch.receive();
            } catch (Throwable e) {
                thrown = true;
            }
            assertTrue(thrown);
        }
    }).start();
    fib1.join();
    fib2.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 63 with Fiber

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

the class ChannelTest method whenReceiveNotCalledFromOwnerThenThrowException2.

@Ignore
@Test
public void whenReceiveNotCalledFromOwnerThenThrowException2() throws Exception {
    assumeTrue(Debug.isAssertionsEnabled());
    final Channel<String> ch = newChannel();
    Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            String m = ch.receive();
            assertThat(m, equalTo("a message"));
        }
    }).start();
    Thread.sleep(50);
    ch.send("a message");
    boolean thrown = false;
    try {
        ch.receive();
    } catch (Throwable e) {
        thrown = true;
    }
    assertTrue(thrown);
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 64 with Fiber

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

the class ChannelTest method sendMessageFromFiberToFiber.

@Test
public void sendMessageFromFiberToFiber() throws Exception {
    final Channel<String> ch = newChannel();
    Fiber fib1 = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            String m = ch.receive();
            assertThat(m, equalTo("a message"));
        }
    }).start();
    Fiber fib2 = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            Fiber.sleep(50);
            ch.send("a message");
        }
    }).start();
    fib1.join();
    fib2.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 65 with Fiber

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

the class ChannelTest method testChannelCloseException.

@Test
public void testChannelCloseException() throws Exception {
    final Channel<Integer> ch = newChannel();
    Fiber fib = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            for (int i = 1; i <= 5; i++) {
                Integer m = ch.receive();
                assertThat(m, equalTo(i));
            }
            try {
                ch.receive();
                fail();
            } catch (ProducerException e) {
                assertThat(e.getCause().getMessage(), equalTo("foo"));
            }
            assertTrue(ch.isClosed());
        }
    }).start();
    Thread.sleep(50);
    ch.send(1);
    ch.send(2);
    ch.send(3);
    ch.send(4);
    ch.send(5);
    ch.close(new Exception("foo"));
    ch.send(6);
    ch.send(7);
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) QueueCapacityExceededException(co.paralleluniverse.strands.queues.QueueCapacityExceededException) 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