renamed api-java to fun-test-java + upgrade guiceyfruit to 2.0

This commit is contained in:
Jonas Boner 2009-05-25 15:52:45 +02:00
parent 8c11f04262
commit 9235b0e221
40 changed files with 363 additions and 630 deletions

View file

@ -0,0 +1,113 @@
/**
* Copyright (C) 2009 Scalable Solutions.
*/
package se.scalablesolutions.akka.api;
import se.scalablesolutions.akka.annotation.*;
import se.scalablesolutions.akka.kernel.config.ActiveObjectGuiceConfiguratorForJava;
import se.scalablesolutions.akka.annotation.*;
import se.scalablesolutions.akka.kernel.config.*;
import static se.scalablesolutions.akka.kernel.config.JavaConfig.*;
import se.scalablesolutions.akka.kernel.TransactionalMap;
import se.scalablesolutions.akka.kernel.InMemoryTransactionalMap;
import com.google.inject.Inject;
import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import junit.framework.TestCase;
public class ActiveObjectGuiceConfiguratorTest extends TestCase {
static String messageLog = "";
final private ActiveObjectGuiceConfiguratorForJava conf = new ActiveObjectGuiceConfiguratorForJava();
protected void setUp() {
conf.addExternalGuiceModule(new AbstractModule() {
protected void configure() {
bind(Ext.class).to(ExtImpl.class).in(Scopes.SINGLETON);
}
}).configureActiveObjects(
new RestartStrategy(new AllForOne(), 3, 5000), new Component[]{
new Component(
Foo.class,
new LifeCycle(new Permanent(), 1000),
10000),
new Component(
Bar.class,
BarImpl.class,
new LifeCycle(new Permanent(), 1000),
10000)
}).inject().supervise();
}
public void testGuiceActiveObjectInjection() {
messageLog = "";
Foo foo = conf.getActiveObject(Foo.class);
Bar bar = conf.getActiveObject(Bar.class);
assertEquals(foo.getBar(), bar);
}
public void testGuiceExternalDependencyInjection() {
messageLog = "";
Bar bar = conf.getActiveObject(Bar.class);
Ext ext = conf.getExternalDependency(Ext.class);
assertTrue(bar.getExt().toString().equals(ext.toString()));
}
public void testLookupNonSupervisedInstance() {
try {
String str = conf.getActiveObject(String.class);
fail("exception should have been thrown");
} catch (Exception e) {
assertEquals("Class [java.lang.String] has not been put under supervision (by passing in the config to the 'configureActiveObjects' and then invoking 'supervise') method", e.getMessage());
}
}
public void testActiveObjectInvocation() throws InterruptedException {
messageLog = "";
Foo foo = conf.getActiveObject(Foo.class);
messageLog += foo.foo("foo ");
foo.bar("bar ");
messageLog += "before_bar ";
Thread.sleep(500);
assertEquals("foo return_foo before_bar ", messageLog);
}
public void testActiveObjectInvocationsInvocation() throws InterruptedException {
messageLog = "";
Foo foo = conf.getActiveObject(Foo.class);
Bar bar = conf.getActiveObject(Bar.class);
messageLog += foo.foo("foo ");
foo.bar("bar ");
messageLog += "before_bar ";
Thread.sleep(500);
assertEquals("foo return_foo before_bar ", messageLog);
}
public void testForcedTimeout() {
messageLog = "";
Foo foo = conf.getActiveObject(Foo.class);
try {
foo.longRunning();
fail("exception should have been thrown");
} catch (se.scalablesolutions.akka.kernel.ActiveObjectInvocationTimeoutException e) {
}
}
public void testForcedException() {
messageLog = "";
Foo foo = conf.getActiveObject(Foo.class);
try {
foo.throwsException();
fail("exception should have been thrown");
} catch (RuntimeException e) {
}
}
}

View file

@ -0,0 +1,9 @@
package se.scalablesolutions.akka.api;
import se.scalablesolutions.akka.annotation.oneway;
public interface Bar {
@oneway
void bar(String msg);
Ext getExt();
}

View file

@ -0,0 +1,13 @@
package se.scalablesolutions.akka.api;
import com.google.inject.Inject;
public class BarImpl implements Bar {
@Inject
private Ext ext;
public Ext getExt() {
return ext;
}
public void bar(String msg) {
}
}

View file

@ -0,0 +1,6 @@
package se.scalablesolutions.akka.api;
public interface Ext {
void ext();
}

View file

@ -0,0 +1,6 @@
package se.scalablesolutions.akka.api;
public class ExtImpl implements Ext {
public void ext() {
}
}

View file

@ -0,0 +1,28 @@
package se.scalablesolutions.akka.api;
import com.google.inject.Inject;
import se.scalablesolutions.akka.annotation.oneway;
public class Foo {
@Inject
private Bar bar;
public Bar getBar() {
return bar;
}
public String foo(String msg) {
return msg + "return_foo ";
}
@oneway
public void bar(String msg) {
bar.bar(msg);
}
public void longRunning() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
public void throwsException() {
throw new RuntimeException("expected");
}
}

View file

@ -0,0 +1,7 @@
package se.scalablesolutions.akka.api;
public class InMemFailer {
public void fail() {
throw new RuntimeException("expected");
}
}

View file

@ -0,0 +1,68 @@
package se.scalablesolutions.akka.api;
import se.scalablesolutions.akka.annotation.state;
import se.scalablesolutions.akka.annotation.transactional;
import se.scalablesolutions.akka.kernel.*;
public class InMemStateful {
@state private TransactionalMap<String, String> mapState = new InMemoryTransactionalMap<String, String>();
@state private TransactionalVector<String> vectorState = new InMemoryTransactionalVector<String>();
@state private TransactionalRef<String> refState = new TransactionalRef<String>();
public String getMapState(String key) {
return (String)mapState.get(key).get();
}
public String getVectorState() {
return (String)vectorState.last();
}
public String getRefState() {
return (String)refState.get().get();
}
public void setMapState(String key, String msg) {
mapState.put(key, msg);
}
public void setVectorState(String msg) {
vectorState.add(msg);
}
public void setRefState(String msg) {
refState.swap(msg);
}
@transactional
public void success(String key, String msg) {
mapState.put(key, msg);
//vectorState.add(msg);
//refState.swap(msg);
}
@transactional
public void failure(String key, String msg, InMemFailer failer) {
mapState.put(key, msg);
//vectorState.add(msg);
//refState.swap(msg);
failer.fail();
}
@transactional
public void thisMethodHangs(String key, String msg, InMemFailer failer) {
setMapState(key, msg);
}
/*
public void clashOk(String key, String msg, InMemClasher clasher) {
mapState.put(key, msg);
clasher.clash();
}
public void clashNotOk(String key, String msg, InMemClasher clasher) {
mapState.put(key, msg);
clasher.clash();
this.success("clash", "clash");
}
*/
}

View file

