2009-04-19 10:58:20 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009 Scalable Solutions.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package se.scalablesolutions.akka.api;
|
|
|
|
|
|
|
|
|
|
import se.scalablesolutions.akka.annotation.*;
|
2009-05-09 19:55:42 +02:00
|
|
|
import se.scalablesolutions.akka.kernel.config.*;
|
2009-05-13 19:28:55 +02:00
|
|
|
import se.scalablesolutions.akka.kernel.config.JavaConfig.AllForOne;
|
|
|
|
|
import se.scalablesolutions.akka.kernel.config.JavaConfig.Component;
|
|
|
|
|
import se.scalablesolutions.akka.kernel.config.JavaConfig.LifeCycle;
|
|
|
|
|
import se.scalablesolutions.akka.kernel.config.JavaConfig.Permanent;
|
|
|
|
|
import se.scalablesolutions.akka.kernel.config.JavaConfig.RestartStrategy;
|
2009-05-09 19:55:42 +02:00
|
|
|
import static se.scalablesolutions.akka.kernel.config.JavaConfig.*;
|
2009-05-13 19:28:55 +02:00
|
|
|
import se.scalablesolutions.akka.kernel.Kernel;
|
2009-05-09 19:55:42 +02:00
|
|
|
import se.scalablesolutions.akka.kernel.TransactionalMap;
|
|
|
|
|
import se.scalablesolutions.akka.kernel.CassandraPersistentTransactionalMap;
|
2009-04-19 10:58:20 +02:00
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
|
|
public class PersistentStateTest extends TestCase {
|
|
|
|
|
static String messageLog = "";
|
|
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
static {
|
|
|
|
|
Kernel.startCassandra();
|
|
|
|
|
}
|
2009-05-09 19:55:42 +02:00
|
|
|
final private ActiveObjectGuiceConfiguratorForJava conf = new ActiveObjectGuiceConfiguratorForJava();
|
2009-05-13 19:28:55 +02:00
|
|
|
|
2009-04-19 10:58:20 +02:00
|
|
|
protected void setUp() {
|
|
|
|
|
conf.configureActiveObjects(
|
2009-05-09 19:55:42 +02:00
|
|
|
new JavaConfig.RestartStrategy(new JavaConfig.AllForOne(), 3, 5000),
|
2009-05-13 19:28:55 +02:00
|
|
|
new Component[] {
|
2009-05-09 19:55:42 +02:00
|
|
|
new Component("persistent-stateful", PersistentStateful.class, PersistentStatefulImpl.class, new LifeCycle(new Permanent(), 1000), 10000000),
|
|
|
|
|
new Component("persistent-failer", PersistentFailer.class, PersistentFailerImpl.class, new LifeCycle(new Permanent(), 1000), 1000),
|
|
|
|
|
new Component("persistent-clasher", PersistentClasher.class, PersistentClasherImpl.class, new LifeCycle(new Permanent(), 1000), 100000)
|
2009-05-13 19:28:55 +02:00
|
|
|
}).supervise();
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
protected void tearDown() {
|
|
|
|
|
conf.stop();
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-19 10:58:20 +02:00
|
|
|
public void testShouldNotRollbackStateForStatefulServerInCaseOfSuccess() {
|
2009-05-13 19:28:55 +02:00
|
|
|
PersistentStateful stateful = conf.getActiveObject("persistent-stateful");
|
|
|
|
|
stateful.setState("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "init"); // set init state
|
|
|
|
|
stateful.success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state"); // transactional
|
|
|
|
|
stateful.success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state"); // to trigger commit
|
|
|
|
|
assertEquals("new state", stateful.getState("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void testShouldRollbackStateForStatefulServerInCaseOfFailure() {
|
|
|
|
|
PersistentStateful stateful = conf.getActiveObject("persistent-stateful");
|
|
|
|
|
stateful.setState("testShouldRollbackStateForStatefulServerInCaseOfFailure", "init"); // set init state
|
|
|
|
|
PersistentFailer failer = conf.getActiveObject("persistent-failer");
|
|
|
|
|
try {
|
|
|
|
|
stateful.failure("testShouldRollbackStateForStatefulServerInCaseOfFailure", "new state", failer); // call failing transactional method
|
|
|
|
|
fail("should have thrown an exception");
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
} // expected
|
|
|
|
|
assertEquals("init", stateful.getState("testShouldRollbackStateForStatefulServerInCaseOfFailure")); // check that state is == init state
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
interface PersistentStateful {
|
|
|
|
|
@transactional
|
|
|
|
|
public void success(String key, String msg);
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
@transactional
|
|
|
|
|
public void failure(String key, String msg, PersistentFailer failer);
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
@transactional
|
|
|
|
|
public void clashOk(String key, String msg, PersistentClasher clasher);
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
@transactional
|
|
|
|
|
public void clashNotOk(String key, String msg, PersistentClasher clasher);
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public String getState(String key);
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public void setState(String key, String value);
|
|
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
class PersistentStatefulImpl implements PersistentStateful {
|
2009-05-09 19:55:42 +02:00
|
|
|
private TransactionalMap state = new CassandraPersistentTransactionalMap(this);
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public String getState(String key) {
|
2009-05-13 19:28:55 +02:00
|
|
|
return (String)state.get(key).get();
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public void setState(String key, String msg) {
|
|
|
|
|
state.put(key, msg);
|
|
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public void success(String key, String msg) {
|
|
|
|
|
state.put(key, msg);
|
|
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public void failure(String key, String msg, PersistentFailer failer) {
|
|
|
|
|
state.put(key, msg);
|
|
|
|
|
failer.fail();
|
|
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public void clashOk(String key, String msg, PersistentClasher clasher) {
|
|
|
|
|
state.put(key, msg);
|
|
|
|
|
clasher.clash();
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public void clashNotOk(String key, String msg, PersistentClasher clasher) {
|
|
|
|
|
state.put(key, msg);
|
|
|
|
|
clasher.clash();
|
|
|
|
|
clasher.clash();
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
interface PersistentFailer {
|
|
|
|
|
public void fail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class PersistentFailerImpl implements PersistentFailer {
|
|
|
|
|
public void fail() {
|
|
|
|
|
throw new RuntimeException("expected");
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
interface PersistentClasher {
|
|
|
|
|
public void clash();
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public String getState(String key);
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public void setState(String key, String value);
|
|
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
class PersistentClasherImpl implements PersistentClasher {
|
2009-05-09 19:55:42 +02:00
|
|
|
private TransactionalMap state = new CassandraPersistentTransactionalMap(this);
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public String getState(String key) {
|
2009-05-13 19:28:55 +02:00
|
|
|
return (String)state.get(key).get();
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public void setState(String key, String msg) {
|
|
|
|
|
state.put(key, msg);
|
|
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
public void clash() {
|
|
|
|
|
state.put("clasher", "was here");
|
|
|
|
|
// spend some time here
|
|
|
|
|
|
|
|
|
|
// FIXME: this statement gives me this error:
|
|
|
|
|
// se.scalablesolutions.akka.kernel.ActiveObjectException:
|
|
|
|
|
// Unexpected message [!(scala.actors.Channel@c2b2f6,ErrRef[Right(null)])]
|
|
|
|
|
// to
|
|
|
|
|
// [GenericServer[se.scalablesolutions.akka.api.StatefulImpl]] from
|
|
|
|
|
// [GenericServer[se.scalablesolutions.akka.api.ClasherImpl]]]
|
|
|
|
|
// try { Thread.sleep(1000); } catch (InterruptedException e) {}
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
}
|