use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransferSelectorTest method testSelectSendWithClose1.
@Test
public void testSelectSendWithClose1() throws Exception {
final Channel<String> channel1 = newChannel();
final Channel<String> channel2 = newChannel();
final Channel<String> channel3 = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
SelectAction<String> sa1 = select(send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
SelectAction<String> sa2 = select(send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
assertThat(sa1.index(), is(1));
assertThat(sa2.index(), is(1));
}
}).start();
Thread.sleep(200);
channel2.close();
fib.join();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransferSelectorTest method testSelectReceive1.
@Test
public void testSelectReceive1() throws Exception {
final Channel<String> channel1 = newChannel();
final Channel<String> channel2 = newChannel();
final Channel<String> channel3 = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
SelectAction<String> sa1 = select(receive(channel1), receive(channel2), receive(channel3));
String m1 = sa1.message();
SelectAction<String> sa2 = select(receive(channel1), receive(channel2), receive(channel3));
String m2 = sa2.message();
assertThat(sa1.index(), is(0));
assertThat(m1, equalTo("hello"));
assertThat(sa2.index(), is(2));
assertThat(m2, equalTo("world!"));
}
}).start();
Thread.sleep(200);
channel1.send("hello");
Thread.sleep(200);
channel3.send("world!");
fib.join();
}
use of co.paralleluniverse.fibers.Fiber 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.fibers.Fiber 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.fibers.Fiber 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();
}
Aggregations