From 02456f8bd21ea64edc39b372b76acbcbc3ab927a Mon Sep 17 00:00:00 2001 From: Michajlo Matijkiw Date: Mon, 2 Apr 2012 00:19:34 -0400 Subject: [PATCH] testing for gen_call stuff --- .../otpakka/behaviours/GenServer.scala | 3 + .../behaviours/workers/GenServerWorker.scala | 2 +- .../workers/GenServerWorkerTest.scala | 87 +++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/test/scala/org/michajlo/otpakka/behaviours/workers/GenServerWorkerTest.scala diff --git a/src/main/scala/org/michajlo/otpakka/behaviours/GenServer.scala b/src/main/scala/org/michajlo/otpakka/behaviours/GenServer.scala index ec0fdf4..15b38e0 100644 --- a/src/main/scala/org/michajlo/otpakka/behaviours/GenServer.scala +++ b/src/main/scala/org/michajlo/otpakka/behaviours/GenServer.scala @@ -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)) } \ No newline at end of file diff --git a/src/main/scala/org/michajlo/otpakka/behaviours/workers/GenServerWorker.scala b/src/main/scala/org/michajlo/otpakka/behaviours/workers/GenServerWorker.scala index 68fa17b..05b77c2 100644 --- a/src/main/scala/org/michajlo/otpakka/behaviours/workers/GenServerWorker.scala +++ b/src/main/scala/org/michajlo/otpakka/behaviours/workers/GenServerWorker.scala @@ -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 diff --git a/src/test/scala/org/michajlo/otpakka/behaviours/workers/GenServerWorkerTest.scala b/src/test/scala/org/michajlo/otpakka/behaviours/workers/GenServerWorkerTest.scala new file mode 100644 index 0000000..73500be --- /dev/null +++ b/src/test/scala/org/michajlo/otpakka/behaviours/workers/GenServerWorkerTest.scala @@ -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) + () + } +} \ No newline at end of file