use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class PeerTKB method run.
public void run() throws ExecutionException, InterruptedException {
switch(SCENARIO.testGenEvent) {
case test:
final Store store = Grid.getInstance().store();
if (i == 1) {
StoreTransaction tx = store.beginTransaction();
try {
long root = store.getRoot("root", tx);
// "hello".getBytes();
byte[] buf = null;
store.set(root, buf, tx);
store.commit(tx);
} catch (TimeoutException ex) {
throw new RuntimeException("set failed");
}
Thread.sleep(20000);
} else {
StoreTransaction tx = store.beginTransaction();
byte[] get;
try {
long root = store.getRoot("root", tx);
get = store.get(root);
store.commit(tx);
} catch (TimeoutException ex) {
throw new RuntimeException("get failed");
}
System.out.println(get);
}
break;
case testGenServer:
if (i == 1) {
spawnGenServer(new AbstractServerHandler<Message, Integer, Message>() {
@Override
public void init() throws SuspendExecution {
super.init();
ServerActor.currentServerActor().register("myServer");
}
@Override
public Integer handleCall(ActorRef<?> from, Object id, Message m) {
return m.a + m.b;
}
}).join();
} else {
Integer get = spawnActor(new BasicActor<Message, Integer>(new MailboxConfig(10, Channels.OverflowPolicy.THROW)) {
protected Integer doRun() throws SuspendExecution, InterruptedException {
final Server<Message, Integer, Message> gs = (Server) ActorRegistry.getActor("myServer");
return gs.call(new Message(3, 4));
}
}).get();
System.out.println("value is " + get);
assert get == 7;
}
break;
case testGenEvent:
if (i == 1) {
final Val<String> dv = new Val<>();
spawnGenEvent(new Initializer() {
@Override
public void init() throws SuspendExecution {
EventSourceActor.currentEventSourceActor().register("myEventServer");
try {
final EventSource<String> ge = LocalActor.self();
ge.addHandler(new EventHandler<String>() {
@Override
public void handleEvent(String event) {
dv.set(event);
System.out.println("sout " + event);
ge.shutdown();
}
});
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
@Override
public void terminate(Throwable cause) throws SuspendExecution {
System.out.println("terminated");
}
});
String get = dv.get();
System.out.println("got msg " + get);
assert get.equals("hello world");
} else {
spawnActor(new BasicActor<Message, Void>() {
protected Void doRun() throws SuspendExecution, InterruptedException {
final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
ge.notify("hello world");
return null;
}
}).join();
}
break;
case testMultiGetActor:
if (i == 1) {
spawnGenEvent(new Initializer() {
AtomicInteger ai = new AtomicInteger();
@Override
public void init() throws SuspendExecution {
Actor.currentActor().register("myEventServer");
try {
final EventSource<String> ge = LocalActor.self();
ge.addHandler(new EventHandler<String>() {
@Override
public void handleEvent(String event) {
System.out.println("msg no " + ai.incrementAndGet() + ": " + event);
}
});
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
@Override
public void terminate(Throwable cause) throws SuspendExecution {
System.out.println("terminated");
}
}).join();
} else {
Queue<Actor> queue = new LinkedList<>();
for (int j = 0; j < 1000; j++) {
final BasicActor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>("actor-" + j) {
protected Void doRun() throws SuspendExecution, InterruptedException {
try {
final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
ge.notify("hwf " + getName());
} catch (Exception e) {
System.out.println("error in " + getName());
throw e;
}
return null;
}
});
queue.add(actor);
// actor.join();
}
for (Actor localActor : queue) localActor.join();
Thread.sleep(500);
}
break;
case testOrdering:
if (i == 1) {
spawnGenEvent(new Initializer() {
AtomicInteger ai = new AtomicInteger();
@Override
public void init() throws SuspendExecution {
EventSourceActor.currentEventSourceActor().register("myEventServer");
try {
EventSourceActor<String> ge = EventSourceActor.currentEventSourceActor();
ge.ref().addHandler(new EventHandler<String>() {
@Override
public void handleEvent(String event) {
System.out.println("msg no " + ai.incrementAndGet() + ": " + event);
}
});
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
@Override
public void terminate(Throwable cause) throws SuspendExecution {
System.out.println("terminated");
}
}).join();
} else {
Queue<Actor> queue = new LinkedList<>();
for (int j = 0; j < 1; j++) {
final BasicActor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>("actor-" + j) {
protected Void doRun() throws SuspendExecution, InterruptedException {
try {
final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
for (int k = 0; k < 3000; k++) ge.notify("hw " + k + " f" + getName());
} catch (Exception e) {
System.out.println("error in " + getName());
throw e;
}
return null;
}
});
queue.add(actor);
// actor.join();
}
for (Actor localActor : queue) localActor.join();
Thread.sleep(5000);
}
break;
default:
}
System.out.println("finished");
System.exit(0);
while (true) {
System.out.println("==================");
ThreadUtil.dumpThreads();
Thread.sleep(5000);
}
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class FiniteStateMachineTest method testStates.
@Test
public void testStates() throws Exception {
final AtomicBoolean success = new AtomicBoolean();
ActorRef<Object> a = new FiniteStateMachineActor() {
@Override
protected SuspendableCallable<SuspendableCallable> initialState() {
return new SuspendableCallable<SuspendableCallable>() {
public SuspendableCallable run() throws SuspendExecution, InterruptedException {
return state1();
}
};
}
private SuspendableCallable<SuspendableCallable> state1() throws SuspendExecution, InterruptedException {
return receive(new MessageProcessor<Object, SuspendableCallable<SuspendableCallable>>() {
@Override
public SuspendableCallable<SuspendableCallable> process(Object m) throws SuspendExecution, InterruptedException {
if ("a".equals(m))
return new SuspendableCallable<SuspendableCallable>() {
public SuspendableCallable run() throws SuspendExecution, InterruptedException {
return state2();
}
};
return null;
}
});
}
private SuspendableCallable<SuspendableCallable> state2() throws SuspendExecution, InterruptedException {
return receive(new MessageProcessor<Object, SuspendableCallable<SuspendableCallable>>() {
@Override
public SuspendableCallable<SuspendableCallable> process(Object m) throws SuspendExecution, InterruptedException {
if ("b".equals(m)) {
success.set(true);
return TERMINATE;
}
return null;
}
});
}
}.spawn();
a.send("b");
a.send("a");
LocalActor.join(a, 100, TimeUnit.MILLISECONDS);
assertTrue(success.get());
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class ProxyServerTest method testCast.
@Test
public void testCast() throws Exception {
final AtomicInteger called = new AtomicInteger(0);
final Server<?, ?, ?> a = spawnServer(false, new A() {
public int foo(String str, int x) {
throw new UnsupportedOperationException();
}
@Suspendable
public void bar(int x) {
try {
Strand.sleep(100);
called.set(x);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (SuspendExecution e) {
throw new AssertionError(e);
}
}
});
((A) a).bar(15);
assertThat(called.get(), is(0));
Thread.sleep(200);
assertThat(called.get(), is(15));
a.shutdown();
LocalActor.join(a, 100, TimeUnit.MILLISECONDS);
}
use of co.paralleluniverse.fibers.SuspendExecution in project quasar by puniverse.
the class ServerTest method whenCalledThenDeferredResultIsReturned.
@Test
public void whenCalledThenDeferredResultIsReturned() throws Exception {
final Server<Message, Integer, Message> s = new ServerActor<Message, Integer, Message>() {
private int a, b;
private ActorRef<?> from;
private Object id;
private boolean received;
@Override
public void init() throws SuspendExecution {
setTimeout(50, TimeUnit.MILLISECONDS);
}
@Override
public Integer handleCall(ActorRef<?> from, Object id, Message m) {
// save for later
this.a = m.a;
this.b = m.b;
this.from = from;
this.id = id;
this.received = true;
return null;
}
@Override
protected void handleTimeout() throws SuspendExecution {
if (received) {
reply(from, id, a + b);
shutdown();
}
}
}.spawn();
Actor<Message, Integer> actor = spawnActor(new BasicActor<Message, Integer>(mailboxConfig) {
protected Integer doRun() throws SuspendExecution, InterruptedException {
return s.call(new Message(3, 4));
}
});
int res = actor.get();
assertThat(res, is(7));
LocalActor.join(s, 100, TimeUnit.MILLISECONDS);
}
use of co.paralleluniverse.fibers.SuspendExecution 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();
}
Aggregations