use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testSendFilterWithTimeouts.
@Test
public void testSendFilterWithTimeouts() throws Exception {
final Channel<Integer> ch = newChannel();
final Channel<Object> sync = Channels.newChannel(0);
final Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
// 0
sync.receive();
final Integer m1 = ch.receive(200, TimeUnit.MILLISECONDS);
// 1
sync.receive();
final Integer m0 = ch.receive(10, TimeUnit.MILLISECONDS);
final Integer m2 = ch.receive(190, TimeUnit.MILLISECONDS);
final Integer m3 = ch.receive(30, TimeUnit.MILLISECONDS);
assertThat(m1, equalTo(2));
assertThat(m0, is(nullValue()));
assertThat(m2, equalTo(4));
assertThat(m3, is(nullValue()));
}
}).start();
SendPort<Integer> ch1 = Channels.filterSend((SendPort<Integer>) ch, new Predicate<Integer>() {
@Override
public boolean apply(Integer input) {
return input % 2 == 0;
}
});
// 0
sync.send(GO);
Strand.sleep(50);
// Discarded (at send side)
ch1.send(1, 10, TimeUnit.SECONDS);
ch1.send(2, 10, TimeUnit.SECONDS);
// 1
sync.send(GO);
Strand.sleep(50);
// Discarded (at send side)
ch1.send(3, 10, TimeUnit.SECONDS);
ch1.send(4, 10, TimeUnit.SECONDS);
// Discarded (at send side)
ch1.send(5, 10, TimeUnit.SECONDS);
ch1.close();
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testFlatmapThreadToFiber.
@Test
public void testFlatmapThreadToFiber() throws Exception {
final Channel<Integer> ch1 = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
ReceivePort<Integer> ch = Channels.flatMap(ch1, 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);
}
});
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();
Strand.sleep(50);
ch1.send(1);
ch1.send(2);
ch1.send(3);
ch1.send(4);
ch1.send(5);
ch1.close();
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testSendReduceInitThreadToFiber.
@Test
public void testSendReduceInitThreadToFiber() throws Exception {
final Channel<Integer> ch = newChannel();
final Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
Integer m1 = ch.receive();
Integer m2 = ch.receive();
assertThat(m1, equalTo(0));
assertNull(m2);
}
}).start();
final SendPort<Integer> ch1 = Channels.reduceSend((SendPort<Integer>) ch, new Function2<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer accum, Integer input) {
return accum + input;
}
}, 0);
Strand.sleep(50);
ch1.close();
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable 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.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testSendSplitThreadToFiber.
@Test
public void testSendSplitThreadToFiber() throws Exception {
final Channel<String> chF1 = newChannel();
final Channel<String> chF2 = newChannel();
final SendPort<String> splitSP = new SplitSendPort<String>() {
@Override
protected SendPort select(final String m) {
if (m.equals("f1"))
return chF1;
else
return chF2;
}
};
final Fiber f1 = new Fiber("split-send-1", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
assertThat(chF1.receive(), is("f1"));
assertThat(chF1.receive(100, TimeUnit.NANOSECONDS), is(nullValue()));
assertThat(chF1.receive(new Timeout(100, TimeUnit.NANOSECONDS)), is(nullValue()));
assertThat(chF1.receive(), is(nullValue()));
assertTrue(chF1.isClosed());
}
}).start();
final Fiber f2 = new Fiber("split-send-2", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
assertThat(chF2.receive(), is(not("f1")));
assertThat(chF2.receive(100, TimeUnit.NANOSECONDS), is(nullValue()));
assertThat(chF2.receive(new Timeout(100, TimeUnit.NANOSECONDS)), is(nullValue()));
assertThat(chF2.receive(), is(nullValue()));
assertTrue(chF2.isClosed());
}
}).start();
splitSP.send("f1");
splitSP.send("f2");
splitSP.close();
assertFalse(chF1.isClosed());
assertFalse(chF2.isClosed());
Thread.sleep(100);
chF1.close();
chF2.close();
f1.join();
f2.join();
}
Aggregations