use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testFilterFiberToThread.
@Test
public void testFilterFiberToThread() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
Fiber.sleep(100);
Strand.sleep(50);
ch.send(1);
ch.send(2);
Strand.sleep(50);
ch.send(3);
ch.send(4);
ch.send(5);
ch.close();
}
}).start();
ReceivePort<Integer> ch1 = Channels.filter((ReceivePort<Integer>) ch, new Predicate<Integer>() {
@Override
public boolean apply(Integer input) {
return input % 2 == 0;
}
});
Integer m1 = ch1.receive();
Integer m2 = ch1.receive();
Integer m3 = ch1.receive();
assertThat(m1, equalTo(2));
assertThat(m2, equalTo(4));
assertThat(m3, is(nullValue()));
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testMapThreadToFiber.
@Test
public void testMapThreadToFiber() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
ReceivePort<Integer> ch1 = Channels.map((ReceivePort<Integer>) ch, new Function<Integer, Integer>() {
@Override
public Integer apply(Integer input) {
return input + 10;
}
});
Integer m1 = ch1.receive();
Integer m2 = ch1.receive();
Integer m3 = ch1.receive();
Integer m4 = ch1.receive();
Integer m5 = ch1.receive();
Integer m6 = ch1.receive();
assertThat(m1, equalTo(11));
assertThat(m2, equalTo(12));
assertThat(m3, equalTo(13));
assertThat(m4, equalTo(14));
assertThat(m5, equalTo(15));
assertThat(m6, is(nullValue()));
}
}).start();
Strand.sleep(50);
ch.send(1);
ch.send(2);
Strand.sleep(50);
ch.send(3);
ch.send(4);
ch.send(5);
ch.close();
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable 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.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testZipThreadToFiber.
@Test
public void testZipThreadToFiber() throws Exception {
final Channel<String> ch1 = newChannel();
final Channel<Integer> ch2 = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
ReceivePort<String> ch = Channels.zip(ch1, ch2, new Function2<String, Integer, String>() {
@Override
public String apply(String x1, Integer x2) {
return x1 + x2;
}
});
String m1 = ch.receive();
String m2 = ch.receive();
String m3 = ch.receive();
String m4 = ch.receive();
assertThat(m1, equalTo("a1"));
assertThat(m2, equalTo("b2"));
assertThat(m3, equalTo("c3"));
assertThat(m4, is(nullValue()));
}
}).start();
Strand.sleep(50);
ch1.send("a");
ch2.send(1);
ch1.send("b");
ch2.send(2);
ch1.send("c");
ch2.send(3);
ch1.send("foo");
ch2.close();
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testSendFilterFiberToThread.
@Test
public void testSendFilterFiberToThread() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
Fiber.sleep(100);
SendPort<Integer> ch1 = Channels.filterSend((SendPort<Integer>) ch, new Predicate<Integer>() {
@Override
public boolean apply(Integer input) {
return input % 2 == 0;
}
});
Strand.sleep(50);
ch1.send(1);
ch1.send(2);
Strand.sleep(50);
ch1.send(3);
ch1.send(4);
ch1.send(5);
ch1.close();
}
}).start();
Integer m1 = ch.receive();
Integer m2 = ch.receive();
Integer m3 = ch.receive();
assertThat(m1, equalTo(2));
assertThat(m2, equalTo(4));
assertThat(m3, is(nullValue()));
fib.join();
}
Aggregations