use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class PipelineTest method testPipeline.
@Test
public void testPipeline() throws Exception {
final Channel<Integer> i = newChannel();
final Channel<Integer> o = newChannel();
final Pipeline<Integer, Integer> p = new Pipeline<>(i, o, new SuspendableAction2<Integer, Channel<Integer>>() {
@Override
public void call(final Integer i, final Channel<Integer> out) throws SuspendExecution, InterruptedException {
out.send(i + 1);
out.close();
}
}, parallelism);
final Fiber<Long> pf = new Fiber("pipeline", scheduler, p).start();
final Fiber receiver = new Fiber("receiver", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
final Integer m1 = o.receive();
final Integer m2 = o.receive();
final Integer m3 = o.receive();
final Integer m4 = o.receive();
assertThat(m1, notNullValue());
assertThat(m2, notNullValue());
assertThat(m3, notNullValue());
assertThat(m4, notNullValue());
assertThat(ImmutableSet.of(m1, m2, m3, m4), equalTo(ImmutableSet.of(2, 3, 4, 5)));
try {
pf.join();
} catch (ExecutionException ex) {
// It should never happen
throw new AssertionError(ex);
}
// This is needed, else `isClosed` could return false
assertNull(o.tryReceive());
// Can be used reliably only in owner (receiver)
assertTrue(o.isClosed());
}
}).start();
i.send(1);
i.send(2);
i.send(3);
i.send(4);
i.close();
// Join pipeline
long transferred = pf.get();
assertThat(transferred, equalTo(p.getTransferred()));
assertThat(transferred, equalTo(4l));
receiver.join();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class ChannelTest method whenChannelClosedThenBlockedSendsComplete.
@Test
public void whenChannelClosedThenBlockedSendsComplete() throws Exception {
assumeThat(policy, is(OverflowPolicy.BLOCK));
final Channel<Integer> ch = newChannel();
final SuspendableRunnable r = new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
for (int i = 1; i <= 100; i++) {
ch.send(i);
}
}
};
Fiber fib1 = new Fiber("fiber", scheduler, r).start();
Fiber fib2 = new Fiber("fiber", scheduler, r).start();
Thread.sleep(500);
ch.close();
fib1.join();
fib2.join();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class ChannelTest method testChannelGroupReceive.
@Test
public void testChannelGroupReceive() throws Exception {
final Channel<String> channel1 = newChannel();
final Channel<String> channel2 = newChannel();
final Channel<String> channel3 = newChannel();
final ReceivePortGroup<String> group = new ReceivePortGroup<>(channel1, channel2, channel3);
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
String m1 = group.receive();
String m2 = channel2.receive();
assertThat(m1, equalTo("hello"));
assertThat(m2, equalTo("world!"));
}
}).start();
Thread.sleep(100);
channel3.send("hello");
Thread.sleep(100);
if (policy != OverflowPolicy.BLOCK) {
// TransferChannel will block here
channel1.send("goodbye");
Thread.sleep(100);
}
channel2.send("world!");
fib.join();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class ChannelTest method sendMessageFromThreadToFiber.
@Test
public void sendMessageFromThreadToFiber() throws Exception {
final Channel<String> ch = newChannel();
Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
String m = ch.receive();
assertThat(m, equalTo("a message"));
}
}).start();
Thread.sleep(50);
ch.send("a message");
fib.join();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransferSelectorTest method testSelectSendTimeout.
@Test
public void testSelectSendTimeout() 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(1, TimeUnit.MILLISECONDS, send(channel1, "hi1"), send(channel2, "hi2"), send(channel3, "hi3"));
SelectAction<String> sa2 = select(300, TimeUnit.MILLISECONDS, send(channel1, "bye1"), send(channel2, "bye2"), send(channel3, "bye3"));
assertThat(sa1, is(nullValue()));
assertThat(sa2.index(), is(2));
}
}).start();
Thread.sleep(200);
String m1 = channel3.receive();
// the first send is cancelled
assertThat(m1, equalTo("bye3"));
fib.join();
}
Aggregations