use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransformingChannelTest method testSendSplitThreadToFiber.
@Test
public void testSendSplitThreadToFiber() throws Exception {
final Channel<String> chF1 = newChannel();
final Channel<String> chF2 = newChannel();
final SendPort<String> splitSP = new SplitSendPort<String>() {
@Override
protected SendPort select(final String m) {
if (m.equals("f1"))
return chF1;
else
return chF2;
}
};
final Fiber f1 = new Fiber("split-send-1", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
assertThat(chF1.receive(), is("f1"));
assertThat(chF1.receive(100, TimeUnit.NANOSECONDS), is(nullValue()));
assertThat(chF1.receive(new Timeout(100, TimeUnit.NANOSECONDS)), is(nullValue()));
assertThat(chF1.receive(), is(nullValue()));
assertTrue(chF1.isClosed());
}
}).start();
final Fiber f2 = new Fiber("split-send-2", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
assertThat(chF2.receive(), is(not("f1")));
assertThat(chF2.receive(100, TimeUnit.NANOSECONDS), is(nullValue()));
assertThat(chF2.receive(new Timeout(100, TimeUnit.NANOSECONDS)), is(nullValue()));
assertThat(chF2.receive(), is(nullValue()));
assertTrue(chF2.isClosed());
}
}).start();
splitSP.send("f1");
splitSP.send("f2");
splitSP.close();
assertFalse(chF1.isClosed());
assertFalse(chF2.isClosed());
Thread.sleep(100);
chF1.close();
chF2.close();
f1.join();
f2.join();
}
use of co.paralleluniverse.fibers.Fiber 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.fibers.Fiber 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.fibers.Fiber in project quasar by puniverse.
the class TransformingChannelTest method testFlatmapSendWithTimeoutsThreadToFiber.
//@Test
public void testFlatmapSendWithTimeoutsThreadToFiber() throws Exception {
final Channel<Integer> ch = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
assertThat(ch.receive(), is(1));
assertThat(ch.receive(30, TimeUnit.MILLISECONDS), is(nullValue()));
assertThat(ch.receive(40, TimeUnit.MILLISECONDS), is(20));
assertThat(ch.receive(), is(200));
assertThat(ch.receive(), is(2000));
assertThat(ch.receive(), is(40));
assertThat(ch.receive(), is(400));
assertThat(ch.receive(), is(4000));
assertThat(ch.receive(30, TimeUnit.MILLISECONDS), is(nullValue()));
assertThat(ch.receive(40, TimeUnit.MILLISECONDS), is(5));
assertThat(ch.receive(), is(nullValue()));
assertThat(ch.isClosed(), is(true));
}
}).start();
SendPort<Integer> ch1 = Channels.flatMapSend(Channels.<Integer>newChannel(1), ch, new Function<Integer, ReceivePort<Integer>>() {
@Override
public ReceivePort<Integer> apply(Integer x) {
if (x == 3)
return null;
if (x % 2 == 0)
return Channels.toReceivePort(Arrays.asList(new Integer[] { x * 10, x * 100, x * 1000 }));
else
return Channels.singletonReceivePort(x);
}
});
Strand.sleep(50);
ch1.send(1);
Strand.sleep(50);
ch1.send(2);
ch1.send(3);
ch1.send(4);
Strand.sleep(50);
ch1.send(5);
ch1.close();
fib.join();
}
use of co.paralleluniverse.fibers.Fiber 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();
}
Aggregations