Permalink
Browse files

copy classes used by tests (actor, command, message) even under the t…

…est webapp, to test (later) real calls to them from there
  • Loading branch information...
1 parent da0d058 commit 9ece8849763b509ea15a7c80aacfd2a7b73fb4aa @smartiniOnGitHub committed Sep 12, 2012
View
1 docs/TODO.md
@@ -77,6 +77,7 @@ cls && grails test-app integration: -echoOut
+ add some Groovy-specific extensions (annotations, etc), to show how to simplify code ... ok
- move test classes in subpackages by type (actor, command, message) ... ok
+- copy classes used by tests (actor, command, message) even under the test webapp, to test (later) real calls to them from there ... ok
---------------
View
21 test/webapp/grails-akka-test/src/groovy/grails_akka/actor/BaseActor.groovy
@@ -0,0 +1,21 @@
+package grails_akka.actor
+
+import akka.actor.UntypedActor
+import akka.event.*
+
+
+/**
+ * Abstract base actor class, to use (when desired) as base class for all other messages here.
+ */
+public abstract class BaseActor extends UntypedActor
+{
+ LoggingAdapter log = Logging.getLogger(getContext().system(), this);
+
+ @Override
+ public void onReceive(Object message) throws Exception
+ {
+ // do nothing here ... but override in subclasses
+ // String messageClassName = message?.getClass()?.getName();
+ }
+
+}
View
72 test/webapp/grails-akka-test/src/groovy/grails_akka/actor/GreetingActor.groovy
@@ -0,0 +1,72 @@
+package grails_akka.actor
+
+import akka.actor.UntypedActor
+import akka.event.*
+
+import grails_akka.actor.*
+import grails_akka.command.*
+import grails_akka.message.*
+
+
+/**
+ * Greeting actor, as a sample.
+ * <br/>
+ * This code is derived from Akka Samples.
+ */
+public class GreetingActor extends UntypedActor
+{
+ LoggingAdapter log = Logging.getLogger(getContext().system(), this)
+
+ @Override
+ public void onReceive(Object message) throws Exception
+ {
+ String messageClassName = message?.getClass()?.getName();
+
+ if (message == null)
+ {
+ log.warning("$messageClassName: null message")
+ unhandled(message)
+ }
+ else if (message instanceof Greeting)
+ {
+ log.info("$messageClassName: Hello \"" + ((Greeting) message)?.who + "\"")
+ }
+ // else if (message instanceof BaseMessage) // but it's abstract ...
+ // {
+ // log.info("$messageClassName: Hello BaseMessage instance")
+ // }
+ else if (message instanceof Stop)
+ {
+ log.info(messageClassName + ": " + "Stop this actor now ...")
+ // Stops this actor and all its supervised children
+ getContext().stop(getSelf())
+ }
+ else if (message instanceof Shutdown)
+ {
+ log.info(messageClassName + ": " + "Shutdown this akka system now ...")
+ // Shutdown the entire akka system
+ getContext.getSystem().shutdown()
+ }
+ else if (message instanceof Wait)
+ {
+ long sleep = ((Wait) message).msec
+ log.info(messageClassName + ": " + "Waiting for " + sleep + " milliseconds now ...")
+ // Sleep this actor for the given time
+ long startSleep = System.currentTimeMillis()
+ // sleep this actor
+ // note that this is not the right way, but should be ok in this small test ...
+ // because Thread.sleep breaks actors management as it will monopolize all threads of the used executor
+ Thread.sleep(sleep)
+ // TODO: probably it's needed something like this
+ // getContext.getSystem().getScheduler().scheduleOnce(sleep, sender, "Done")
+ long stopSleep = System.currentTimeMillis()
+ log.info("Wait: " + "End Waiting, after " + (stopSleep - startSleep) + " milliseconds.")
+ }
+ else
+ {
+ log.warning("Unknown message type $messageClassName, contents: \"" + message?.toString() + "\"")
+ unhandled(message)
+ }
+ }
+
+}
View
46 test/webapp/grails-akka-test/src/groovy/grails_akka/command/ActorCommand.groovy
@@ -0,0 +1,46 @@
+package grails_akka.command
+
+import akka.actor.*
+
+import groovy.transform.*
+
+
+/**
+ * Command that send the given message to the given actor.
+ */
+@EqualsAndHashCode
+// @Immutable
+@ToString
+public class ActorCommand implements Command
+{
+ // read only properties, with public getter generated by Groovy, but not setter
+ final ActorRef actor;
+ final Serializable message;
+
+ /**
+ * Constructor
+ *
+ * @param actor
+ * The Actor Reference, must be not null.
+ *
+ * @param message
+ * The Serializable message to send, must be not null.
+ */
+ public ActorCommand(ActorRef actor, Serializable message)
+ {
+ if (actor == null)
+ throw new IllegalArgumentException("actor must be not null.");
+ if (message == null)
+ throw new IllegalArgumentException("message must be not null.");
+
+ this.actor = actor;
+ this.message = message;
+ }
+
+ @Override
+ public void execute()
+ {
+ actor.tell(message);
+ }
+
+}
View
9 test/webapp/grails-akka-test/src/groovy/grails_akka/command/Command.groovy
@@ -0,0 +1,9 @@
+package grails_akka.command
+
+/**
+ * The Command pattern.
+ */
+public interface Command
+{
+ public void execute();
+}
View
27 test/webapp/grails-akka-test/src/groovy/grails_akka/command/GenericCommand.groovy
@@ -0,0 +1,27 @@
+package grails_akka.command
+
+/**
+ * GenericCommand, with command details in the given map.
+ * <br/>
+ * Subclass and override the execute method for similar concrete implementation of command.
+ */
+public class GenericCommand implements Command
+{
+ // read only property, with public getter generated by Groovy, but not setter
+ final Map arguments;
+
+ public GenericCommand(Map arguments)
+ {
+ if (arguments == null)
+ throw new IllegalArgumentException("arguments Map must be not null.");
+
+ this.arguments = arguments;
+ }
+
+ @Override
+ public void execute()
+ {
+ // do nothing here ... but override in subclasses
+ }
+
+}
View
9 test/webapp/grails-akka-test/src/groovy/grails_akka/command/GenericCommandArguments.groovy
@@ -0,0 +1,9 @@
+package grails_akka.command
+
+/**
+ * Enum with standard keys to use as keys (but as Strings) in the GenericCommand arguments map.
+ */
+public enum GenericCommandArguments
+{
+ NAME
+}
View
17 test/webapp/grails-akka-test/src/groovy/grails_akka/message/BaseMessage.groovy
@@ -0,0 +1,17 @@
+package grails_akka.message
+
+/**
+ * Abstract base message class, to use (when desired) as base class for all other messages here.
+ * <br/>
+ * Used a kind of message to send to actors.
+ * <br/>
+ * This code is derived from Akka Samples.
+ */
+public abstract class BaseMessage implements Serializable
+{
+
+ public BaseMessage()
+ {
+ }
+
+}
View
23 test/webapp/grails-akka-test/src/groovy/grails_akka/message/GenericMessage.groovy
@@ -0,0 +1,23 @@
+package grails_akka.message
+
+import groovy.transform.*
+
+
+/**
+ * GenericMessage.
+ * <br/>
+ * Used a kind of message to send to actors.
+ */
+@EqualsAndHashCode
+@ToString
+public class GenericMessage<T> extends BaseMessage implements Serializable
+{
+ // read only property, with public getter generated by Groovy, but not setter
+ final T message;
+
+ public GenericMessage(T message)
+ {
+ this.message = message;
+ }
+
+}
View
19 test/webapp/grails-akka-test/src/groovy/grails_akka/message/Greeting.groovy
@@ -0,0 +1,19 @@
+package grails_akka.message
+
+/**
+ * Greeting message, as a sample.
+ * <br/>
+ * Used a kind of message to send to actors.
+ * <br/>
+ * This code is derived from Akka Samples.
+ */
+public class Greeting implements Serializable
+{
+ public final String who;
+
+ public Greeting(String who)
+ {
+ this.who = who;
+ }
+
+}
View
29 test/webapp/grails-akka-test/src/groovy/grails_akka/message/ImmutableMessage.groovy
@@ -0,0 +1,29 @@
+package grails_akka.message
+
+/**
+ * ImmutableMessage, as a sample.
+ * <br/>
+ * Used a kind of message to send to actors.
+ * <br/>
+ * This code is derived from Akka Samples.
+ */
+public class ImmutableMessage
+{
+ private final int sequenceNumber;
+ private final List<String> values;
+
+ public ImmutableMessage(int sequenceNumber, List<String> values)
+ {
+ this.sequenceNumber = sequenceNumber;
+ this.values = Collections.unmodifiableList(new ArrayList<String>(values));
+ }
+
+ public int getSequenceNumber() {
+ return sequenceNumber;
+ }
+
+ public List<String> getValues() {
+ return values;
+ }
+
+}
View
17 test/webapp/grails-akka-test/src/groovy/grails_akka/message/Shutdown.groovy
@@ -0,0 +1,17 @@
+package grails_akka.message
+
+import groovy.transform.*
+
+
+/**
+ * Shutdown message, as a sample.
+ * <br/>
+ * Used a kind of message to send to actors.
+ */
+// @EqualsAndHashCode
+@Immutable
+// @ToString
+public class Shutdown extends BaseMessage implements Serializable
+{
+
+}
View
17 test/webapp/grails-akka-test/src/groovy/grails_akka/message/Stop.groovy
@@ -0,0 +1,17 @@
+package grails_akka.message
+
+import groovy.transform.*
+
+
+/**
+ * Stop message, as a sample.
+ * <br/>
+ * Used a kind of message to send to actors.
+ */
+// @EqualsAndHashCode
+@Immutable
+// @ToString
+public class Stop extends BaseMessage implements Serializable
+{
+
+}
View
27 test/webapp/grails-akka-test/src/groovy/grails_akka/message/Wait.groovy
@@ -0,0 +1,27 @@
+package grails_akka.message
+
+import groovy.transform.*
+
+
+/**
+ * Waiting message, as a sample.
+ * <br/>
+ * Used a kind of message to send to actors.
+ */
+@EqualsAndHashCode
+// @Immutable
+@ToString
+public class Wait implements Serializable
+{
+ final long msec;
+
+ public Wait(long msec)
+ {
+ // this.msec = (msec < 0) ? 0l : msec;
+ if (msec < 0)
+ throw new IllegalArgumentException("Waiting time must be 0 or positive.");
+
+ this.msec = msec;
+ }
+
+}

0 comments on commit 9ece884

Please sign in to comment.