Package co.paralleluniverse.fibers

Examples of co.paralleluniverse.fibers.Fiber


    @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();
    }
View Full Code Here


    //@Test
    public void testFlatmapSendWithTimeoutsThreadToFiber() 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(30, TimeUnit.MILLISECONDS), is(nullValue()));
                assertThat(ch.receive(40, TimeUnit.MILLISECONDS), 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(30, TimeUnit.MILLISECONDS), is(nullValue()));
                assertThat(ch.receive(40, TimeUnit.MILLISECONDS), 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);
        Strand.sleep(50);
        ch1.send(2);
        ch1.send(3);
        ch1.send(4);
        Strand.sleep(50);
        ch1.send(5);
        ch1.close();
        fib.join();
    }
View Full Code Here

    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();
    }
View Full Code Here

    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();
    }
View Full Code Here

    public void testSelectReceiveWithClose1() 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(2));
                assertThat(m1, nullValue());
                assertThat(m2, nullValue());
            }
        }).start();

        Thread.sleep(200);
        channel3.close();

        fib.join();
    }
View Full Code Here

    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();
    }
View Full Code Here

    public void testSelectReceiveTimeout() 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(1, TimeUnit.MILLISECONDS,
                        receive(channel1),
                        receive(channel2),
                        receive(channel3));

                SelectAction<String> sa2 = select(300, TimeUnit.MILLISECONDS,
                        receive(channel1),
                        receive(channel2),
                        receive(channel3));
                String m2 = sa2.message();

                assertThat(sa1, is(nullValue()));
                assertThat(sa2.index(), is(0));
                assertThat(m2, equalTo("hello"));
            }
        }).start();

        Thread.sleep(200);

        channel1.send("hello");

        fib.join();
    }
View Full Code Here

    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();
    }
View Full Code Here

    public void testSelectSend1() 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(0));
            }
        }).start();

        Thread.sleep(200);

        String m1 = channel2.receive();
        assertThat(m1, equalTo("hi2"));

        Thread.sleep(200);
        String m2 = channel1.receive();
        assertThat(m2, equalTo("hi1"));

        fib.join();
    }
View Full Code Here

    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();
    }
View Full Code Here

TOP

Related Classes of co.paralleluniverse.fibers.Fiber

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.