Last active
January 3, 2016 20:29
-
-
Save ktoso/8515182 to your computer and use it in GitHub Desktop.
raft testing with akka
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
system.actorOf(Props(new RaftActor with EventStreamAllMessages)) |
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
/** | |
* Use for testing. | |
* | |
* Forwards all messages received to the system's EventStream. | |
* Use this to deterministically `awaitForLeaderElection` etc. | |
*/ | |
trait EventStreamAllMessages { | |
this: Actor => | |
override def aroundReceive(receive: Actor.Receive, msg: Any) = { | |
context.system.eventStream.publish(msg.asInstanceOf[AnyRef]) | |
receive.applyOrElse(msg, unhandled) | |
} | |
} |
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
implicit val probe = TestProbe() // somewhere | |
def subscribeElectedLeader()(implicit probe: TestProbe): Unit = | |
system.eventStream.subscribe(probe.ref, classOf[ElectedAsLeader]) | |
def awaitElectedLeader()(implicit probe: TestProbe): Unit = | |
probe.expectMsgClass(max = 3.seconds, classOf[ElectedAsLeader]) |
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
it should "elect initial Leader" in { | |
// given | |
info("Before election: ") | |
infoMemberStates() | |
// when | |
awaitElectedLeader() | |
info("After election: ") | |
infoMemberStates() | |
// then | |
members.count(_.stateName == Leader) should equal (1) | |
members.count(_.stateName == Candidate) should equal (0) | |
members.count(_.stateName == Follower) should equal (4) | |
} | |
it should "elect replacement Leader if current Leader dies" in { | |
// given | |
infoMemberStates() | |
// when | |
val leaderToStop = leader.get | |
leaderToStop.stop() | |
info(s"Stopped leader: ${simpleName(leaderToStop)}") | |
// then | |
awaitElectedLeader() | |
info("New leader elected: ") | |
infoMemberStates() | |
members.count(_.stateName == Leader) should equal (1) | |
members.count(_.stateName == Candidate) should equal (0) | |
members.count(_.stateName == Follower) should equal (3) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment