use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class Server method main.
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.setProperty("galaxy.nodeId", Integer.toString(nodeId));
System.setProperty("galaxy.port", Integer.toString(7050 + nodeId));
System.setProperty("galaxy.slave_port", Integer.toString(8050 + nodeId));
new Fiber(new ServerActor(new AbstractServerHandler<SumRequest, Integer, SumRequest>() {
@Override
public void init() throws SuspendExecution {
super.init();
Actor.currentActor().register("myServer");
System.out.println(this.toString() + " is ready");
}
@Override
public Integer handleCall(ActorRef<?> from, Object id, SumRequest m) {
System.out.println(this.toString() + " is handling " + m);
if (m.a == 0 && m.b == 0)
ServerActor.currentServerActor().shutdown();
return m.a + m.b;
}
})).start().join();
System.exit(0);
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransferSelectorTest method testSelectReceiveTimeout.
@Test
public void testSelectReceiveTimeout() 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, receive(channel1), receive(channel2), receive(channel3));
SelectAction<String> sa2 = select(300, TimeUnit.MILLISECONDS, receive(channel1), receive(channel2), receive(channel3));
String m2 = sa2.message();
assertThat(sa1, is(nullValue()));
assertThat(sa2.index(), is(0));
assertThat(m2, equalTo("hello"));
}
}).start();
Thread.sleep(200);
channel1.send("hello");
fib.join();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransferSelectorTest method testSelectReceiveWithClose1.
@Test
public void testSelectReceiveWithClose1() 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(2));
assertThat(m1, nullValue());
assertThat(m2, nullValue());
}
}).start();
Thread.sleep(200);
channel3.close();
fib.join();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class TransformingChannelTest method testFlatmapWithTimeoutsThreadToFiber.
@Test
public void testFlatmapWithTimeoutsThreadToFiber() throws Exception {
final Channel<Integer> ch1 = 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> ch = Channels.flatMap(ch1, new Function<Integer, ReceivePort<Integer>>() {
@Override
public ReceivePort<Integer> apply(Integer x) {
if (x == 3)
// Discard
return null;
if (x % 2 == 0)
return Channels.toReceivePort(Arrays.asList(new Integer[] { x * 10, x * 100, x * 1000 }));
else
return Channels.singletonReceivePort(x);
}
});
// 0
sync.receive();
assertThat(ch.receive(200, TimeUnit.MILLISECONDS), is(1));
// 1
sync.receive();
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(nullValue()));
assertThat(ch.receive(190, TimeUnit.MILLISECONDS), is(20));
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(200));
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(2000));
// 2
sync.receive();
assertThat(ch.receive(200, TimeUnit.MILLISECONDS), is(40));
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(400));
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(4000));
// 3
sync.receive();
assertThat(ch.receive(10, TimeUnit.MILLISECONDS), is(nullValue()));
assertThat(ch.receive(190, TimeUnit.MILLISECONDS), is(5));
// 4
sync.receive();
assertThat(ch.receive(30, TimeUnit.MILLISECONDS), is(nullValue()));
assertThat(ch.isClosed(), is(true));
}
}).start();
// 0
sync.send(GO);
Strand.sleep(50);
ch1.send(1, 10, TimeUnit.SECONDS);
// 1
sync.send(GO);
Strand.sleep(100);
ch1.send(2, 10, TimeUnit.SECONDS);
// 2
sync.send(GO);
// Discarded
ch1.send(3, 10, TimeUnit.SECONDS);
ch1.send(4, 10, TimeUnit.SECONDS);
// 3
sync.send(GO);
Strand.sleep(50);
ch1.send(5, 10, TimeUnit.SECONDS);
// 4
sync.send(GO);
ch1.close();
fib.join();
}
use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.
the class ArrayTest method testArray.
@Test
public void testArray() {
Fiber co = new Fiber((String) null, null, this);
TestsHelper.exec(co);
assertEquals(42, l1.i);
}
Aggregations