Skip to content

Instantly share code, notes, and snippets.

View chbatey's full-sized avatar
🐯
WFH

Christopher Batey chbatey

🐯
WFH
View GitHub Profile
@chbatey
chbatey / FixedNumberRetryPolicy.java
Created October 3, 2013 15:15
example retry impl
@Override
public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) {
return getRetryDecision(cl, nbRetry);
}
private RetryDecision getRetryDecision(ConsistencyLevel cl, int nbRetry) {
if (nbRetry >= numberOfTimesToRetry) {
return RetryDecision.rethrow();
}
return RetryDecision.retry(cl);
@chbatey
chbatey / RetryPolicy.java
Created October 3, 2013 15:33
Use of fixed retry policy
Cluster cluster = Cluster.builder()
.addContactPoint("127.0.0.1")
.withRetryPolicy(new LoggingRetryPolicy(new FixedNumberRetryPolicy(3)))
.build();
@chbatey
chbatey / Server.scala
Created January 14, 2014 23:27
Basic actor that should send a message back to sender
package batey.akka.testing.backtosender
import akka.actor.Actor
class Server extends Actor {
def receive: Actor.Receive = {
case msg @ _ => {
println("I should really send something back!")
}
}
@chbatey
chbatey / ServerTest.scala
Last active January 3, 2016 07:09
Test specifying that a message should be sent back to sender.
package batey.akka.testing.backtosender
import akka.testkit.{ImplicitSender, TestKit}
import akka.actor.{Props, ActorSystem}
import org.scalatest.FunSuiteLike
class ServerTest extends TestKit(ActorSystem("TestSystem")) with FunSuiteLike with ImplicitSender {
test("Should send back Ready message when Startup message is received") {
val actorRef = system.actorOf(Props[Server])
@chbatey
chbatey / Server.scala
Created January 14, 2014 23:34
Server having implemented sending a ready message on startup
package batey.akka.testing.backtosender
import akka.actor.Actor
import batey.akka.testing.backtosender.Messages.{Ready, Startup}
class Server extends Actor {
def receive: Actor.Receive = {
case Startup => {
sender ! Ready
}
@chbatey
chbatey / ParentActor.scala
Created February 5, 2014 22:25
Akka: Example parent sending message to child
class ParentActor extends Actor {
def receive: Actor.Receive = {
case msg @ _ => {
println(s"Received msg, delegating work to a child actor")
val childActor = context.actorOf(Props[ChildActor])
childActor ! "Go and do something important!"
}
}
}
@chbatey
chbatey / ParentActorTest.scala
Created February 5, 2014 22:29
Akka: How do I test parent sending msg to child?
class ParentActorTest extends TestKit(ActorSystem("TestSystem")) with FunSuiteLike {
test("Should delegate the important work to the client") {
val underTest = TestActorRef(new ParentActor)
underTest ! "Go do some work"
// how do i test this?
}
}
@chbatey
chbatey / ParentActor.scala
Created February 5, 2014 22:34
Akka: Using a ActorRefFactory to make it easier to test
class ParentActor(childFactory: (ActorRefFactory) => ActorRef) extends Actor {
def receive: Actor.Receive = {
case msg @ _ => {
println(s"Received msg, delegating work to a child actor")
val childActor = childFactory(context)
childActor ! "Go and do something important!"
}
}
}
@chbatey
chbatey / ParentActorTest.scala
Created February 5, 2014 22:47
Testing a message sent to a child
class ParentActorTest extends TestKit(ActorSystem("TestSystem")) with FunSuiteLike {
test("Should delegate the important work to the client") {
val testProbeForChild = TestProbe()
val underTest = TestActorRef(new ParentActor(_ => testProbeForChild.ref))
underTest ! "Go do some work"
testProbeForChild.expectMsg("Go and do something important!")
}
}
@chbatey
chbatey / ParentActorTest.scala
Created February 5, 2014 22:51
Akka: Using actor factory ref in real code
test("Using a real child actor") {
val underTest = TestActorRef(new ParentActor(actorFactory => actorFactory.actorOf(Props[ChildActor])))
underTest ! "Go do some work"
// Can't test this but shows how to crate a ParentActor in production code
}