Permalink
Browse files

testing for gen_call stuff

  • Loading branch information...
1 parent f8b342e commit 02456f8bd21ea64edc39b372b76acbcbc3ab927a @michajlo committed Apr 2, 2012
@@ -45,4 +45,7 @@ trait GenServer {
def handle_cast: PartialFunction[(Any, Any), (Symbol, Any)]
def handle_info: PartialFunction[(Any, Any), (Symbol, Any)]
+
+ def do_handle_call(msg: Any, from: ActorRef, state: Any): (Symbol, Any, Any) =
+ handle_call((msg, from, state))
}
@@ -14,7 +14,7 @@ class GenServerWorker(val genServer: GenServer, args: List[Any]) extends Actor {
def receive = {
case ('gen_call, msg) =>
- genServer.handle_call((msg, sender, state)) match {
+ genServer.do_handle_call(msg, sender, state) match {
case ('reply, reply, newState) =>
state = newState
sender ! reply
@@ -0,0 +1,87 @@
+package org.michajlo.otpakka.behaviours.workers
+import org.junit.Assert._
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.michajlo.otpakka.behaviours.GenServer
+import org.mockito.Mockito._
+import org.mockito.Matchers
+import org.mockito.Mockito
+
+import akka.actor.ActorRef
+import akka.actor.ActorSystem
+import akka.pattern.ask
+import akka.testkit.ImplicitSender
+import akka.testkit.TestActorRef
+import akka.util.duration._
+import akka.dispatch.Await
+
+class GenServerWorkerTest {
+
+ implicit var actorSystem: ActorSystem = _
+
+ var state: List[Any] = _
+
+ var mockGenServer: GenServer = _
+
+ var testActor: TestActorRef[GenServerWorker] = _
+
+ var underTest: GenServerWorker = _
+
+ @Before
+ def setUp() = {
+ actorSystem = ActorSystem("test")
+
+ state = Nil
+ mockGenServer = mock(classOf[GenServer])
+ doReturn(('ok, state)).when(mockGenServer).init(Matchers.any(classOf[List[Any]]))
+
+ testActor = TestActorRef(new GenServerWorker(mockGenServer, Nil))
+ underTest = testActor.underlyingActor
+ }
+
+ @After
+ def tearDown() = {
+ actorSystem.shutdown()
+ }
+
+ // TODO: figure out how to test startup, akka has some weird rules about
+ // starting actors...
+
+ @Test
+ def testInitialStateIsAsReturnedByInit() = {
+ // note, this was done in @Before
+ assertEquals(Nil, underTest.state)
+ }
+
+ @Test
+ def testGenCallMessagePassedOnToGenServerProperly() = {
+ val msg = "Hello there"
+
+ doReturn(('reply, 'ok, Nil)).when(mockGenServer).do_handle_call(Matchers.any(), Matchers.any(), Matchers.any())
+ testActor ! ('gen_call, msg)
+ verify(mockGenServer).do_handle_call(Matchers.eq(msg), Matchers.any(classOf[ActorRef]), Matchers.eq(Nil))
+ ()
+ }
+
+ @Test
+ def testHandleCallWithReplyAndNewStateProperlyUpdatesState() = {
+ val newState = "State"
+
+ doReturn(('reply, 'ok, newState)).when(mockGenServer).do_handle_call(Matchers.any(), Matchers.any(), Matchers.any())
+ testActor ! ('gen_call, "blah")
+ assertEquals(newState, underTest.state)
+ ()
+ }
+
+ @Test
+ def testHandleCallWithReplyRepliesToSender() = {
+ val reply = "reply"
+
+ doReturn(('reply, reply, Nil)).when(mockGenServer).do_handle_call(Matchers.any(), Matchers.any(), Matchers.any())
+ val callFuture = (testActor ? ('gen_call, "blah"))(1 second)
+ val result = Await.result(callFuture, 1 second)
+ assertEquals(reply, result)
+ ()
+ }
+}

0 comments on commit 02456f8

Please sign in to comment.