@ -0,0 +1,155 @@
/**
* Copyright (C) 2009 Scalable Solutions.
*/
package se.scalablesolutions.akka.api;
import se.scalablesolutions.akka.kernel.config.*;
import static se.scalablesolutions.akka.kernel.config.JavaConfig.*;
import junit.framework.TestCase;
public class InMemoryStateTest extends TestCase {
static String messageLog = "";
final private ActiveObjectGuiceConfiguratorForJava conf = new ActiveObjectGuiceConfiguratorForJava();
protected void setUp() {
conf.configureActiveObjects(
new RestartStrategy(new AllForOne(), 3, 5000),
new Component[] {
// FIXME: remove string-name, add ctor to only accept target class
new Component(InMemStateful.class, new LifeCycle(new Permanent(), 1000), 10000000),
new Component(InMemFailer.class, new LifeCycle(new Permanent(), 1000), 1000)
//new Component("inmem-clasher", InMemClasher.class, InMemClasherImpl.class, new LifeCycle(new Permanent(), 1000), 100000)
}).inject().supervise();
}
protected void tearDown() {
conf.stop();
}
public void testMapShouldNotRollbackStateForStatefulServerInCaseOfSuccess() {
InMemStateful stateful = conf.getActiveObject(InMemStateful.class);
stateful.setMapState("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "init"); // set init state
stateful.success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state"); // transactional
stateful.success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state"); // to trigger commit
assertEquals("new state", stateful.getMapState("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess"));
}
public void testMapShouldRollbackStateForStatefulServerInCaseOfFailure() {
InMemStateful stateful = conf.getActiveObject(InMemStateful.class);
stateful.setMapState("testShouldRollbackStateForStatefulServerInCaseOfFailure", "init"); // set init state
InMemFailer failer = conf.getActiveObject(InMemFailer.class);
try {
stateful.failure("testShouldRollbackStateForStatefulServerInCaseOfFailure", "new state", failer); // call failing transactional method
fail("should have thrown an exception");
} catch (RuntimeException e) {
} // expected
assertEquals("init", stateful.getMapState("testShouldRollbackStateForStatefulServerInCaseOfFailure")); // check that state is == init state
}
public void testVectorShouldNotRollbackStateForStatefulServerInCaseOfSuccess() {
InMemStateful stateful = conf.getActiveObject(InMemStateful.class);
stateful.setVectorState("init"); // set init state
stateful.success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state"); // transactional
stateful.success("testShouldNotRollbackStateForStatefulServerInCaseOfSuccess", "new state"); // to trigger commit
assertEquals("new state", stateful.getVectorState());
}
public void testVectorShouldRollbackStateForStatefulServerInCaseOfFailure() {
InMemStateful stateful = conf.getActiveObject(InMemStateful.class);
stateful.setVectorState("init"); // set init state
InMemFailer failer = conf.getActiveObject(InMemFailer.class);
try {
stateful.failure("testShouldRollbackStateForStatefulServerInCaseOfFailure", "new state", failer); // call failing transactional method
fail("should have thrown an exception");
} catch (RuntimeException e) {
} // expected
assertEquals("init", stateful.getVectorState()); // check that state is == init state
}
public void testNestedNonTransactionalMethodHangs() {
InMemStateful stateful = conf.getActiveObject(InMemStateful.class);
stateful.setMapState("testShouldRollbackStateForStatefulServerInCaseOfFailure", "init"); // set init state
InMemFailer failer = conf.getActiveObject(InMemFailer.class);
try {
stateful.thisMethodHangs("testShouldRollbackStateForStatefulServerInCaseOfFailure", "new state", failer); // call failing transactional method
fail("should have thrown an exception");
} catch (RuntimeException e) {
} // expected
assertEquals("init", stateful.getMapState("testShouldRollbackStateForStatefulServerInCaseOfFailure")); // check that state is == init state
}
/*
*/
// public void testShouldRollbackStateForStatefulServerInCaseOfMessageClash()
// {
// InMemStateful stateful = conf.getActiveObject(InMemStateful.class);
// stateful.setState("stateful", "init"); // set init state
//
// InMemClasher clasher = conf.getActiveObject(InMemClasher.class);
// clasher.setState("clasher", "init"); // set init state
//
// // try {
// // stateful.clashOk("stateful", "new state", clasher);
// // } catch (RuntimeException e) { } // expected
// // assertEquals("new state", stateful.getState("stateful")); // check that
// // state is == init state
// // assertEquals("was here", clasher.getState("clasher")); // check that
// // state is == init state
//
// try {
// stateful.clashNotOk("stateful", "new state", clasher);
// fail("should have thrown an exception");
// } catch (RuntimeException e) {
// System.out.println(e);
// } // expected
// assertEquals("init", stateful.getState("stateful")); // check that state is
// // == init state
// // assertEquals("init", clasher.getState("clasher")); // check that state
// is
// // == init state
// }
}
/*
interface InMemClasher {
public void clash();
public String getState(String key);
public void setState(String key, String value);
}
class InMemClasherImpl implements InMemClasher {
@state
private TransactionalMap<String, Object> state = new InMemoryTransactionalMap<String, Object>();
public String getState(String key) {
return (String) state.get(key).get();
}
public void setState(String key, String msg) {
state.put(key, msg);
}
public void clash() {
state.put("clasher", "was here");
// spend some time here
// for (long i = 0; i < 1000000000; i++) {
// for (long j = 0; j < 10000000; j++) {
// j += i;
// }
// }
// FIXME: this statement gives me this error:
// se.scalablesolutions.akka.kernel.ActiveObjectException:
// Unexpected message [!(scala.actors.Channel@c2b2f6,ResultOrFailure[Right(null)])]
// to
// [GenericServer[se.scalablesolutions.akka.api.StatefulImpl]] from
// [GenericServer[se.scalablesolutions.akka.api.ClasherImpl]]]
// try { Thread.sleep(1000); } catch (InterruptedException e) {}
}
}
*/

View file

@ -0,0 +1,14 @@
package se.scalablesolutions.akka.api;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
@Path("/foo")
public class JerseyFoo {
@GET
@Produces({"application/json"})
public String foo() {
return "hello foo";
}
}

View file

@ -0,0 +1,29 @@
package se.scalablesolutions.akka.api;
import se.scalablesolutions.akka.kernel.TransactionalMap;
import se.scalablesolutions.akka.kernel.CassandraPersistentTransactionalMap;
public class PersistentClasher {
private TransactionalMap state = new CassandraPersistentTransactionalMap(this);
public String getState(String key) {
return (String)state.get(key).get();
}
public void setState(String key, String msg) {
state.put(key, msg);
}
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,ResultOrFailure[Right(null)])]
// to
// [GenericServer[se.scalablesolutions.akka.api.StatefulImpl]] from
// [GenericServer[se.scalablesolutions.akka.api.ClasherImpl]]]
// try { Thread.sleep(1000); } catch (InterruptedException e) {}
}
}

