use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class ChannelTest method testChannelCloseException.
@Test
public void testChannelCloseException() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib = new Fiber(scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
for (int i = 1; i <= 5; i++) {
Integer m = ch.receive();
assertThat(m, equalTo(i));
}
try {
ch.receive();
fail();
} catch (ProducerException e) {
assertThat(e.getCause().getMessage(), equalTo("foo"));
}
assertTrue(ch.isClosed());
}
}).start();
Thread.sleep(50);
ch.send(1);
ch.send(2);
ch.send(3);
ch.send(4);
ch.send(5);
ch.close(new Exception("foo"));
ch.send(6);
ch.send(7);
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable 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.strands.SuspendableRunnable 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.strands.SuspendableRunnable 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.strands.SuspendableRunnable 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();
}
Aggregations