Search in sources :

Example 86 with SuspendableRunnable

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

the class InterfaceTest method testSuspend.

@Test
public void testSuspend() {
    // final I i = new C();
    Fiber co = new Fiber((String) null, null, new SuspendableRunnable() {

        @Override
        public final void run() throws SuspendExecution {
            // next line causes an error because of incomplete merge in TypeInterpreter
            // SomeInterface i = System.currentTimeMillis() > 0 ? new C() : new C2();
            SomeInterface i = new C();
            System.out.println("i = " + i);
            i.doStuff();
        }
    });
    while (!TestsHelper.exec(co)) System.out.println("State=" + co.getState());
    System.out.println("State=" + co.getState());
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 87 with SuspendableRunnable

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

the class VerificationTest method testVerifyUninstrumentedMethod.

@Test
public final void testVerifyUninstrumentedMethod() throws ExecutionException, InterruptedException {
    assumeTrue(SystemProperties.isEmptyOrTrue("co.paralleluniverse.fibers.verifyInstrumentation"));
    final I1 i1 = new C1();
    Throwable t = null;
    final Fiber<?> fUninstrumentedMethod1 = new Fiber<>(new SuspendableRunnable() {

        @Override
        public final void run() throws SuspendExecution, InterruptedException {
            try {
                // **
                doUninstrumented();
                Fiber.sleep(10);
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    }).start();
    try {
        fUninstrumentedMethod1.join();
    } catch (final ExecutionException re) {
        t = re.getCause().getCause();
        t.printStackTrace();
    }
    assertTrue(t instanceof VerifyInstrumentationException && t.getMessage().contains(" **"));
    final Fiber<?> fUninstrumentedMethod2 = new Fiber(new SuspendableRunnable() {

        @Override
        public final void run() throws SuspendExecution, InterruptedException {
            try {
                i1.doIt();
            } finally {
                i1.doIt();
            }
        }
    }).start();
    try {
        fUninstrumentedMethod2.join();
    } catch (final ExecutionException re) {
        t = re.getCause();
        t.printStackTrace();
    }
    assertTrue(t instanceof VerifyInstrumentationException && t.getMessage().contains(" **"));
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) VerifyInstrumentationException(co.paralleluniverse.fibers.VerifyInstrumentationException) VerifyInstrumentationException(co.paralleluniverse.fibers.VerifyInstrumentationException) Test(org.junit.Test)

Example 88 with SuspendableRunnable

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

the class GeneralSelectorTest method fanin.

<Message> Channel<Message> fanin(final ReceivePort<Message>[] ins) {
    final Channel<Message> chan = newChannel();
    spawn(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            for (; ; ) {
                List<SelectAction<Message>> as = new ArrayList<>(ins.length);
                for (ReceivePort<Message> c : ins) as.add(receive(c));
                SelectAction<Message> sa = select(as);
                // System.out.println("Received from " + sa.index());
                Message m = sa.message();
                // System.out.println("fanin: " + m);
                if (m == null) {
                    chan.close();
                    break;
                } else
                    chan.send(m);
            }
        // System.err.println("fanin done");
        }
    });
    return chan;
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) ArrayList(java.util.ArrayList) List(java.util.List)

Example 89 with SuspendableRunnable

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

the class TickerChannelTest method testMultipleConsumersAlwaysAscending.

@Test
public void testMultipleConsumersAlwaysAscending() throws Exception {
    final Channel<Integer> sch = Channels.newChannel(bufferSize, Channels.OverflowPolicy.DISPLACE);
    final SuspendableRunnable run = new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            // System.out.println(Strand.currentStrand() + ": starting");
            final ReceivePort<Integer> ch = Channels.newTickerConsumerFor(sch);
            int prev = -1;
            long prevIndex = -1;
            Integer m;
            while ((m = ch.receive()) != null) {
                // System.out.println(Strand.currentStrand() + ": " + m);
                long index = ((TickerChannelConsumer) ch).getLastIndexRead();
                assertThat("index", index, greaterThan(prevIndex));
                assertThat("message", m.intValue(), greaterThan(prev));
                prev = m;
                prevIndex = index;
            }
            assertThat(ch.isClosed(), is(true));
        }
    };
    int i = 1;
    for (; i < 50; i++) sch.send(i);
    Fiber f1 = new Fiber(scheduler, run).start();
    Thread t1 = new Thread(Strand.toRunnable(run));
    t1.start();
    for (; i < 200; i++) sch.send(i);
    Fiber f2 = new Fiber(scheduler, run).start();
    Thread t2 = new Thread(Strand.toRunnable(run));
    t2.start();
    for (; i < 600; i++) sch.send(i);
    Fiber f3 = new Fiber(scheduler, run).start();
    Thread t3 = new Thread(Strand.toRunnable(run));
    t3.start();
    for (; i < 800; i++) sch.send(i);
    Fiber f4 = new Fiber(scheduler, run).start();
    Thread t4 = new Thread(Strand.toRunnable(run));
    t4.start();
    for (; i < 2000; i++) sch.send(i);
    sch.close();
    System.out.println("done send");
    Debug.dumpAfter(5000, "ticker.log");
    f1.join();
    System.out.println("f1: " + f1);
    f2.join();
    System.out.println("f2: " + f2);
    f3.join();
    System.out.println("f3: " + f3);
    f4.join();
    System.out.println("f4: " + f4);
    t1.join();
    System.out.println("t1: " + t1);
    t2.join();
    System.out.println("t2: " + t2);
    t3.join();
    System.out.println("t3: " + t3);
    t4.join();
    System.out.println("t4: " + t4);
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 90 with SuspendableRunnable

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

the class ValTest method complexTest2.

@Test
public void complexTest2() throws Exception {
    final Val<Integer> val1 = new Val<>();
    final Val<Integer> val2 = new Val<>();
    final Val<Integer> val3 = new Val<>();
    final Val<Integer> val4 = new Val<>();
    final AtomicReference<Integer> res = new AtomicReference<>();
    final Strand f1 = new Fiber<Integer>(new SuspendableRunnable() {

        @Override
        public void run() throws InterruptedException, SuspendExecution {
            // 2
            val2.set(val1.get() + 1);
        }
    }).start();
    final Strand t1 = Strand.of(new Thread(Strand.toRunnable(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution {
            try {
                // 3
                val3.set(val1.get() + val2.get());
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
        }
    }))).start();
    final Strand f2 = new Fiber<Integer>(new SuspendableRunnable() {

        @Override
        public void run() throws InterruptedException, SuspendExecution {
            // 5
            val4.set(val2.get() + val3.get());
        }
    }).start();
    Thread.sleep(100);
    val1.set(1);
    int myRes = val4.get();
    assertThat(myRes, is(5));
    t1.join();
    f1.join();
    f2.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Strand(co.paralleluniverse.strands.Strand) 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