View file

@ -0,0 +1,7 @@
package se.scalablesolutions.akka.api;
public class PersistentFailer {
public void fail() {
throw new RuntimeException("expected");
}
}

View file

@ -0,0 +1,58 @@
/**
* Copyright (C) 2009 Scalable Solutions.
*/
package se.scalablesolutions.akka.api;
import se.scalablesolutions.akka.annotation.*;
import se.scalablesolutions.akka.kernel.config.*;
import static se.scalablesolutions.akka.kernel.config.JavaConfig.*;
import se.scalablesolutions.akka.kernel.Kernel;
import se.scalablesolutions.akka.kernel.TransactionalMap;
import se.scalablesolutions.akka.kernel.CassandraPersistentTransactionalMap;
import junit.framework.TestCase;
public class PersistentStateTest extends TestCase {
static String messageLog = "";
static {
System.setProperty("storage-config", "config");
Kernel.startCassandra();
}
final private ActiveObjectGuiceConfiguratorForJava conf = new ActiveObjectGuiceConfiguratorForJava();
protected void setUp() {
conf.configureActiveObjects(
new RestartStrategy(new AllForOne(), 3, 5000),
new Component[] {
new Component(PersistentStateful.class, new LifeCycle(new Permanent(), 1000), 10000000),
new Component(PersistentFailer.class, new LifeCycle(new Permanent(), 1000), 1000),
new Component(PersistentClasher.class, new LifeCycle(new Permanent(), 1000), 100000)
}).supervise();
}
protected void tearDown() {
conf.stop();
}
public void testShouldNotRollbackStateForStatefulServerInCaseOfSuccess() {
PersistentStateful stateful = conf.getActiveObject(PersistentStateful.class);
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(PersistentStateful.class);
stateful.setState("testShouldRollbackStateForStatefulServerInCaseOfFailure", "init"); // set init state
PersistentFailer failer = conf.getActiveObject(PersistentFailer.class);
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
}
}

