Search in sources :

Example 16 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class TransferSelectorTest method testSelectReceiveWithClose2.

@Test
public void testSelectReceiveWithClose2() 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 {
            Strand.sleep(200);
            SelectAction<String> sa1 = select(receive(channel1), receive(channel2), receive(channel3));
            String m1 = sa1.message();
            Strand.sleep(200);
            SelectAction<String> sa2 = select(receive(channel1), receive(channel2), receive(channel3));
            String m2 = sa2.message();
            assertThat(sa1.index(), is(2));
            assertThat(m1, nullValue());
            assertThat(m2, nullValue());
        }
    }).start();
    channel3.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 17 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class TransferSelectorTest method testSelectSend2.

@Test
public void testSelectSend2() 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 {
            Strand.sleep(200);
            SelectAction<String> sa1 = select(send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
            Strand.sleep(200);
            SelectAction<String> sa2 = select(send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
            assertThat(sa1.index(), is(1));
            assertThat(sa2.index(), is(0));
        }
    }).start();
    String m1 = channel2.receive();
    assertThat(m1, equalTo("hi2"));
    String m2 = channel1.receive();
    assertThat(m2, equalTo("hi1"));
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 18 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class TransferSelectorTest method testSelectReceive2.

@Test
public void testSelectReceive2() 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 {
            Strand.sleep(200);
            SelectAction<String> sa1 = select(receive(channel1), receive(channel2), receive(channel3));
            Strand.sleep(200);
            SelectAction<String> sa2 = select(receive(channel1), receive(channel2), receive(channel3));
            if (sa2.index() < sa1.index()) {
                SelectAction<String> tmp = sa1;
                sa1 = sa2;
                sa2 = tmp;
            }
            String m1 = sa1.message();
            String m2 = sa2.message();
            assertThat(sa1.index(), is(0));
            assertThat(m1, equalTo("hello"));
            assertThat(sa2.index(), is(2));
            assertThat(m2, equalTo("world!"));
        }
    }).start();
    channel1.send("hello");
    channel3.send("world!");
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 19 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class TransferSelectorTest method testSelectSendWithClose1.

@Test
public void testSelectSendWithClose1() 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"));
            SelectAction<String> sa2 = select(send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
            assertThat(sa1.index(), is(1));
            assertThat(sa2.index(), is(1));
        }
    }).start();
    Thread.sleep(200);
    channel2.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 20 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.

the class TransferSelectorTest method testSelectReceive1.

@Test
public void testSelectReceive1() 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));
            String m1 = sa1.message();
            SelectAction<String> sa2 = select(receive(channel1), receive(channel2), receive(channel3));
            String m2 = sa2.message();
            assertThat(sa1.index(), is(0));
            assertThat(m1, equalTo("hello"));
            assertThat(sa2.index(), is(2));
            assertThat(m2, equalTo("world!"));
        }
    }).start();
    Thread.sleep(200);
    channel1.send("hello");
    Thread.sleep(200);
    channel3.send("world!");
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Aggregations

SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)90 Test (org.junit.Test)82 Fiber (co.paralleluniverse.fibers.Fiber)73 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Strand (co.paralleluniverse.strands.Strand)4 QueueCapacityExceededException (co.paralleluniverse.strands.queues.QueueCapacityExceededException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Ignore (org.junit.Ignore)3 Condition (co.paralleluniverse.strands.Condition)2 SimpleConditionSynchronizer (co.paralleluniverse.strands.SimpleConditionSynchronizer)2 Timeout (co.paralleluniverse.strands.Timeout)2 Channel (co.paralleluniverse.strands.channels.Channel)2 IntChannel (co.paralleluniverse.strands.channels.IntChannel)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CheckedCallable (co.paralleluniverse.common.util.CheckedCallable)1 Function2 (co.paralleluniverse.common.util.Function2)1 Pair (co.paralleluniverse.common.util.Pair)1 VerifyInstrumentationException (co.paralleluniverse.fibers.VerifyInstrumentationException)1