From 12dfba899fc9eb499391eacdca24f3a42af53dcb Mon Sep 17 00:00:00 2001 From: Debasish Ghosh Date: Sat, 5 Mar 2011 22:56:16 +0530 Subject: [PATCH] Fixed #675 : preStart() is called twice when creating new instance of TypedActor --- .../main/scala/akka/actor/TypedActor.scala | 5 +- .../actor/typed-actor/Issue675Spec.scala | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 akka-typed-actor/src/test/scala/actor/typed-actor/Issue675Spec.scala diff --git a/akka-typed-actor/src/main/scala/akka/actor/TypedActor.scala b/akka-typed-actor/src/main/scala/akka/actor/TypedActor.scala index 790e9824a1..ea072db84f 100644 --- a/akka-typed-actor/src/main/scala/akka/actor/TypedActor.scala +++ b/akka-typed-actor/src/main/scala/akka/actor/TypedActor.scala @@ -445,9 +445,8 @@ object TypedActor { * @param intfClass interface the typed actor implements * @param targetClass implementation class of the typed actor */ - def newInstance[T](intfClass: Class[T], targetClass: Class[_]): T = { + def newInstance[T](intfClass: Class[T], targetClass: Class[_]): T = newInstance(intfClass, targetClass, TypedActorConfiguration()) - } /** * Factory method for typed actor. @@ -759,7 +758,6 @@ object TypedActor { val typedActor = if (instance.isInstanceOf[TypedActor]) instance.asInstanceOf[TypedActor] else throw new IllegalArgumentException("Actor [" + targetClass.getName + "] is not a sub class of 'TypedActor'") - typedActor.preStart typedActor } @@ -768,7 +766,6 @@ object TypedActor { val typedActor = if (instance.isInstanceOf[TypedActor]) instance.asInstanceOf[TypedActor] else throw new IllegalArgumentException("Actor [" + instance.getClass.getName + "] is not a sub class of 'TypedActor'") - typedActor.preStart typedActor } diff --git a/akka-typed-actor/src/test/scala/actor/typed-actor/Issue675Spec.scala b/akka-typed-actor/src/test/scala/actor/typed-actor/Issue675Spec.scala new file mode 100644 index 0000000000..e978b61c45 --- /dev/null +++ b/akka-typed-actor/src/test/scala/actor/typed-actor/Issue675Spec.scala @@ -0,0 +1,51 @@ +/** + * Copyright (C) 2009-2011 Scalable Solutions AB + */ + +package akka.actor + +import org.scalatest.Spec +import org.scalatest.Assertions +import org.scalatest.matchers.ShouldMatchers +import org.scalatest.BeforeAndAfterEach +import org.scalatest.junit.JUnitRunner +import org.junit.runner.RunWith + +import Issue675Spec._ + + +object Issue675Spec { + var l = collection.mutable.ListBuffer.empty[String] + + trait RegistrationService { + def register(user: String, cred: String): Unit + } + + class RegistrationServiceImpl extends TypedActor with RegistrationService { + def register(user: String, cred: String): Unit = {} + + override def preStart() { + l += "RegistrationServiceImpl.preStart() called" + } + } +} + + +@RunWith(classOf[JUnitRunner]) +class Issue675Spec extends + Spec with + ShouldMatchers with + BeforeAndAfterEach { + + override def afterEach() { + Actor.registry.shutdownAll + } + + describe("TypedActor preStart method") { + it("should be invoked once") { + import Issue675Spec._ + val simplePojo = TypedActor.newInstance(classOf[RegistrationService], classOf[RegistrationServiceImpl]) + l.size should equal(1) + } + } +}