View file

@ -0,0 +1,42 @@
package se.scalablesolutions.akka.api;
import se.scalablesolutions.akka.kernel.TransactionalMap;
import se.scalablesolutions.akka.kernel.CassandraPersistentTransactionalMap;
import se.scalablesolutions.akka.annotation.transactional;
public class PersistentStateful {
private TransactionalMap state = new CassandraPersistentTransactionalMap(this);
public String getState(String key) {
return (String)state.get(key).get();
}
public void setState(String key, String msg) {
state.put(key, msg);
}
@transactional
public void success(String key, String msg) {
state.put(key, msg);
}
@transactional
public void failure(String key, String msg, PersistentFailer failer) {
state.put(key, msg);
failer.fail();
}
@transactional
public void clashOk(String key, String msg, PersistentClasher clasher) {
state.put(key, msg);
clasher.clash();
}
@transactional
public void clashNotOk(String key, String msg, PersistentClasher clasher) {
state.put(key, msg);
clasher.clash();
clasher.clash();
}
}

View file

@ -0,0 +1,86 @@
/**
* Copyright (C) 2009 Scalable Solutions.
*/
package se.scalablesolutions.akka.api;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.grizzly.http.SelectorThread;
import com.sun.grizzly.http.servlet.ServletAdapter;
import com.sun.grizzly.tcp.Adapter;
import com.sun.grizzly.standalone.StaticStreamAlgorithm;
import javax.ws.rs.core.UriBuilder;
import javax.servlet.Servlet;
import org.junit.*;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.URI;
import junit.framework.TestSuite;
import se.scalablesolutions.akka.kernel.config.ActiveObjectGuiceConfiguratorForJava;
import se.scalablesolutions.akka.kernel.config.JavaConfig;
public class RestTest extends TestSuite {
private static int PORT = 9998;
private static URI URI = UriBuilder.fromUri("http://localhost/").port(PORT).build();
private static SelectorThread selector = null;
private static ActiveObjectGuiceConfiguratorForJava conf = new ActiveObjectGuiceConfiguratorForJava();
@BeforeClass
public static void initialize() throws IOException {
conf.configureActiveObjects(
new JavaConfig.RestartStrategy(new JavaConfig.AllForOne(), 3, 5000),
new JavaConfig.Component[] {
new JavaConfig.Component(
JerseyFoo.class,
new JavaConfig.LifeCycle(new JavaConfig.Permanent(), 1000), 10000000)
}).inject().supervise();
selector = startJersey();
}
@AfterClass
public static void cleanup() throws IOException {
conf.stop();
selector.stopEndpoint();
System.exit(0);
}
@Test
public void simpleRequest() throws IOException, InstantiationException {
selector.start();
Client client = Client.create();
WebResource webResource = client.resource(URI);
String responseMsg = webResource.path("/foo").get(String.class);
assertEquals("hello foo", responseMsg);
selector.stopEndpoint();
}
private static SelectorThread startJersey() {
try {
ServletAdapter adapter = new ServletAdapter();
Servlet servlet = se.scalablesolutions.akka.kernel.jersey.AkkaServlet.class.newInstance();
adapter.setServletInstance(servlet);
adapter.setContextPath(URI.getPath());
return createGrizzlySelector(adapter, URI, PORT);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static SelectorThread createGrizzlySelector(Adapter adapter, URI uri, int port) throws IOException, InstantiationException {
final String scheme = uri.getScheme();
if (!scheme.equalsIgnoreCase("http"))
throw new IllegalArgumentException("The URI scheme, of the URI " + uri + ", must be equal (ignoring case) to 'http'");
final SelectorThread selectorThread = new SelectorThread();
selectorThread.setAlgorithmClassName(StaticStreamAlgorithm.class.getName());
selectorThread.setPort(port);
selectorThread.setAdapter(adapter);
selectorThread.listen();
return selectorThread;
}
}