use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransformingChannelTest method testSendFilterThreadToFiber.
@Test
public void testSendFilterThreadToFiber() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
Integer m1 = ch.receive();
Integer m2 = ch.receive();
Integer m3 = ch.receive();
assertThat(m1, equalTo(2));
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;
}
});
Strand.sleep(50);
ch1.send(1);
ch1.send(2);
Strand.sleep(50);
ch1.send(3);
ch1.send(4);
ch1.send(5);
ch1.close();
fib.join();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransformingChannelTest method testReduceThreadToFiber.
@Test
public void testReduceThreadToFiber() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
final ReceivePort<Integer> ch1 = Channels.reduce((ReceivePort<Integer>) ch, new Function2<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer accum, Integer input) {
return accum + input;
}
}, 0);
final Integer m1 = ch1.receive();
final Integer m2 = ch1.receive();
final Integer m3 = ch1.receive();
final Integer m4 = ch1.receive();
final Integer m5 = ch1.receive();
final Integer m6 = ch1.receive();
assertThat(m1, equalTo(1));
assertThat(m2, equalTo(3));
assertThat(m3, equalTo(6));
assertThat(m4, equalTo(10));
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.fibers.Fiber in project quasar by puniverse.
the class TransformingChannelTest method testFilterFiberToFiber.
@Test
public void testFilterFiberToFiber() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib1 = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
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()));
}
}).start();
Fiber fib2 = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
Strand.sleep(50);
ch.send(1);
ch.send(2);
Strand.sleep(50);
ch.send(3);
ch.send(4);
ch.send(5);
ch.close();
}
}).start();
fib1.join();
fib2.join();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransformingChannelTest method testFilterThreadToFiber.
@Test
public void testFilterThreadToFiber() 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.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()));
}
}).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.fibers.Fiber in project quasar by puniverse.
the class TransformingChannelTest method testSendMapThreadToFiber.
@Test
public void testSendMapThreadToFiber() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
Integer m1 = ch.receive();
Integer m2 = ch.receive();
Integer m3 = ch.receive();
Integer m4 = ch.receive();
Integer m5 = ch.receive();
Integer m6 = ch.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();
SendPort<Integer> ch1 = Channels.mapSend((SendPort<Integer>) ch, new Function<Integer, Integer>() {
@Override
public Integer apply(Integer input) {
return input + 10;
}
});
Strand.sleep(50);
ch1.send(1);
ch1.send(2);
Strand.sleep(50);
ch1.send(3);
ch1.send(4);
ch1.send(5);
ch1.close();
fib.join();
}
Aggregations