Add Java API class UntypedActorWithStash. Add tests for Stash Java API.
This commit is contained in:
parent
9244e8399d
commit
00e5bf3bdf
4 changed files with 95 additions and 0 deletions
36
akka-actor-tests/src/test/java/akka/actor/StashJavaAPI.java
Normal file
36
akka-actor-tests/src/test/java/akka/actor/StashJavaAPI.java
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package akka.actor;
|
||||
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.japi.Creator;
|
||||
import akka.testkit.AkkaSpec;
|
||||
import com.typesafe.config.ConfigFactory;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class StashJavaAPI {
|
||||
|
||||
private static ActorSystem system;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeAll() {
|
||||
system = ActorSystem.create("StashJavaAPI", ConfigFactory.parseString(ActorWithStashSpec.testConf()));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterAll() {
|
||||
system.shutdown();
|
||||
system = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mustBeAbleToUseStash() {
|
||||
ActorRef ref = system.actorOf(new Props(StashJavaAPITestActor.class).withDispatcher("my-dispatcher"));
|
||||
ref.tell("Hello", ref);
|
||||
ref.tell("Hello", ref);
|
||||
ref.tell(new Object());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package akka.actor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class StashJavaAPITestActor extends UntypedActorWithStash {
|
||||
int count = 0;
|
||||
public void onReceive(Object msg) {
|
||||
if (msg instanceof String) {
|
||||
if (count < 0) {
|
||||
getSender().tell(new Integer(((String) msg).length()));
|
||||
} else if (count == 2) {
|
||||
count = -1;
|
||||
unstashAll();
|
||||
} else {
|
||||
count += 1;
|
||||
stash();
|
||||
}
|
||||
} else if (msg instanceof Integer) {
|
||||
int value = ((Integer) msg).intValue();
|
||||
assertEquals(value, 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ import akka.pattern.ask
|
|||
import akka.util.duration._
|
||||
import com.typesafe.config.{ Config, ConfigFactory }
|
||||
import org.scalatest.BeforeAndAfterEach
|
||||
import org.scalatest.junit.JUnitSuite
|
||||
|
||||
object ActorWithStashSpec {
|
||||
|
||||
|
|
@ -76,6 +77,8 @@ object ActorWithStashSpec {
|
|||
|
||||
}
|
||||
|
||||
class JavaActorWithStashSpec extends StashJavaAPI with JUnitSuite
|
||||
|
||||
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
|
||||
class ActorWithStashSpec extends AkkaSpec(ActorWithStashSpec.testConf) with DefaultTimeout with BeforeAndAfterEach {
|
||||
import ActorWithStashSpec._
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.actor
|
||||
|
||||
/**
|
||||
* Actor base class that should be extended to create an actor with a stash.
|
||||
*
|
||||
* The stash enables an actor to temporarily stash away messages that can not or
|
||||
* should not be handled using the actor's current behavior.
|
||||
* <p/>
|
||||
* Example:
|
||||
* <pre>
|
||||
* public class MyActorWithStash extends UntypedActorWithStash {
|
||||
* int count = 0;
|
||||
* public void onReceive(Object msg) {
|
||||
* if (msg instanceof String) {
|
||||
* if (count < 0) {
|
||||
* getSender().tell(new Integer(((String) msg).length()));
|
||||
* } else if (count == 2) {
|
||||
* count = -1;
|
||||
* unstashAll();
|
||||
* } else {
|
||||
* count += 1;
|
||||
* stash();
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
abstract class UntypedActorWithStash extends UntypedActor with Stash
|
||||
Loading…
Add table
Add a link
Reference in a new issue