use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class InstrumentationOptimizerTest method testSkipForwardsToSuspendableDouble.
@Test
public void testSkipForwardsToSuspendableDouble() throws InterruptedException, SuspendExecution, ExecutionException {
new Fiber(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
skipForwardsToSuspendableDouble();
}
}).start().join();
assertTrue(isOptimized("skipForwardsToSuspendableDouble"));
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class TwoSidedTest method twoSidedTestWithProcessor.
@Test
public void twoSidedTestWithProcessor() throws Exception {
// Publisher
final Channel<Integer> publisherChannel = Channels.newChannel(random() ? 0 : 5, OverflowPolicy.BLOCK);
final Strand publisherStrand = new Fiber<Void>(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
for (long i = 0; i < ELEMENTS; i++) publisherChannel.send((int) (i % 1000));
publisherChannel.close();
}
}).start();
final Publisher<Integer> publisher = ReactiveStreams.toPublisher(publisherChannel);
// Processor
final Processor<Integer, Integer> processor = ReactiveStreams.toProcessor(5, OverflowPolicy.BLOCK, new SuspendableAction2<ReceivePort<Integer>, SendPort<Integer>>() {
@Override
public void call(ReceivePort<Integer> in, SendPort<Integer> out) throws SuspendExecution, InterruptedException {
long count = 0;
for (Integer element; ((element = in.receive()) != null); count++) {
out.send(element * 10);
out.send(element * 100);
// Fiber.sleep(1); // just for fun
assertTrue(count < ELEMENTS);
}
assertEquals(ELEMENTS, count);
out.close();
}
});
publisher.subscribe(processor);
// Subscriber
final ReceivePort<Integer> subscriberChannel = ReactiveStreams.subscribe(buffer, overflowPolicy, processor);
final Strand subscriberStrand = new Fiber<Void>(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
long count = 0;
for (; ; ) {
Integer x = subscriberChannel.receive();
if (x == null)
break;
assertTrue(x % 10 == 0);
if (count % 2 != 0)
assertTrue(x % 100 == 0);
count++;
}
subscriberChannel.close();
assertEquals(ELEMENTS * 2, count);
}
}).start();
subscriberStrand.join(5, TimeUnit.SECONDS);
publisherStrand.join(5, TimeUnit.SECONDS);
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class TransformingChannelTest method testFiberTransform1.
@Test
public void testFiberTransform1() throws Exception {
final Channel<Integer> in = newChannel();
final Channel<Integer> out = newChannel();
Channels.fiberTransform(in, out, new SuspendableAction2<ReceivePort<Integer>, SendPort<Integer>>() {
@Override
public void call(ReceivePort<Integer> in, SendPort<Integer> out) throws SuspendExecution, InterruptedException {
Integer x;
while ((x = in.receive()) != null) {
if (x % 2 == 0)
out.send(x * 10);
}
out.send(1234);
out.close();
}
});
Fiber fib1 = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
assertThat(out.receive(), equalTo(20));
assertThat(out.receive(), equalTo(40));
assertThat(out.receive(), equalTo(1234));
assertThat(out.receive(), is(nullValue()));
}
}).start();
Fiber fib2 = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
Strand.sleep(50);
in.send(1);
in.send(2);
Strand.sleep(50);
in.send(3);
in.send(4);
in.send(5);
in.close();
}
}).start();
fib1.join();
fib2.join();
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class GlxRemoteChannel method submitSend.
private static void submitSend(final Object message, final GlxGlobalChannelId id) {
LOG.debug("sending: " + message);
sendThreadPool.submit(new Runnable() {
@Override
public void run() {
try {
send(message, id);
} catch (SuspendExecution e) {
throw new AssertionError(e);
}
}
});
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class InstrumentationOptimizerTest method testDontSkipForwardsWithMethodBefore.
@Test
public void testDontSkipForwardsWithMethodBefore() throws InterruptedException, SuspendExecution, ExecutionException {
new Fiber(new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
dontSkipForwardsWithMethodBefore();
}
}).start().join();
assertFalse(isOptimized("skipForwardsWithMethodBefore"));
}
Aggregations