This repository has been archived by the owner on Sep 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 9ece884
Showing
14 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
test/webapp/grails-akka-test/src/groovy/grails_akka/actor/BaseActor.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
test/webapp/grails-akka-test/src/groovy/grails_akka/actor/GreetingActor.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
test/webapp/grails-akka-test/src/groovy/grails_akka/command/ActorCommand.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
test/webapp/grails-akka-test/src/groovy/grails_akka/command/Command.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package grails_akka.command | ||
|
||
/** | ||
* The Command pattern. | ||
*/ | ||
public interface Command | ||
{ | ||
public void execute(); | ||
} |
27 changes: 27 additions & 0 deletions
27
test/webapp/grails-akka-test/src/groovy/grails_akka/command/GenericCommand.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
test/webapp/grails-akka-test/src/groovy/grails_akka/command/GenericCommandArguments.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
17 changes: 17 additions & 0 deletions
17
test/webapp/grails-akka-test/src/groovy/grails_akka/message/BaseMessage.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
{ | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
test/webapp/grails-akka-test/src/groovy/grails_akka/message/GenericMessage.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
test/webapp/grails-akka-test/src/groovy/grails_akka/message/Greeting.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
test/webapp/grails-akka-test/src/groovy/grails_akka/message/ImmutableMessage.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
test/webapp/grails-akka-test/src/groovy/grails_akka/message/Shutdown.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
test/webapp/grails-akka-test/src/groovy/grails_akka/message/Stop.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
test/webapp/grails-akka-test/src/groovy/grails_akka/message/Wait.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |