Search in sources :

Example 31 with Fiber

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

the class ChannelTest method testChannelGroupMix.

@Test
public void testChannelGroupMix() throws Exception {
    assumeThat(mailboxSize, greaterThan(1));
    final Channel<String> channel1 = newChannel();
    final Channel<String> channel2 = newChannel();
    final Channel<String> channel3 = newChannel();
    final Channel<Object> sync = Channels.newChannel(0);
    final ReceivePortGroup<String> group = new ReceivePortGroup<>();
    group.add(channel3);
    final Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            final String m1 = group.receive();
            final String m2 = channel2.receive();
            // 1
            sync.receive();
            // 2
            sync.receive();
            final String m3 = group.receive(10, TimeUnit.MILLISECONDS);
            final String m4 = group.receive(200, TimeUnit.MILLISECONDS);
            // 3
            sync.receive();
            // 4
            sync.receive();
            final String m5 = group.receive(10, TimeUnit.MILLISECONDS);
            final String m6 = group.receive(200, TimeUnit.MILLISECONDS);
            assertThat(m1, equalTo("hello"));
            assertThat(m2, equalTo("world!"));
            assertThat(m3, nullValue());
            assertThat(m4, equalTo("foo"));
            assertThat(m5, nullValue());
            assertThat(m6, equalTo("bar"));
            // 5
            sync.receive();
            // 6
            sync.receive();
            final String m7 = group.receive();
            assertThat(m7, equalTo("2-solo-sings"));
            // 7
            sync.receive();
            // 8
            sync.receive();
            String m8 = group.receive();
            while (m8 != null && m8.contains("leaks")) m8 = group.receive();
            String m9 = group.receive();
            while (m9 != null && m9.contains("leaks")) m9 = group.receive();
            String m10 = group.receive();
            while (m10 != null && m10.contains("leaks")) m10 = group.receive();
            assertThat(ImmutableSet.of(m8, m9, m10), equalTo(ImmutableSet.of("1-paused-by-2-solo-waits", "3-paused-by-2-solo-waits", "1-normal")));
            // 9
            sync.receive();
            // 10
            sync.receive();
            String m11 = group.receive();
            while (m11 != null && m11.contains("leaks")) m11 = group.receive();
            assertThat(m11, equalTo("2-solo-sings-again"));
            // 11
            sync.receive();
            // 12
            sync.receive();
            String m12 = group.receive();
            while (m12 != null && m12.contains("leaks")) m12 = group.receive();
            assertThat(m12, nullValue());
        }
    }).start();
    final Object ping = new Object();
    Thread.sleep(100);
    channel3.send("hello", 1, TimeUnit.SECONDS);
    Thread.sleep(100);
    channel2.send("world!", 1, TimeUnit.SECONDS);
    // 1
    sync.send(ping, 1, TimeUnit.SECONDS);
    group.remove(channel3);
    group.add(channel1);
    // 2
    sync.send(ping, 1, TimeUnit.SECONDS);
    Thread.sleep(150);
    channel1.send("foo", 1, TimeUnit.SECONDS);
    // 3
    sync.send(ping, 1, TimeUnit.SECONDS);
    group.remove(channel1);
    group.add(channel2);
    // 4
    sync.send(ping, 1, TimeUnit.SECONDS);
    Thread.sleep(100);
    channel2.send("bar", 1, TimeUnit.SECONDS);
    // 5
    sync.send(ping, 1, TimeUnit.SECONDS);
    // Solo and mute, solo wins and others are paused (default)
    group.add(channel1);
    group.add(channel3);
    group.setState(new Mix.State(Mix.Mode.MUTE, true), channel2);
    // 6
    sync.send(ping, 1, TimeUnit.SECONDS);
    channel1.send("1-paused-by-2-solo-waits", 1, TimeUnit.SECONDS);
    channel3.send("3-paused-by-2-solo-waits", 1, TimeUnit.SECONDS);
    channel2.send("2-solo-sings", 1, TimeUnit.SECONDS);
    // 7
    sync.send(ping, 1, TimeUnit.SECONDS);
    // Remove solo
    group.setState(new Mix.State(false), channel2);
    // 8
    sync.send(ping, 1, TimeUnit.SECONDS);
    channel2.send("2-muted-leaks", 1, TimeUnit.SECONDS);
    channel1.send("1-normal", 1, TimeUnit.SECONDS);
    // 9
    sync.send(ping, 1, TimeUnit.SECONDS);
    // Restore normal state and solo and change solo effect on other channels to MUTE
    group.setState(new Mix.State(Mix.Mode.NORMAL, true), channel2);
    group.setSoloEffect(Mix.SoloEffect.MUTE_OTHERS);
    // 10
    sync.send(ping, 1, TimeUnit.SECONDS);
    channel1.send("1-muted-by-2-solo-leaks", 1, TimeUnit.SECONDS);
    channel3.send("3-muted-by-2-solo-leaks", 1, TimeUnit.SECONDS);
    channel2.send("2-solo-sings-again", 1, TimeUnit.SECONDS);
    // 11
    sync.send(ping, 1, TimeUnit.SECONDS);
    channel1.close();
    channel2.close();
    channel3.close();
    // 12
    sync.send(ping, 1, TimeUnit.SECONDS);
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 32 with Fiber

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

