Search in sources :

Example 36 with SuspendableRunnable

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

the class TransformingChannelTest method testFlatmapSendThreadToFiber.

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

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            assertThat(ch.receive(), is(1));
            assertThat(ch.receive(), is(20));
            assertThat(ch.receive(), is(200));
            assertThat(ch.receive(), is(2000));
            assertThat(ch.receive(), is(40));
            assertThat(ch.receive(), is(400));
            assertThat(ch.receive(), is(4000));
            assertThat(ch.receive(), is(5));
            assertThat(ch.receive(), is(nullValue()));
            assertThat(ch.isClosed(), is(true));
        }
    }).start();
    SendPort<Integer> ch1 = Channels.flatMapSend(Channels.<Integer>newChannel(1), ch, new Function<Integer, ReceivePort<Integer>>() {

        @Override
        public ReceivePort<Integer> apply(Integer x) {
            if (x == 3)
                return null;
            if (x % 2 == 0)
                return Channels.toReceivePort(Arrays.asList(new Integer[] { x * 10, x * 100, x * 1000 }));
            else
                return Channels.singletonReceivePort(x);
        }
    });
    Strand.sleep(50);
    ch1.send(1);
    ch1.send(2);
    ch1.send(3);
    ch1.send(4);
    ch1.send(5);
    ch1.close();
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 37 with SuspendableRunnable

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

the class FiberAsyncIOTest method testFiberAsyncSocket.

@Test
public void testFiberAsyncSocket() throws Exception {
    final IntChannel sync = Channels.newIntChannel(0);
    final Fiber server = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            try (FiberServerSocketChannel socket = FiberServerSocketChannel.open().bind(new InetSocketAddress(PORT))) {
                // Start client
                sync.send(0);
                try (FiberSocketChannel ch = socket.accept()) {
                    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
                    // long-typed reqeust/response
                    int n = ch.read(buf);
                    // we assume the message is sent in a single packet
                    assertThat(n, is(8));
                    buf.flip();
                    long req = buf.getLong();
                    assertThat(req, is(12345678L));
                    buf.clear();
                    long res = 87654321L;
                    buf.putLong(res);
                    buf.flip();
                    n = ch.write(buf);
                    assertThat(n, is(8));
                    // String reqeust/response
                    buf.clear();
                    // we assume the message is sent in a single packet
                    ch.read(buf);
                    buf.flip();
                    String req2 = decoder.decode(buf).toString();
                    assertThat(req2, is("my request"));
                    String res2 = "my response";
                    ch.write(encoder.encode(CharBuffer.wrap(res2)));
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }).start();
    final Fiber client = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution {
            try {
                // Wait that the server is ready
                sync.receive();
            } catch (InterruptedException ex) {
                // This should never happen
                throw new AssertionError(ex);
            }
            try (FiberSocketChannel ch = FiberSocketChannel.open(new InetSocketAddress(PORT))) {
                ByteBuffer buf = ByteBuffer.allocateDirect(1024);
                // long-typed reqeust/response
                long req = 12345678L;
                buf.putLong(req);
                buf.flip();
                int n = ch.write(buf);
                assertThat(n, is(8));
                buf.clear();
                n = ch.read(buf);
                // we assume the message is sent in a single packet
                assertThat(n, is(8));
                buf.flip();
                long res = buf.getLong();
                assertThat(res, is(87654321L));
                // String reqeust/response
                String req2 = "my request";
                ch.write(encoder.encode(CharBuffer.wrap(req2)));
                buf.clear();
                // we assume the message is sent in a single packet
                ch.read(buf);
                buf.flip();
                String res2 = decoder.decode(buf).toString();
                assertThat(res2, is("my response"));
                // verify that the server has closed the socket
                buf.clear();
                n = ch.read(buf);
                assertThat(n, is(-1));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }).start();
    client.join();
    server.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) InetSocketAddress(java.net.InetSocketAddress) Fiber(co.paralleluniverse.fibers.Fiber) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) IntChannel(co.paralleluniverse.strands.channels.IntChannel) Test(org.junit.Test)

Example 38 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable 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 39 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable 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 40 with SuspendableRunnable

use of co.paralleluniverse.strands.SuspendableRunnable 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)

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