use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testZipWithTimeoutsThreadToFiber.
@Test
public void testZipWithTimeoutsThreadToFiber() throws Exception {
final Channel<String> ch1 = newChannel();
final Channel<Integer> ch2 = newChannel();
final Channel<Object> sync = Channels.newChannel(0);
final Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
final ReceivePort<String> ch = Channels.zip(ch1, ch2, new Function2<String, Integer, String>() {
@Override
public String apply(String x1, Integer x2) {
return x1 + x2;
}
});
// 0
sync.receive();
final String m1 = ch.receive(200, TimeUnit.MILLISECONDS);
// 1
sync.receive();
final String m0 = ch.receive(10, TimeUnit.MILLISECONDS);
final String m2 = ch.receive(190, TimeUnit.MILLISECONDS);
// 2
sync.receive();
final String m3 = ch.receive(30, TimeUnit.MILLISECONDS);
// 3
sync.receive();
final String m4 = ch.receive(30, TimeUnit.MILLISECONDS);
assertThat(m1, equalTo("a1"));
assertThat(m0, is(nullValue()));
assertThat(m2, equalTo("b2"));
assertThat(m3, equalTo("c3"));
assertThat(m4, is(nullValue()));
}
}).start();
// 0
sync.send(GO);
Strand.sleep(50);
ch1.send("a", 10, TimeUnit.SECONDS);
ch2.send(1, 10, TimeUnit.SECONDS);
// 1
sync.send(GO);
ch1.send("b", 10, TimeUnit.SECONDS);
Strand.sleep(100);
ch2.send(2, 10, TimeUnit.SECONDS);
// 2
sync.send(GO);
ch1.send("c", 10, TimeUnit.SECONDS);
ch2.send(3, 10, TimeUnit.SECONDS);
// 3
sync.send(GO);
ch1.send("foo", 10, TimeUnit.SECONDS);
// Discards previous
ch2.close();
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testSendReduceThreadToFiber.
@Test
public void testSendReduceThreadToFiber() 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();
Integer m3 = ch.receive();
Integer m4 = ch.receive();
Integer m5 = ch.receive();
Integer m6 = ch.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();
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.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 testSendFilterFiberToFiber.
@Test
public void testSendFilterFiberToFiber() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib1 = 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();
Fiber fib2 = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
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();
fib1.join();
fib2.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testFilterWithTimeouts.
@Test
public void testFilterWithTimeouts() 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 {
final ReceivePort<Integer> ch1 = Channels.filter((ReceivePort<Integer>) ch, new Predicate<Integer>() {
@Override
public boolean apply(Integer input) {
return input % 2 == 0;
}
});
// 0
sync.receive();
final Integer m1 = ch1.receive(200, TimeUnit.MILLISECONDS);
// 1
sync.receive();
final Integer m0 = ch1.receive(10, TimeUnit.MILLISECONDS);
final Integer m2 = ch1.receive(190, TimeUnit.MILLISECONDS);
final Integer m3 = ch1.receive(30, TimeUnit.MILLISECONDS);
assertThat(m1, equalTo(2));
assertThat(m0, is(nullValue()));
assertThat(m2, equalTo(4));
assertThat(m3, is(nullValue()));
}
}).start();
// 0
sync.send(GO);
Strand.sleep(50);
// Discarded (at receive side)
ch.send(1, 10, TimeUnit.SECONDS);
ch.send(2, 10, TimeUnit.SECONDS);
// 1
sync.send(GO);
Strand.sleep(100);
// Discarded (at receive side)
ch.send(3, 10, TimeUnit.SECONDS);
ch.send(4, 10, TimeUnit.SECONDS);
// Discarded (at receive side)
ch.send(5, 10, TimeUnit.SECONDS);
ch.close();
fib.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class TransformingChannelTest method testReduceInitThreadToFiber.
@Test
public void testReduceInitThreadToFiber() 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();
assertThat(m1, equalTo(0));
assertNull(m2);
}
}).start();
Strand.sleep(50);
ch.close();
fib.join();
}
Aggregations