the class ChannelTest method testTopic.

@Test
public void testTopic() throws Exception {
    final Channel<String> channel1 = newChannel();
    final Channel<String> channel2 = newChannel();
    final Channel<String> channel3 = newChannel();
    final Topic<String> topic = new Topic<>();
    topic.subscribe(channel1);
    topic.subscribe(channel2);
    topic.subscribe(channel3);
    Fiber f1 = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            assertThat(channel1.receive(), equalTo("hello"));
            assertThat(channel1.receive(), equalTo("world!"));
        }
    }).start();
    Fiber f2 = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            assertThat(channel2.receive(), equalTo("hello"));
            assertThat(channel2.receive(), equalTo("world!"));
        }
    }).start();
    Fiber f3 = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            assertThat(channel3.receive(), equalTo("hello"));
            assertThat(channel3.receive(), equalTo("world!"));
        }
    }).start();
    Thread.sleep(100);
    topic.send("hello");
    Thread.sleep(100);
    topic.send("world!");
    f1.join();
    f2.join();
    f3.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 33 with Fiber

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

the class ChannelTest method testChannelGroupReceiveWithTimeout.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            String m1 = group.receive();
            String m2 = channel2.receive();
            String m3 = group.receive(10, TimeUnit.MILLISECONDS);
            String m4 = group.receive(200, TimeUnit.MILLISECONDS);
            assertThat(m1, equalTo("hello"));
            assertThat(m2, equalTo("world!"));
            assertThat(m3, nullValue());
            assertThat(m4, equalTo("foo"));
        }
    }).start();
    Thread.sleep(100);
    channel3.send("hello");
    Thread.sleep(100);
    channel2.send("world!");
    Thread.sleep(100);
    channel1.send("foo");
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 34 with Fiber

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

the class ChannelTest method whenChannelClosedExceptionThenBlockedSendsComplete.

@Test
public void whenChannelClosedExceptionThenBlockedSendsComplete() throws Exception {
    assumeThat(policy, is(OverflowPolicy.BLOCK));
    final Channel<Integer> ch = newChannel();
    final SuspendableRunnable r = new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            for (int i = 1; i <= 100; i++) {
                ch.send(i);
            }
        }
    };
    Fiber fib1 = new Fiber("fiber", scheduler, r).start();
    Fiber fib2 = new Fiber("fiber", scheduler, r).start();
    Thread.sleep(500);
    ch.close(new Exception("foo"));
    fib1.join();
    fib2.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) QueueCapacityExceededException(co.paralleluniverse.strands.queues.QueueCapacityExceededException) Test(org.junit.Test)

Example 35 with Fiber

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

the class ChannelTest method testChannelCloseWithSleep.

@Test
public void testChannelCloseWithSleep() 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));
            }
            Integer m = ch.receive();
            assertThat(m, nullValue());
            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();
    ch.send(6);
    ch.send(7);
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) 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