diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/TestException.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/TestException.scala new file mode 100644 index 0000000000..32b632e436 --- /dev/null +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/TestException.scala @@ -0,0 +1,13 @@ +/** + * Copyright (C) 2018 Lightbend Inc. + */ + +package akka.actor.testkit.typed + +import scala.util.control.NoStackTrace + +/** + * A predefined exception that can be used in tests. It doesn't include a stack trace. + */ +class TestException(msg: String) extends RuntimeException(msg) with NoStackTrace + diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestKitUtils.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestKitUtils.scala index f6c809cb25..2aef48352e 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestKitUtils.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestKitUtils.scala @@ -4,10 +4,11 @@ package akka.actor.testkit.typed.internal +import java.lang.reflect.Modifier + import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.{ ActorRef, ActorSystem, Behavior, Props } import akka.annotation.InternalApi - import scala.concurrent.{ Await, TimeoutException } import scala.concurrent.duration.Duration @@ -47,6 +48,15 @@ private[akka] object TestKitUtils { private val TestKitRegex = """akka\.testkit\.typed\.(?:javadsl|scaladsl)\.ActorTestKit(?:\$.*)?""".r def testNameFromCallStack(classToStartFrom: Class[_]): String = { + + def isAbstractClass(className: String): Boolean = { + try { + Modifier.isAbstract(Class.forName(className).getModifiers) + } catch { + case _: Throwable ⇒ false // yes catch everything, best effort check + } + } + val startFrom = classToStartFrom.getName val filteredStack = Thread.currentThread.getStackTrace.toIterator .map(_.getClassName) @@ -57,11 +67,24 @@ private[akka] object TestKitUtils { case `startFrom` ⇒ true case str if str.startsWith(startFrom + "$") ⇒ true // lambdas inside startFrom etc case TestKitRegex() ⇒ true // testkit internals + case str if isAbstractClass(str) ⇒ true case _ ⇒ false } + if (filteredStack.isEmpty) + throw new IllegalArgumentException(s"Couldn't find [${classToStartFrom.getName}] in call stack") + // sanitize for actor system name - filteredStack.next() + scrubActorSystemName(filteredStack.next()) + } + + /** + * Sanitize the `name` to be used as valid actor system name by + * replacing invalid characters. `name` may for example be a fully qualified + * class name and then the short class name will be used. + */ + def scrubActorSystemName(name: String): String = { + name .replaceFirst("""^.*\.""", "") // drop package name .replaceAll("""\$\$?\w+""", "") // drop scala anonymous functions/classes .replaceAll("[^a-zA-Z_0-9]", "_") diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/ActorTestKit.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/ActorTestKit.scala index 229d2ae45c..f20ad76927 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/ActorTestKit.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/ActorTestKit.scala @@ -10,7 +10,7 @@ import akka.actor.Scheduler import akka.actor.typed.{ ActorRef, ActorSystem, Behavior, Props } import akka.actor.testkit.typed.TestKitSettings import akka.actor.testkit.typed.internal.TestKitUtils -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit ⇒ ScalaTestKit } +import akka.actor.testkit.typed.scaladsl import akka.util.Timeout import com.typesafe.config.Config import akka.util.JavaDurationConverters._ @@ -18,34 +18,49 @@ import akka.util.JavaDurationConverters._ object ActorTestKit { /** - * Create a testkit named from the class that is calling this method + * Create a testkit named from the class that is calling this method. + * + * It will create an [[akka.actor.typed.ActorSystem]] with this name, + * e.g. threads will include the name. + * When the test has completed you should terminate the `ActorSystem` and + * the testkit with [[ActorTestKit#shutdownTestKit]]. */ - def create(): ActorTestKit = new ActorTestKit(new ScalaTestKit {}) + def create(): ActorTestKit = + new ActorTestKit(scaladsl.ActorTestKit(TestKitUtils.testNameFromCallStack(classOf[ActorTestKit]))) /** - * Create a testkit named with this test class + * Create a named testkit. + * + * It will create an [[akka.actor.typed.ActorSystem]] with this name, + * e.g. threads will include the name. + * When the test has completed you should terminate the `ActorSystem` and + * the testkit with [[ActorTestKit#shutdownTestKit]]. */ - def create(testClass: Class[_]): ActorTestKit = new ActorTestKit(new ScalaTestKit { - override def name = TestKitUtils.testNameFromCallStack(testClass) - }) + def create(name: String): ActorTestKit = + new ActorTestKit(scaladsl.ActorTestKit(name)) /** - * Create a testkit named with this test class, and use a custom config for the actor system + * Create a named testkit, and use a custom config for the actor system. + * + * It will create an [[akka.actor.typed.ActorSystem]] with this name, + * e.g. threads will include the name. + * When the test has completed you should terminate the `ActorSystem` and + * the testkit with [[ActorTestKit#shutdownTestKit]]. */ - def create(testClass: Class[_], customConfig: Config) = new ActorTestKit(new ScalaTestKit { - override def name = TestKitUtils.testNameFromCallStack(testClass) - override def config = customConfig - }) + def create(name: String, customConfig: Config): ActorTestKit = + new ActorTestKit(scaladsl.ActorTestKit(name, customConfig)) /** - * Create a testkit named with this test class, and use a custom config for the actor system, + * Create a named testkit, and use a custom config for the actor system, * and a custom [[akka.actor.testkit.typed.TestKitSettings]] + * + * It will create an [[akka.actor.typed.ActorSystem]] with this name, + * e.g. threads will include the name. + * When the test has completed you should terminate the `ActorSystem` and + * the testkit with [[ActorTestKit#shutdownTestKit]]. */ - def create(testClass: Class[_], customConfig: Config, settings: TestKitSettings) = new ActorTestKit(new ScalaTestKit { - override def name = TestKitUtils.testNameFromCallStack(testClass) - override def config = customConfig - override def testkitSettings: TestKitSettings = settings - }) + def create(name: String, customConfig: Config, settings: TestKitSettings): ActorTestKit = + new ActorTestKit(scaladsl.ActorTestKit(name, customConfig, settings)) /** * Shutdown the given actor system and wait up to `duration` for shutdown to complete. @@ -91,13 +106,13 @@ object ActorTestKit { * The actor system has a custom guardian that allows for spawning arbitrary actors using the `spawn` methods. * * Designed to work with any test framework, but framework glue code that calls `shutdownTestKit` after all tests has - * run needs to be provided by the user. + * run needs to be provided by the user or with [[TestKitJunitResource]]. * * Use `TestKit.create` factories to construct manually or [[TestKitJunitResource]] to use together with JUnit tests * * For synchronous testing of a `Behavior` see [[BehaviorTestKit]] */ -final class ActorTestKit protected (delegate: akka.actor.testkit.typed.scaladsl.ActorTestKit) { +final class ActorTestKit private[akka] (delegate: akka.actor.testkit.typed.scaladsl.ActorTestKit) { /** * The default timeout as specified with the config/[[akka.actor.testkit.typed.TestKitSettings]] @@ -110,6 +125,8 @@ final class ActorTestKit protected (delegate: akka.actor.testkit.typed.scaladsl. */ def system: ActorSystem[Void] = delegate.system.asInstanceOf[ActorSystem[Void]] + def testKitSettings: TestKitSettings = delegate.testKitSettings + /** * The scheduler of the testkit actor system */ @@ -157,6 +174,8 @@ final class ActorTestKit protected (delegate: akka.actor.testkit.typed.scaladsl. */ def createTestProbe[M](name: String, clazz: Class[M]): TestProbe[M] = TestProbe.create(name, clazz, system) + // Note that if more methods are added here they should also be added to TestKitJunitResource + /** * Terminate the actor system and the testkit */ diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestKitJunitResource.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestKitJunitResource.scala index 99a6b225c7..3606a79d24 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestKitJunitResource.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestKitJunitResource.scala @@ -5,15 +5,18 @@ package akka.actor.testkit.typed.javadsl import akka.actor.Scheduler +import akka.actor.testkit.typed.TestKitSettings +import akka.actor.testkit.typed.internal.TestKitUtils import akka.actor.typed.{ ActorRef, ActorSystem, Behavior, Props } import akka.util.Timeout import com.typesafe.config.Config +import com.typesafe.config.ConfigFactory import org.junit.Rule import org.junit.rules.ExternalResource /** - * A Junit external resource for the testkit, making it possible to have Junit manage the lifecycle of the testkit. - * The testkit will be automatically shut down when the test completes fails. + * A Junit external resource for the [[ActorTestKit]], making it possible to have Junit manage the lifecycle of the testkit. + * The testkit will be automatically shut down when the test completes or fails. * * Note that Junit is not provided as a transitive dependency of the testkit module but must be added explicitly * to your project to use this. @@ -32,22 +35,42 @@ import org.junit.rules.ExternalResource * } * }}} */ -class TestKitJunitResource(_kit: ActorTestKit) extends ExternalResource { +final class TestKitJunitResource(_kit: ActorTestKit) extends ExternalResource { - def this() = this(ActorTestKit.create(classOf[TestKitJunitResource])) - def this(customConfig: Config) = this(ActorTestKit.create(classOf[TestKitJunitResource], customConfig)) + def this() = this(ActorTestKit.create(TestKitUtils.testNameFromCallStack(classOf[TestKitJunitResource]))) - def this(testClass: Class[_]) = this(ActorTestKit.create()) - def this(testClass: Class[_], customConfig: Config) = this(ActorTestKit.create(testClass, customConfig)) + /** + * Use a custom config for the actor system. + */ + def this(customConfig: String) = + this(ActorTestKit.create( + TestKitUtils.testNameFromCallStack(classOf[TestKitJunitResource]), + ConfigFactory.parseString(customConfig))) + + /** + * Use a custom config for the actor system. + */ + def this(customConfig: Config) = + this(ActorTestKit.create(TestKitUtils.testNameFromCallStack(classOf[TestKitJunitResource]), customConfig)) + + /** + * Use a custom config for the actor system, and a custom [[akka.actor.testkit.typed.TestKitSettings]]. + */ + def this(customConfig: Config, settings: TestKitSettings) = + this(ActorTestKit.create(TestKitUtils.testNameFromCallStack(classOf[TestKitJunitResource]), customConfig, settings)) @Rule - final val testKit = _kit + val testKit: ActorTestKit = _kit // delegates of the TestKit api for minimum fuss /** * See corresponding method on [[ActorTestKit]] */ def system: ActorSystem[Void] = testKit.system + /** + * See corresponding method on [[ActorTestKit]] + */ + def testKitSettings: TestKitSettings = testKit.testKitSettings /** * See corresponding method on [[ActorTestKit]] */ diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala index e7a21197fa..fff86d8f3e 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala @@ -56,7 +56,8 @@ object TestProbe { } /** - * Java API: * Create instances through the `create` factories in the [[TestProbe]] companion. + * Java API: * Create instances through the `create` factories in the [[TestProbe]] companion + * or via [[ActorTestKit#createTestProbe]]. * * A test probe is essentially a queryable mailbox which can be used in place of an actor and the received * messages can then be asserted etc. diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/ActorTestKit.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/ActorTestKit.scala index fc84afb6d7..efacac2a76 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/ActorTestKit.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/ActorTestKit.scala @@ -8,14 +8,78 @@ import akka.actor.typed.scaladsl.AskPattern._ import akka.actor.typed.{ ActorRef, ActorSystem, Behavior, Props } import akka.annotation.{ ApiMayChange, InternalApi } import akka.actor.testkit.typed.TestKitSettings + import akka.actor.testkit.typed.internal.{ ActorTestKitGuardian, TestKitUtils } import com.typesafe.config.{ Config, ConfigFactory } - -import scala.concurrent.{ Await, Future } +import scala.concurrent.Await import scala.concurrent.duration._ +import akka.actor.Scheduler +import akka.util.Timeout + object ActorTestKit { + /** + * Create a testkit named from the class that is calling this method. + * + * It will create an [[akka.actor.typed.ActorSystem]] with this name, + * e.g. threads will include the name. + * When the test has completed you should terminate the `ActorSystem` and + * the testkit with [[ActorTestKit#shutdownTestKit]]. + */ + def apply(): ActorTestKit = + new ActorTestKit( + name = TestKitUtils.testNameFromCallStack(classOf[ActorTestKit]), + config = noConfigSet, + settings = None + ) + + /** + * Create a named testkit. + * + * It will create an [[akka.actor.typed.ActorSystem]] with this name, + * e.g. threads will include the name. + * When the test has completed you should terminate the `ActorSystem` and + * the testkit with [[ActorTestKit#shutdownTestKit]]. + */ + def apply(name: String): ActorTestKit = + new ActorTestKit( + name = TestKitUtils.scrubActorSystemName(name), + config = noConfigSet, + settings = None + ) + + /** + * Create a named testkit, and use a custom config for the actor system. + * + * It will create an [[akka.actor.typed.ActorSystem]] with this name, + * e.g. threads will include the name. + * When the test has completed you should terminate the `ActorSystem` and + * the testkit with [[ActorTestKit#shutdownTestKit]]. + */ + def apply(name: String, customConfig: Config): ActorTestKit = + new ActorTestKit( + name = TestKitUtils.scrubActorSystemName(name), + config = customConfig, + settings = None + ) + + /** + * Create a named testkit, and use a custom config for the actor system, + * and a custom [[akka.actor.testkit.typed.TestKitSettings]] + * + * It will create an [[akka.actor.typed.ActorSystem]] with this name, + * e.g. threads will include the name. + * When the test has completed you should terminate the `ActorSystem` and + * the testkit with [[ActorTestKit#shutdownTestKit]]. + */ + def apply(name: String, customConfig: Config, settings: TestKitSettings): ActorTestKit = + new ActorTestKit( + name = TestKitUtils.scrubActorSystemName(name), + config = customConfig, + settings = Some(settings) + ) + /** * Shutdown the given [[akka.actor.typed.ActorSystem]] and block until it shuts down, * if more time than `TestKitSettings.DefaultActorSystemShutdownTimeout` passes an exception is thrown @@ -58,40 +122,27 @@ object ActorTestKit { * For synchronous testing of a `Behavior` see [[BehaviorTestKit]] */ @ApiMayChange -trait ActorTestKit { - /** - * Actor system name based on the test it is mixed into, override to customize, or pass to constructor - * if using [[ActorTestKit]] rather than [[ActorTestKit]] - */ - protected def name: String = TestKitUtils.testNameFromCallStack(classOf[ActorTestKit]) +final class ActorTestKit private[akka] (val name: String, val config: Config, settings: Option[TestKitSettings]) { - /** - * Configuration the actor system is created with, override to customize, or pass to constructor - * if using [[ActorTestKit]] rather than [[ActorTestKit]] - */ - def config: Config = ActorTestKit.noConfigSet - - /** - * TestKit settings used in the tests, override or provide custom config to customize - */ - protected implicit def testkitSettings = TestKitSettings(system) + implicit def testKitSettings: TestKitSettings = + settings.getOrElse(TestKitSettings(system)) private val internalSystem: ActorSystem[ActorTestKitGuardian.TestKitCommand] = if (config eq ActorTestKit.noConfigSet) ActorSystem(ActorTestKitGuardian.testKitGuardian, name) else ActorSystem(ActorTestKitGuardian.testKitGuardian, name, config) - implicit final def system: ActorSystem[Nothing] = internalSystem + implicit def system: ActorSystem[Nothing] = internalSystem - implicit def scheduler = system.scheduler + implicit def scheduler: Scheduler = system.scheduler private val childName: Iterator[String] = Iterator.from(0).map(_.toString) - implicit val timeout = testkitSettings.DefaultTimeout + implicit val timeout: Timeout = testKitSettings.DefaultTimeout - final def shutdownTestKit(): Unit = { + def shutdownTestKit(): Unit = { ActorTestKit.shutdown( system, - testkitSettings.DefaultActorSystemShutdownTimeout, - testkitSettings.ThrowOnShutdownTimeout + testKitSettings.DefaultActorSystemShutdownTimeout, + testKitSettings.ThrowOnShutdownTimeout ) } @@ -99,28 +150,48 @@ trait ActorTestKit { * Spawn the given behavior. This is created as a child of the test kit * guardian */ - final def spawn[T](behavior: Behavior[T]): ActorRef[T] = + def spawn[T](behavior: Behavior[T]): ActorRef[T] = spawn(behavior, Props.empty) /** * Spawn the given behavior. This is created as a child of the test kit * guardian */ - final def spawn[T](behavior: Behavior[T], props: Props): ActorRef[T] = + def spawn[T](behavior: Behavior[T], props: Props): ActorRef[T] = Await.result(internalSystem ? (ActorTestKitGuardian.SpawnActorAnonymous(behavior, _, props)), timeout.duration) /** * Spawn the given behavior. This is created as a child of the test kit * guardian */ - final def spawn[T](behavior: Behavior[T], name: String): ActorRef[T] = + def spawn[T](behavior: Behavior[T], name: String): ActorRef[T] = spawn(behavior, name, Props.empty) /** * Spawn the given behavior. This is created as a child of the test kit * guardian */ - final def spawn[T](behavior: Behavior[T], name: String, props: Props): ActorRef[T] = + def spawn[T](behavior: Behavior[T], name: String, props: Props): ActorRef[T] = Await.result(internalSystem ? (ActorTestKitGuardian.SpawnActor(name, behavior, _, props)), timeout.duration) + /** + * Shortcut for creating a new test probe for the testkit actor system + * @tparam M the type of messages the probe should accept + */ + def createTestProbe[M](): TestProbe[M] = TestProbe()(system) + + /** + * Shortcut for creating a new named test probe for the testkit actor system + * @tparam M the type of messages the probe should accept + */ + def createTestProbe[M](name: String): TestProbe[M] = TestProbe(name)(system) + + // FIXME needed for Akka internal tests but, users shouldn't spawn system actors? + @InternalApi + private[akka] def systemActor[T](behavior: Behavior[T], name: String): ActorRef[T] = + Await.result(system.systemActorOf(behavior, name), timeout.duration) + + @InternalApi + private[akka] def systemActor[T](behavior: Behavior[T]): ActorRef[T] = + Await.result(system.systemActorOf(behavior, childName.next()), timeout.duration) } diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/ActorTestKitBase.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/ActorTestKitBase.scala new file mode 100644 index 0000000000..d56df2b2dc --- /dev/null +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/ActorTestKitBase.scala @@ -0,0 +1,103 @@ +/** + * Copyright (C) 2018 Lightbend Inc. + */ + +package akka.actor.testkit.typed.scaladsl + +import akka.actor.Scheduler +import akka.actor.testkit.typed.TestKitSettings +import akka.actor.testkit.typed.internal.TestKitUtils +import akka.actor.typed.ActorRef +import akka.actor.typed.ActorSystem +import akka.actor.typed.Behavior +import akka.actor.typed.Props +import akka.util.Timeout +import com.typesafe.config.Config +import com.typesafe.config.ConfigFactory + +object ActorTestKitBase { + def testNameFromCallStack(): String = TestKitUtils.testNameFromCallStack(classOf[ActorTestKitBase]) +} + +/** + * A base class for the [[ActorTestKit]], making it possible to have testing framework (e.g. ScalaTest) + * manage the lifecycle of the testkit. + * + * An implementation for ScalaTest is [[ActorTestKitWordSpec]]. + * + * Another abstract class that is testing framework specific should extend this class and + * automatically shut down the `testKit` when the test completes or fails by implementing [[ActorTestKitBase#afterAll]]. + */ +abstract class ActorTestKitBase(val testKit: ActorTestKit) { + + def this() = this(ActorTestKit(ActorTestKitBase.testNameFromCallStack())) + + /** + * Use a custom config for the actor system. + */ + def this(config: String) = + this(ActorTestKit(ActorTestKitBase.testNameFromCallStack(), ConfigFactory.parseString(config))) + + /** + * Use a custom config for the actor system. + */ + def this(config: Config) = this(ActorTestKit(ActorTestKitBase.testNameFromCallStack(), config)) + + /** + * Use a custom config for the actor system, and a custom [[akka.actor.testkit.typed.TestKitSettings]]. + */ + def this(config: Config, settings: TestKitSettings) = + this(ActorTestKit(ActorTestKitBase.testNameFromCallStack(), config, settings)) + + // delegates of the TestKit api for minimum fuss + /** + * See corresponding method on [[ActorTestKit]] + */ + implicit def system: ActorSystem[Nothing] = testKit.system + /** + * See corresponding method on [[ActorTestKit]] + */ + implicit def testKitSettings: TestKitSettings = testKit.testKitSettings + /** + * See corresponding method on [[ActorTestKit]] + */ + implicit def timeout: Timeout = testKit.timeout + /** + * See corresponding method on [[ActorTestKit]] + */ + implicit def scheduler: Scheduler = testKit.scheduler + + /** + * See corresponding method on [[ActorTestKit]] + */ + def spawn[T](behavior: Behavior[T]): ActorRef[T] = testKit.spawn(behavior) + /** + * See corresponding method on [[ActorTestKit]] + */ + def spawn[T](behavior: Behavior[T], name: String): ActorRef[T] = testKit.spawn(behavior, name) + /** + * See corresponding method on [[ActorTestKit]] + */ + def spawn[T](behavior: Behavior[T], props: Props): ActorRef[T] = testKit.spawn(behavior, props) + /** + * See corresponding method on [[ActorTestKit]] + */ + def spawn[T](behavior: Behavior[T], name: String, props: Props): ActorRef[T] = testKit.spawn(behavior, name, props) + + /** + * See corresponding method on [[ActorTestKit]] + */ + def createTestProbe[M](): TestProbe[M] = testKit.createTestProbe[M]() + /** + * See corresponding method on [[ActorTestKit]] + */ + def createTestProbe[M](name: String): TestProbe[M] = testKit.createTestProbe(name) + + /** + * To be implemented by "more" concrete class that can mixin `BeforeAndAfterAll` or similar, + * for example `FlatSpecLike with BeforeAndAfterAll`. Implement by calling + * `testKit.shutdownTestKit()`. + */ + protected def afterAll(): Unit + +} diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/ActorTestKitWordSpec.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/ActorTestKitWordSpec.scala new file mode 100644 index 0000000000..8ca99f8972 --- /dev/null +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/ActorTestKitWordSpec.scala @@ -0,0 +1,56 @@ +/** + * Copyright (C) 2018 Lightbend Inc. + */ + +package akka.actor.testkit.typed.scaladsl + +import akka.actor.testkit.typed.TestKitSettings +import com.typesafe.config.Config +import com.typesafe.config.ConfigFactory +import org.scalatest.BeforeAndAfterAll +import org.scalatest.Matchers +import org.scalatest.WordSpecLike +import org.scalatest.concurrent.Eventually +import org.scalatest.concurrent.ScalaFutures +import org.scalatest.time.Span + +/** + * A ScalaTest base class for the [[ActorTestKit]], making it possible to have ScalaTest manage the lifecycle of the testkit. + * The testkit will be automatically shut down when the test completes or fails. + * + * Note that ScalaTest is not provided as a transitive dependency of the testkit module but must be added explicitly + * to your project to use this. + * + * This is a `WordSpec`, other ScalaTest styles can be implemented in a similar abstract class like this one. + * For example `with FlatSpecLike with BeforeAndAfterAll` and implementing the `afterAll` method by calling + * `testKit.shutdownTestKit()`. + */ +abstract class ActorTestKitWordSpec(testKit: ActorTestKit) extends ActorTestKitBase(testKit) + with WordSpecLike with Matchers with BeforeAndAfterAll with ScalaFutures with Eventually { + + def this() = this(ActorTestKit(ActorTestKitBase.testNameFromCallStack())) + + /** + * Use a custom config for the actor system. + */ + def this(config: String) = + this(ActorTestKit(ActorTestKitBase.testNameFromCallStack(), ConfigFactory.parseString(config))) + + /** + * Use a custom config for the actor system. + */ + def this(config: Config) = this(ActorTestKit(ActorTestKitBase.testNameFromCallStack(), config)) + + /** + * Use a custom config for the actor system, and a custom [[akka.actor.testkit.typed.TestKitSettings]]. + */ + def this(config: Config, settings: TestKitSettings) = + this(ActorTestKit(ActorTestKitBase.testNameFromCallStack(), config, settings)) + + /** + * `PatienceConfig` from [[akka.actor.testkit.typed.TestKitSettings#DefaultTimeout]] + */ + implicit val patience: PatienceConfig = + PatienceConfig(testKit.testKitSettings.DefaultTimeout.duration, Span(100, org.scalatest.time.Millis)) +} + diff --git a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/ActorTestKitTest.java b/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/ActorTestKitTest.java index c64a0a9b61..ff63fb0604 100644 --- a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/ActorTestKitTest.java +++ b/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/ActorTestKitTest.java @@ -10,6 +10,7 @@ import org.junit.ClassRule; import org.junit.Test; import org.scalatest.junit.JUnitSuite; +import java.util.HashMap; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -22,10 +23,31 @@ public class ActorTestKitTest extends JUnitSuite { public static TestKitJunitResource testKit = new TestKitJunitResource(); @Test - public void systemNameShouldComeFromTest() { + public void systemNameShouldComeFromTestClassViaJunitResource() { assertEquals("ActorTestKitTest", testKit.system().name()); } + @Test + public void systemNameShouldComeFromTestClass() { + final ActorTestKit testKit2 = ActorTestKit.create(); + try { + assertEquals("ActorTestKitTest", testKit2.system().name()); + } finally { + testKit2.shutdownTestKit(); + } + } + + @Test + public void systemNameShouldComeFromGivenClassName() { + final ActorTestKit testKit2 = ActorTestKit.create(HashMap.class.getName()); + try { + // removing package name and such + assertEquals("HashMap", testKit2.system().name()); + } finally { + testKit2.shutdownTestKit(); + } + } + @Test public void testKitShouldSpawnActor() throws Exception { final CompletableFuture started = new CompletableFuture<>(); diff --git a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java b/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java similarity index 85% rename from akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java rename to akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java index 5f4529a8d7..9efeee9d24 100644 --- a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java +++ b/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java @@ -2,7 +2,7 @@ * Copyright (C) 2017-2018 Lightbend Inc. */ -package akka.actor.testkit.typed.javadsl; +package jdocs.akka.actor.testkit.typed.javadsl; import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; @@ -12,9 +12,11 @@ import akka.actor.testkit.typed.javadsl.TestProbe; import org.junit.AfterClass; import org.junit.Test; +import static org.junit.Assert.assertEquals; + //#test-header public class AsyncTestingExampleTest { - final static ActorTestKit testKit = ActorTestKit.create(AsyncTestingExampleTest.class); + final static ActorTestKit testKit = ActorTestKit.create(); //#test-header //#under-test @@ -51,8 +53,8 @@ public class AsyncTestingExampleTest { @Test public void testVerifyingAResponse() { //#test-spawn - TestProbe probe = testKit.createTestProbe(); ActorRef pinger = testKit.spawn(echoActor, "ping"); + TestProbe probe = testKit.createTestProbe(); pinger.tell(new Ping("hello", probe.ref())); probe.expectMessage(new Pong("hello")); //#test-spawn @@ -61,10 +63,15 @@ public class AsyncTestingExampleTest { @Test public void testVerifyingAResponseAnonymous() { //#test-spawn-anonymous - TestProbe probe = testKit.createTestProbe(); ActorRef pinger = testKit.spawn(echoActor); + //#test-spawn-anonymous + TestProbe probe = testKit.createTestProbe(); pinger.tell(new Ping("hello", probe.ref())); probe.expectMessage(new Pong("hello")); - //#test-spawn-anonymous + } + + @Test + public void systemNameShouldComeFromTestClass() { + assertEquals(testKit.system().name(), "AsyncTestingExampleTest"); } } diff --git a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/JunitIntegrationExampleTest.java b/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/JunitIntegrationExampleTest.java similarity index 83% rename from akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/JunitIntegrationExampleTest.java rename to akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/JunitIntegrationExampleTest.java index 786b8aa58e..3b978b7ba3 100644 --- a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/JunitIntegrationExampleTest.java +++ b/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/JunitIntegrationExampleTest.java @@ -2,10 +2,11 @@ * Copyright (C) 2009-2018 Lightbend Inc. */ -package akka.actor.testkit.typed.javadsl; +package jdocs.akka.actor.testkit.typed.javadsl; // #junit-integration import akka.actor.testkit.typed.javadsl.TestKitJunitResource; +import akka.actor.testkit.typed.javadsl.TestProbe; import org.junit.ClassRule; import org.junit.Test; diff --git a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/ManualTimerExampleTest.java b/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/ManualTimerExampleTest.java similarity index 96% rename from akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/ManualTimerExampleTest.java rename to akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/ManualTimerExampleTest.java index 8539ce440e..315c966c64 100644 --- a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/ManualTimerExampleTest.java +++ b/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/ManualTimerExampleTest.java @@ -2,7 +2,7 @@ * Copyright (C) 2017-2018 Lightbend Inc. */ -package akka.actor.testkit.typed.javadsl; +package jdocs.akka.actor.testkit.typed.javadsl; //#manual-scheduling-simple diff --git a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java b/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java similarity index 94% rename from akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java rename to akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java index e092f63c95..e27876cd5e 100644 --- a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java +++ b/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java @@ -2,9 +2,12 @@ * Copyright (C) 2018 Lightbend Inc. */ -package akka.actor.testkit.typed.javadsl; +package jdocs.akka.actor.testkit.typed.javadsl; //#imports +import akka.actor.testkit.typed.javadsl.BehaviorTestKit; +import akka.actor.testkit.typed.javadsl.Effects; +import akka.actor.testkit.typed.javadsl.TestInbox; import akka.actor.typed.*; import akka.actor.typed.javadsl.*; //#imports diff --git a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AbstractActorSpec.scala b/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AbstractActorSpec.scala deleted file mode 100644 index d444027a14..0000000000 --- a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AbstractActorSpec.scala +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright (C) 2009-2018 Lightbend Inc. - */ - -package akka.actor.testkit.typed.scaladsl - -// NOTE: do not optimize import, unused import is here on purpose for docs -//#scalatest-glue -import akka.actor.testkit.typed.scaladsl.ActorTestKit -import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpec } - -abstract class AbstractActorSpec extends WordSpec with ActorTestKit with Matchers with BeforeAndAfterAll { - override protected def afterAll(): Unit = { - shutdownTestKit() - } -} -//#scalatest-glue diff --git a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/ActorTestKitSpec.scala b/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/ActorTestKitSpec.scala index de636115e7..a3a34ab9d5 100644 --- a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/ActorTestKitSpec.scala +++ b/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/ActorTestKitSpec.scala @@ -4,22 +4,40 @@ package akka.actor.testkit.typed.scaladsl -import akka.actor.typed.Terminated -import akka.actor.typed.scaladsl.Behaviors -import org.scalatest.concurrent.ScalaFutures -import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpec } - import scala.concurrent.Promise -// can be mixed into any spec style, -class ActorTestKitSpec extends WordSpec with Matchers with ActorTestKit with ScalaFutures { +import akka.actor.typed.Terminated +import akka.actor.typed.scaladsl.Behaviors +import org.scalatest.BeforeAndAfterAll +import org.scalatest.Matchers +import org.scalatest.WordSpec +import org.scalatest.WordSpecLike + +class ActorTestKitSpec extends ActorTestKitWordSpec { "the Scala testkit" should { - "generate a default name from the test class" in { + "generate a default name from the test class via ActorTestKitWordSpec" in { system.name should ===("ActorTestKitSpec") } + "generate a default name from the test class" in { + val testkit2 = ActorTestKit() + try { + testkit2.system.name should ===("ActorTestKitSpec") + } finally + testkit2.shutdownTestKit() + } + + "use name from given class name" in { + val testkit2 = ActorTestKit(classOf[Vector[_]].getName) + try { + // removing package name and such + testkit2.system.name should ===("Vector") + } finally + testkit2.shutdownTestKit() + } + "spawn an actor" in { val sawMessage = Promise[Boolean]() val ref = spawn(Behaviors.setup[AnyRef] { ctx ⇒ @@ -42,26 +60,51 @@ class ActorTestKitSpec extends WordSpec with Matchers with ActorTestKit with Sca "stop the actor system" in { // usually done in test framework hook method but we want to assert - shutdownTestKit() - system.whenTerminated.futureValue shouldBe a[Terminated] + val testkit2 = ActorTestKit() + testkit2.shutdownTestKit() + testkit2.system.whenTerminated.futureValue shouldBe a[Terminated] } } } // derivative classes should also work fine (esp the naming part -trait MyBaseSpec extends WordSpec with ActorTestKit with Matchers with BeforeAndAfterAll { - override protected def afterAll(): Unit = { - shutdownTestKit() - } -} +abstract class MyBaseSpec extends ActorTestKitWordSpec with WordSpecLike with Matchers class MyConcreteDerivateSpec extends MyBaseSpec { "A derivative test" should { - "generate a default name from the test class" in { + "generate a default name from the test class via ActorTestKitWordSpec" in { system.name should ===("MyConcreteDerivateSpec") } + + "generate a default name from the test class" in { + val testkit2 = ActorTestKit() + try { + testkit2.system.name should ===("MyConcreteDerivateSpec") + } finally + testkit2.shutdownTestKit() + } + + "use name from given class name" in { + val testkit2 = ActorTestKit(classOf[Vector[_]].getName) + try { + testkit2.system.name should ===("Vector") + } finally + testkit2.shutdownTestKit() + } } } +class CompositionSpec extends WordSpec with Matchers with BeforeAndAfterAll { + val testKit = ActorTestKit() + + override def afterAll(): Unit = { + testKit.shutdownTestKit() + } + + "generate a default name from the test class" in { + testKit.system.name should ===("CompositionSpec") + } +} + diff --git a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/TestProbeSpec.scala b/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/TestProbeSpec.scala index 1d1cd2c2f8..cbbfaf3747 100644 --- a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/TestProbeSpec.scala +++ b/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/TestProbeSpec.scala @@ -4,11 +4,11 @@ package akka.actor.testkit.typed.scaladsl -import akka.actor.typed.scaladsl.Behaviors - import scala.concurrent.duration._ -class TestProbeSpec extends AbstractActorSpec { +import akka.actor.typed.scaladsl.Behaviors + +class TestProbeSpec extends ActorTestKitWordSpec { def compileOnlyApiTest(): Unit = { val probe = TestProbe[AnyRef]() diff --git a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala b/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala similarity index 60% rename from akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala rename to akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala index 9e548cb8d0..02cb0dc9e0 100644 --- a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala +++ b/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala @@ -2,18 +2,20 @@ * Copyright (C) 2018 Lightbend Inc. */ -package akka.actor.testkit.typed.scaladsl +package docs.akka.actor.testkit.typed.scaladsl +import akka.actor.testkit.typed.scaladsl.ActorTestKit import akka.actor.typed._ import akka.actor.typed.scaladsl.Behaviors -import org.scalatest.{ BeforeAndAfterAll, WordSpec } +import org.scalatest.BeforeAndAfterAll +import org.scalatest.WordSpec object AsyncTestingExampleSpec { //#under-test case class Ping(msg: String, response: ActorRef[Pong]) case class Pong(msg: String) - val echoActor = Behaviors.receive[Ping] { (_, msg) ⇒ + val echoActor: Behavior[Ping] = Behaviors.receive { (_, msg) ⇒ msg match { case Ping(m, replyTo) ⇒ replyTo ! Pong(m) @@ -24,7 +26,8 @@ object AsyncTestingExampleSpec { } //#test-header -class AsyncTestingExampleSpec extends WordSpec with ActorTestKit with BeforeAndAfterAll { +class AsyncTestingExampleSpec extends WordSpec with BeforeAndAfterAll { + val testKit = ActorTestKit() //#test-header import AsyncTestingExampleSpec._ @@ -32,8 +35,8 @@ class AsyncTestingExampleSpec extends WordSpec with ActorTestKit with BeforeAndA "A testkit" must { "support verifying a response" in { //#test-spawn - val probe = TestProbe[Pong]() - val pinger = spawn(echoActor, "ping") + val pinger = testKit.spawn(echoActor, "ping") + val probe = testKit.createTestProbe[Pong]() pinger ! Ping("hello", probe.ref) probe.expectMessage(Pong("hello")) //#test-spawn @@ -41,15 +44,15 @@ class AsyncTestingExampleSpec extends WordSpec with ActorTestKit with BeforeAndA "support verifying a response - anonymous" in { //#test-spawn-anonymous - val probe = TestProbe[Pong]() - val pinger = spawn(echoActor) + val pinger = testKit.spawn(echoActor) + //#test-spawn-anonymous + val probe = testKit.createTestProbe[Pong]() pinger ! Ping("hello", probe.ref) probe.expectMessage(Pong("hello")) - //#test-spawn-anonymous } } //#test-shutdown - override def afterAll(): Unit = shutdownTestKit() + override def afterAll(): Unit = testKit.shutdownTestKit() //#test-shutdown } diff --git a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/ManualTimerExampleSpec.scala b/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/ManualTimerExampleSpec.scala similarity index 90% rename from akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/ManualTimerExampleSpec.scala rename to akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/ManualTimerExampleSpec.scala index fd730732f3..305716e486 100644 --- a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/ManualTimerExampleSpec.scala +++ b/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/ManualTimerExampleSpec.scala @@ -2,16 +2,17 @@ * Copyright (C) 2018 Lightbend Inc. */ -package akka.actor.testkit.typed.scaladsl +package docs.akka.actor.testkit.typed.scaladsl //#manual-scheduling-simple -import akka.actor.typed.scaladsl.Behaviors - import scala.concurrent.duration._ -class ManualTimerExampleSpec extends AbstractActorSpec { +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.testkit.typed.scaladsl.ManualTime +import akka.actor.testkit.typed.scaladsl.TestProbe +import akka.actor.typed.scaladsl.Behaviors - override def config = ManualTime.config +class ManualTimerExampleSpec extends ActorTestKitWordSpec(ManualTime.config) { val manualTime: ManualTime = ManualTime() diff --git a/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/ScalaTestIntegrationExampleSpec.scala b/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/ScalaTestIntegrationExampleSpec.scala new file mode 100644 index 0000000000..7e29cdb89b --- /dev/null +++ b/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/ScalaTestIntegrationExampleSpec.scala @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2018 Lightbend Inc. + */ + +package docs.akka.actor.testkit.typed.scaladsl + +//#scalatest-integration +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + +class ScalaTestIntegrationExampleSpec extends ActorTestKitWordSpec { + + "Something" must { + "behave correctly" in { + val probe = createTestProbe[String]() + // ... assertions etc. + } + } +} +//#scalatest-integration diff --git a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala b/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala similarity index 93% rename from akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala rename to akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala index 776cff489d..27660dbf24 100644 --- a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala +++ b/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala @@ -2,14 +2,17 @@ * Copyright (C) 2018 Lightbend Inc. */ -package akka.actor.testkit.typed.scaladsl +package docs.akka.actor.testkit.typed.scaladsl //#imports +import akka.actor.testkit.typed.Effect._ +import akka.actor.testkit.typed.scaladsl.BehaviorTestKit +import akka.actor.testkit.typed.scaladsl.TestInbox import akka.actor.typed._ import akka.actor.typed.scaladsl._ -import akka.actor.testkit.typed.Effect._ //#imports -import org.scalatest.{ Matchers, WordSpec } +import org.scalatest.Matchers +import org.scalatest.WordSpec object SyncTestingExampleSpec { //#child diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ActorContextSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ActorContextSpec.scala index 47bd4c2121..7801caaaf8 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ActorContextSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ActorContextSpec.scala @@ -6,11 +6,13 @@ package akka.actor.typed import akka.actor.InvalidMessageException import akka.actor.typed.scaladsl.Behaviors -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } - +import akka.actor.testkit.typed.scaladsl.TestProbe import scala.concurrent.duration._ import scala.reflect.ClassTag +import akka.actor.testkit.typed.TestException +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + object ActorSpecMessages { sealed trait Command @@ -59,7 +61,7 @@ object ActorSpecMessages { } -abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +abstract class ActorContextSpec extends ActorTestKitWordSpec { import ActorSpecMessages._ @@ -584,7 +586,6 @@ abstract class ActorContextSpec extends ActorTestKit with TypedAkkaSpecWithShutd } } - override def afterAll(): Unit = shutdownTestKit() } class NormalActorContextSpec extends ActorContextSpec { diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/AskSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/AskSpec.scala index c1d3ba5cc8..8124aa7f2d 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/AskSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/AskSpec.scala @@ -10,25 +10,23 @@ import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.Behaviors._ import akka.actor.typed.scaladsl.adapter._ import akka.testkit.EventFilter -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import akka.util.Timeout import com.typesafe.config.ConfigFactory -import org.scalatest.concurrent.ScalaFutures - import scala.concurrent.duration._ import scala.concurrent.{ ExecutionContext, TimeoutException } import scala.util.Success +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + object AskSpec { sealed trait Msg final case class Foo(s: String)(val replyTo: ActorRef[String]) extends Msg final case class Stop(replyTo: ActorRef[Unit]) extends Msg } -class AskSpec extends ActorTestKit - with TypedAkkaSpecWithShutdown with ScalaFutures { - override def name = "AskSpec" - override def config = ConfigFactory.parseString("akka.loggers = [ akka.testkit.TestEventListener ]") +class AskSpec extends ActorTestKitWordSpec( + "akka.loggers = [ akka.testkit.TestEventListener ]") { import AskSpec._ @@ -48,7 +46,7 @@ class AskSpec extends ActorTestKit "must fail the future if the actor is already terminated" in { val ref = spawn(behavior) (ref ? Stop).futureValue - val probe = TestProbe() + val probe = createTestProbe() probe.expectTerminated(ref, probe.remainingOrDefault) val answer = ref ? Foo("bar") val result = answer.failed.futureValue diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/BehaviorSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/BehaviorSpec.scala index 439c7112b5..f15f9542ad 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/BehaviorSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/BehaviorSpec.scala @@ -12,7 +12,11 @@ import akka.japi.pf.{ FI, PFBuilder } import java.util.function.{ Function ⇒ F1 } import akka.Done +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec import akka.actor.testkit.typed.scaladsl.{ BehaviorTestKit, TestInbox } +import org.scalactic.TypeCheckedTripleEquals +import org.scalatest.Matchers +import org.scalatest.WordSpecLike object BehaviorSpec { sealed trait Command { @@ -63,7 +67,7 @@ object BehaviorSpec { override def next = StateA } - trait Common extends TypedAkkaSpec { + trait Common extends WordSpecLike with Matchers with TypeCheckedTripleEquals { type Aux >: Null <: AnyRef def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) def checkAux(signal: Signal, aux: Aux): Unit = () @@ -338,7 +342,7 @@ object BehaviorSpec { import BehaviorSpec._ -class FullBehaviorSpec extends TypedAkkaSpec with Messages with BecomeWithLifecycle with Stoppable { +class FullBehaviorSpec extends ActorTestKitWordSpec with Messages with BecomeWithLifecycle with Stoppable { override def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) = mkFull(monitor) → null } @@ -374,7 +378,7 @@ class ReceiveBehaviorSpec extends Messages with BecomeWithLifecycle with Stoppab } } -class ImmutableWithSignalScalaBehaviorSpec extends TypedAkkaSpec with Messages with BecomeWithLifecycle with Stoppable { +class ImmutableWithSignalScalaBehaviorSpec extends Messages with BecomeWithLifecycle with Stoppable { override def behavior(monitor: ActorRef[Event]): (Behavior[Command], Aux) = behv(monitor) → null diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/DeferredSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/DeferredSpec.scala index 85305d7408..0148a456bf 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/DeferredSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/DeferredSpec.scala @@ -12,6 +12,8 @@ import scala.util.control.NoStackTrace import akka.actor.ActorInitializationException import com.typesafe.config.ConfigFactory +import org.scalatest.Matchers +import org.scalatest.WordSpec object DeferredSpec { sealed trait Command @@ -29,12 +31,10 @@ object DeferredSpec { }) } -class DeferredSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { - - override def config = ConfigFactory.parseString( - """ - akka.loggers = [akka.testkit.TestEventListener] - """) +class DeferredSpec extends ActorTestKitWordSpec( + """ + akka.loggers = [akka.testkit.TestEventListener] + """) { import DeferredSpec._ implicit val testSettings = TestKitSettings(system) @@ -150,7 +150,7 @@ class DeferredSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { } } -class DeferredStubbedSpec extends TypedAkkaSpec { +class DeferredStubbedSpec extends WordSpec with Matchers { import DeferredSpec._ diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ExtensionsSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ExtensionsSpec.scala index 55e3370001..a0adc6d5cd 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ExtensionsSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/ExtensionsSpec.scala @@ -11,6 +11,7 @@ import scala.concurrent.Future import akka.actor.BootstrapSetup import akka.actor.setup.ActorSystemSetup +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec class DummyExtension1 extends Extension object DummyExtension1 extends ExtensionId[DummyExtension1] { @@ -59,30 +60,30 @@ akka.actor.typed { """).resolve() } -class ExtensionsSpec extends TypedAkkaSpec { +class ExtensionsSpec extends ActorTestKitWordSpec { "The extensions subsystem" must { "return the same instance for the same id" in - withEmptyActorSystem("ExtensionsSpec01") { system ⇒ - val instance1 = system.registerExtension(DummyExtension1) - val instance2 = system.registerExtension(DummyExtension1) + withEmptyActorSystem("ExtensionsSpec01") { sys ⇒ + val instance1 = sys.registerExtension(DummyExtension1) + val instance2 = sys.registerExtension(DummyExtension1) instance1 should be theSameInstanceAs instance2 - val instance3 = DummyExtension1(system) + val instance3 = DummyExtension1(sys) instance3 should be theSameInstanceAs instance2 - val instance4 = DummyExtension1.get(system) + val instance4 = DummyExtension1.get(sys) instance4 should be theSameInstanceAs instance3 } "return the same instance for the same id concurrently" in - withEmptyActorSystem("ExtensionsSpec02") { system ⇒ + withEmptyActorSystem("ExtensionsSpec02") { sys ⇒ // not exactly water tight but better than nothing - import system.executionContext + import sys.executionContext val futures = (0 to 1000).map(n ⇒ Future { - system.registerExtension(SlowExtension) + sys.registerExtension(SlowExtension) } ) @@ -99,12 +100,12 @@ class ExtensionsSpec extends TypedAkkaSpec { """ akka.actor.typed.extensions = ["akka.actor.typed.DummyExtension1$", "akka.actor.typed.SlowExtension$"] """)) - ) { system ⇒ - system.hasExtension(DummyExtension1) should ===(true) - system.extension(DummyExtension1) shouldBe a[DummyExtension1] + ) { sys ⇒ + sys.hasExtension(DummyExtension1) should ===(true) + sys.extension(DummyExtension1) shouldBe a[DummyExtension1] - system.hasExtension(SlowExtension) should ===(true) - system.extension(SlowExtension) shouldBe a[SlowExtension] + sys.hasExtension(SlowExtension) should ===(true) + sys.extension(SlowExtension) shouldBe a[SlowExtension] } "handle extensions that fail to initialize" in { @@ -125,18 +126,18 @@ class ExtensionsSpec extends TypedAkkaSpec { } "support multiple instances of the same type of extension (with different ids)" in - withEmptyActorSystem("ExtensionsSpec06") { system ⇒ + withEmptyActorSystem("ExtensionsSpec06") { sys ⇒ val id1 = new MultiExtensionId(1) val id2 = new MultiExtensionId(2) - system.registerExtension(id1).n should ===(1) - system.registerExtension(id2).n should ===(2) - system.registerExtension(id1).n should ===(1) + sys.registerExtension(id1).n should ===(1) + sys.registerExtension(id2).n should ===(2) + sys.registerExtension(id1).n should ===(1) } "allow for auto-loading of library-extensions" in - withEmptyActorSystem("ExtensionsSpec06") { system ⇒ - val listedExtensions = system.settings.config.getStringList("akka.actor.typed.library-extensions") + withEmptyActorSystem("ExtensionsSpec06") { sys ⇒ + val listedExtensions = sys.settings.config.getStringList("akka.actor.typed.library-extensions") listedExtensions.size should be > 0 // could be initialized by other tests, so at least once InstanceCountingExtension.createCount.get() should be > 0 @@ -159,10 +160,10 @@ class ExtensionsSpec extends TypedAkkaSpec { } "load an extension implemented in Java" in - withEmptyActorSystem("ExtensionsSpec09") { system ⇒ + withEmptyActorSystem("ExtensionsSpec09") { sys ⇒ // no way to make apply work cleanly with extensions implemented in Java - val instance1 = ExtensionsTest.MyExtension.get(system) - val instance2 = ExtensionsTest.MyExtension.get(system) + val instance1 = ExtensionsTest.MyExtension.get(sys) + val instance2 = ExtensionsTest.MyExtension.get(sys) instance1 should be theSameInstanceAs instance2 } @@ -205,14 +206,14 @@ class ExtensionsSpec extends TypedAkkaSpec { akka.actor.typed.extensions = ["akka.actor.typed.DummyExtension1$", "akka.actor.typed.SlowExtension$"] """)), Some(ActorSystemSetup(new DummyExtension1Setup(sys ⇒ new DummyExtension1ViaSetup))) - ) { system ⇒ - system.hasExtension(DummyExtension1) should ===(true) - system.extension(DummyExtension1) shouldBe a[DummyExtension1ViaSetup] - DummyExtension1(system) shouldBe a[DummyExtension1ViaSetup] - DummyExtension1(system) shouldBe theSameInstanceAs(DummyExtension1(system)) + ) { sys ⇒ + sys.hasExtension(DummyExtension1) should ===(true) + sys.extension(DummyExtension1) shouldBe a[DummyExtension1ViaSetup] + DummyExtension1(sys) shouldBe a[DummyExtension1ViaSetup] + DummyExtension1(sys) shouldBe theSameInstanceAs(DummyExtension1(sys)) - system.hasExtension(SlowExtension) should ===(true) - system.extension(SlowExtension) shouldBe a[SlowExtension] + sys.hasExtension(SlowExtension) should ===(true) + sys.extension(SlowExtension) shouldBe a[SlowExtension] } } @@ -223,11 +224,11 @@ class ExtensionsSpec extends TypedAkkaSpec { case Some(c) ⇒ BootstrapSetup(c) case None ⇒ BootstrapSetup(ExtensionsSpec.config) } - val system = setup match { + val sys = setup match { case None ⇒ ActorSystem[Any](Behavior.EmptyBehavior, name, bootstrap) case Some(s) ⇒ ActorSystem[Any](Behavior.EmptyBehavior, name, s.and(bootstrap)) } - try f(system) finally system.terminate().futureValue + try f(sys) finally sys.terminate().futureValue } } diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/InterceptSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/InterceptSpec.scala index 2bc5110c79..1bb4ea38eb 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/InterceptSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/InterceptSpec.scala @@ -13,15 +13,14 @@ import org.scalatest.WordSpecLike import scala.concurrent.duration._ import akka.actor.ActorInitializationException +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec import com.typesafe.config.ConfigFactory -class InterceptSpec extends ActorTestKit with WordSpecLike with TypedAkkaSpecWithShutdown { - import BehaviorInterceptor._ - - override def config = ConfigFactory.parseString( - """ +class InterceptSpec extends ActorTestKitWordSpec( + """ akka.loggers = [akka.testkit.TestEventListener] - """) + """) { + import BehaviorInterceptor._ // FIXME eventfilter support in typed testkit import scaladsl.adapter._ diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/MonitorSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/MonitorSpec.scala index 76f3b5a056..0828ac7443 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/MonitorSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/MonitorSpec.scala @@ -4,10 +4,11 @@ package akka.actor.typed -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.testkit.typed.scaladsl.TestProbe import akka.actor.typed.scaladsl.Behaviors -class MonitorSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class MonitorSpec extends ActorTestKitWordSpec { "The monitor behavior" should { diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/OrElseSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/OrElseSpec.scala index 9bacbda5fb..17d4d57d2a 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/OrElseSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/OrElseSpec.scala @@ -6,7 +6,8 @@ package akka.actor.typed import akka.actor.typed.scaladsl.Behaviors import akka.actor.testkit.typed.scaladsl._ -import org.scalatest.WordSpecLike +import org.scalatest.Matchers +import org.scalatest.WordSpec object OrElseSpec { sealed trait Ping @@ -44,7 +45,7 @@ object OrElseSpec { } -class OrElseSpec extends WordSpecLike with TypedAkkaSpec { +class OrElseSpec extends WordSpec with Matchers { import OrElseSpec._ diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/PropsSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/PropsSpec.scala index f6405fa87a..6e8e3e0e5d 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/PropsSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/PropsSpec.scala @@ -4,7 +4,10 @@ package akka.actor.typed -class PropsSpec extends TypedAkkaSpec { +import org.scalatest.Matchers +import org.scalatest.WordSpec + +class PropsSpec extends WordSpec with Matchers { val dispatcherFirst = DispatcherDefault(DispatcherFromConfig("pool")) diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SpawnProtocolSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SpawnProtocolSpec.scala index 4002361dd6..d296485496 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SpawnProtocolSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SpawnProtocolSpec.scala @@ -10,6 +10,8 @@ import akka.actor.testkit.typed.TestKitSettings import akka.actor.testkit.typed.scaladsl._ import akka.actor.typed.scaladsl.Behaviors import akka.util.Timeout +import org.scalatest.Matchers +import org.scalatest.WordSpec object SpawnProtocolSpec { sealed trait Message @@ -24,7 +26,7 @@ object SpawnProtocolSpec { } } -class SpawnProtocolSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class SpawnProtocolSpec extends ActorTestKitWordSpec { import SpawnProtocolSpec._ implicit val testSettings = TestKitSettings(system) @@ -93,7 +95,7 @@ class SpawnProtocolSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { } } -class StubbedSpawnProtocolSpec extends TypedAkkaSpec { +class StubbedSpawnProtocolSpec extends WordSpec with Matchers { import SpawnProtocolSpec._ diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SupervisionSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SupervisionSpec.scala index e0f5bd8abd..0691a53d31 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SupervisionSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/SupervisionSpec.scala @@ -13,7 +13,6 @@ import akka.actor.typed.scaladsl.Behaviors._ import akka.testkit.EventFilter import akka.actor.testkit.typed.scaladsl._ import akka.actor.testkit.typed._ -import com.typesafe.config.ConfigFactory import org.scalatest.{ Matchers, WordSpec } import scala.concurrent.duration._ @@ -243,15 +242,14 @@ class StubbedSupervisionSpec extends WordSpec with Matchers { } } -class SupervisionSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { - import BehaviorInterceptor._ - - override def config = ConfigFactory.parseString( - """ - akka.loggers = [akka.testkit.TestEventListener] - """) +class SupervisionSpec extends ActorTestKitWordSpec( + """ + akka.loggers = [akka.testkit.TestEventListener] + """) { import SupervisionSpec._ + import BehaviorInterceptor._ + private val nameCounter = Iterator.from(0) private def nextName(prefix: String = "a"): String = s"$prefix-${nameCounter.next()}" @@ -848,7 +846,7 @@ class SupervisionSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { Behaviors.supervise(Behaviors.stopped[Command]) .onFailure(strategy) ) - TestProbe().expectTerminated(actor, 3.second) + createTestProbe().expectTerminated(actor, 3.second) } "that is stopped after setup should be stopped" in { @@ -858,7 +856,7 @@ class SupervisionSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { Behaviors.stopped) ).onFailure(strategy) ) - TestProbe().expectTerminated(actor, 3.second) + createTestProbe().expectTerminated(actor, 3.second) } // this test doesn't make sense for Resume since there will be no second setup @@ -882,7 +880,7 @@ class SupervisionSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { EventFilter[TE](occurrences = 1).intercept { actor ! "boom" } - TestProbe().expectTerminated(actor, 3.second) + createTestProbe().expectTerminated(actor, 3.second) } } } diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/TimerSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/TimerSpec.scala index 706707df5f..5ffe6016f1 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/TimerSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/TimerSpec.scala @@ -10,13 +10,13 @@ import java.util.concurrent.atomic.AtomicInteger import scala.concurrent.duration._ import scala.util.control.NoStackTrace + +import akka.actor.testkit.typed.scaladsl._ import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.TimerScheduler import akka.testkit.TimingTest -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, _ } -import org.scalatest.WordSpecLike -class TimerSpec extends ActorTestKit with WordSpecLike with TypedAkkaSpecWithShutdown { +class TimerSpec extends ActorTestKitWordSpec { sealed trait Command case class Tick(n: Int) extends Command diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/TypedAkkaSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/TypedAkkaSpec.scala deleted file mode 100644 index 8771c57acf..0000000000 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/TypedAkkaSpec.scala +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2018 Lightbend Inc. - */ - -package akka.actor.typed - -import akka.actor.testkit.typed.scaladsl.{ TestInbox, ActorTestKit } -import org.scalactic.TypeCheckedTripleEquals -import org.scalatest.concurrent.{ Eventually, ScalaFutures } -import org.scalatest.time.Span -import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpecLike } - -import scala.concurrent.duration._ -import scala.util.control.NoStackTrace - -/** - * Helper trait to include standard traits for typed tests - */ -trait TypedAkkaSpec extends WordSpecLike with Matchers with BeforeAndAfterAll with ScalaFutures - with TypeCheckedTripleEquals with Eventually { - - implicit override val patienceConfig: PatienceConfig = PatienceConfig(3.seconds, Span(100, org.scalatest.time.Millis)) - - def assertEmpty(inboxes: TestInbox[_]*): Unit = { - inboxes foreach (i ⇒ withClue(s"inbox $i had messages")(i.hasMessages should be(false))) - } - -} - -/** - * Helper that also shuts down the actor system if using [[ActorTestKit]] - */ -trait TypedAkkaSpecWithShutdown extends TypedAkkaSpec { - self: ActorTestKit ⇒ - override protected def afterAll(): Unit = shutdownTestKit() -} - -class TestException(msg: String) extends RuntimeException(msg) with NoStackTrace diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WatchSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WatchSpec.scala index 25c4837758..e32a7080b9 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WatchSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WatchSpec.scala @@ -9,10 +9,12 @@ import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.MutableBehavior import akka.actor.typed.scaladsl.adapter._ import akka.testkit.EventFilter -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } - +import akka.actor.testkit.typed.scaladsl.TestProbe import scala.concurrent._ import scala.concurrent.duration._ + +import akka.actor.testkit.typed.TestException +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec import com.typesafe.config.ConfigFactory object WatchSpec { @@ -39,10 +41,10 @@ object WatchSpec { case class StartWatchingWith(watchee: ActorRef[Stop.type], msg: CustomTerminationMessage) extends Message } -class WatchSpec extends ActorTestKit - with TypedAkkaSpecWithShutdown { +class WatchSpec extends ActorTestKitWordSpec(WatchSpec.config) { + // FIXME why systemActor? spawn? + import testKit.systemActor - override def config = WatchSpec.config implicit def untypedSystem = system.toUntyped import WatchSpec._ diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WidenSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WidenSpec.scala index 53d8025c95..40e2656af7 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WidenSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/WidenSpec.scala @@ -6,11 +6,11 @@ package akka.actor.typed import java.util.concurrent.atomic.AtomicInteger -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.testkit.typed.scaladsl.TestProbe import akka.actor.typed.scaladsl.Behaviors -class WidenSpec extends ActorTestKit - with TypedAkkaSpecWithShutdown { +class WidenSpec extends ActorTestKitWordSpec { "Widen" should { diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/ActorRefSerializationSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/ActorRefSerializationSpec.scala index acf9f18332..17a0e9543c 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/ActorRefSerializationSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/ActorRefSerializationSpec.scala @@ -6,9 +6,9 @@ package akka.actor.typed.internal import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.adapter._ -import akka.actor.typed.{ ActorRef, TypedAkkaSpecWithShutdown } +import akka.actor.typed.ActorRef import akka.serialization.{ JavaSerializer, SerializationExtension } -import akka.actor.testkit.typed.scaladsl.ActorTestKit +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec import com.typesafe.config.ConfigFactory object ActorRefSerializationSpec { @@ -25,9 +25,7 @@ object ActorRefSerializationSpec { case class MessageWrappingActorRef(s: String, ref: ActorRef[Unit]) extends java.io.Serializable } -class ActorRefSerializationSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { - - override def config = ActorRefSerializationSpec.config +class ActorRefSerializationSpec extends ActorTestKitWordSpec(ActorRefSerializationSpec.config) { val serialization = SerializationExtension(system.toUntyped) diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/LocalReceptionistSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/LocalReceptionistSpec.scala index d43586e90c..b0b5933e60 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/LocalReceptionistSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/LocalReceptionistSpec.scala @@ -4,19 +4,22 @@ package akka.actor.typed.internal.receptionist -import akka.actor.typed._ -import akka.actor.typed.receptionist.Receptionist._ -import akka.actor.typed.receptionist.{ Receptionist, ServiceKey } -import akka.actor.typed.scaladsl.AskPattern._ -import akka.actor.typed.scaladsl.Behaviors -import akka.actor.testkit.typed.TestKitSettings -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, BehaviorTestKit, TestInbox, TestProbe } -import org.scalatest.concurrent.Eventually - import scala.concurrent.Future -class LocalReceptionistSpec extends ActorTestKit with TypedAkkaSpecWithShutdown with Eventually { +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.testkit.typed.scaladsl.BehaviorTestKit +import akka.actor.testkit.typed.scaladsl.TestInbox +import akka.actor.testkit.typed.scaladsl.TestProbe +import akka.actor.typed._ +import akka.actor.typed.receptionist.Receptionist +import akka.actor.typed.receptionist.Receptionist._ +import akka.actor.typed.receptionist.ServiceKey +import akka.actor.typed.scaladsl.AskPattern._ +import akka.actor.typed.scaladsl.Behaviors +import org.scalatest.Matchers +import org.scalatest.WordSpec +object LocalReceptionistSpec { trait ServiceA val ServiceKeyA = ServiceKey[ServiceA]("service-a") val behaviorA = Behaviors.empty[ServiceA] @@ -33,7 +36,10 @@ class LocalReceptionistSpec extends ActorTestKit with TypedAkkaSpecWithShutdown } } - implicit val testSettings = TestKitSettings(system) +} + +class LocalReceptionistSpec extends ActorTestKitWordSpec { + import LocalReceptionistSpec._ abstract class TestSetup { val receptionist = spawn(LocalReceptionist.behavior) @@ -41,54 +47,6 @@ class LocalReceptionistSpec extends ActorTestKit with TypedAkkaSpecWithShutdown "A local receptionist" must { - "register a service" in { - val testkit = BehaviorTestKit(LocalReceptionist.behavior) - val a = TestInbox[ServiceA]("a") - val r = TestInbox[Registered]("r") - testkit.run(Register(ServiceKeyA, a.ref, r.ref)) - testkit.retrieveEffect() // watching however that is implemented - r.receiveMessage() should be(Registered(ServiceKeyA, a.ref)) - val q = TestInbox[Listing]("q") - testkit.run(Find(ServiceKeyA, q.ref)) - testkit.retrieveAllEffects() should be(Nil) - q.receiveMessage() should be(Listing(ServiceKeyA, Set(a.ref))) - assertEmpty(a, r, q) - } - - "register two services" in { - val testkit = BehaviorTestKit(LocalReceptionist.behavior) - val a = TestInbox[ServiceA]("a") - val r = TestInbox[Registered]("r") - testkit.run(Register(ServiceKeyA, a.ref, r.ref)) - r.receiveMessage() should be(Registered(ServiceKeyA, a.ref)) - val b = TestInbox[ServiceB]("b") - testkit.run(Register(ServiceKeyB, b.ref, r.ref)) - r.receiveMessage() should be(Registered(ServiceKeyB, b.ref)) - val q = TestInbox[Listing]("q") - testkit.run(Find(ServiceKeyA, q.ref)) - q.receiveMessage() should be(Listing(ServiceKeyA, Set(a.ref))) - testkit.run(Find(ServiceKeyB, q.ref)) - q.receiveMessage() should be(Listing(ServiceKeyB, Set(b.ref))) - assertEmpty(a, b, r, q) - } - - "register two services with the same key" in { - val testkit = BehaviorTestKit(LocalReceptionist.behavior) - val a1 = TestInbox[ServiceA]("a1") - val r = TestInbox[Registered]("r") - testkit.run(Register(ServiceKeyA, a1.ref, r.ref)) - r.receiveMessage() should be(Registered(ServiceKeyA, a1.ref)) - val a2 = TestInbox[ServiceA]("a2") - testkit.run(Register(ServiceKeyA, a2.ref, r.ref)) - r.receiveMessage() should be(Registered(ServiceKeyA, a2.ref)) - val q = TestInbox[Listing]("q") - testkit.run(Find(ServiceKeyA, q.ref)) - q.receiveMessage() should be(Listing(ServiceKeyA, Set(a1.ref, a2.ref))) - testkit.run(Find(ServiceKeyB, q.ref)) - q.receiveMessage() should be(Listing(ServiceKeyB, Set.empty[ActorRef[ServiceB]])) - assertEmpty(a1, a2, r, q) - } - "unregister services when they terminate" in { new TestSetup { val regProbe = TestProbe[Any]("regProbe") @@ -167,3 +125,63 @@ class LocalReceptionistSpec extends ActorTestKit with TypedAkkaSpecWithShutdown } } } + +class LocalReceptionistBehaviorSpec extends WordSpec with Matchers { + import LocalReceptionistSpec._ + + def assertEmpty(inboxes: TestInbox[_]*): Unit = { + inboxes foreach (i ⇒ withClue(s"inbox $i had messages")(i.hasMessages should be(false))) + } + + "A local receptionist behavior" must { + + "register a service" in { + val testkit = BehaviorTestKit(LocalReceptionist.behavior) + val a = TestInbox[ServiceA]("a") + val r = TestInbox[Registered]("r") + testkit.run(Register(ServiceKeyA, a.ref, r.ref)) + testkit.retrieveEffect() // watching however that is implemented + r.receiveMessage() should be(Registered(ServiceKeyA, a.ref)) + val q = TestInbox[Listing]("q") + testkit.run(Find(ServiceKeyA, q.ref)) + testkit.retrieveAllEffects() should be(Nil) + q.receiveMessage() should be(Listing(ServiceKeyA, Set(a.ref))) + assertEmpty(a, r, q) + } + + "register two services" in { + val testkit = BehaviorTestKit(LocalReceptionist.behavior) + val a = TestInbox[ServiceA]("a") + val r = TestInbox[Registered]("r") + testkit.run(Register(ServiceKeyA, a.ref, r.ref)) + r.receiveMessage() should be(Registered(ServiceKeyA, a.ref)) + val b = TestInbox[ServiceB]("b") + testkit.run(Register(ServiceKeyB, b.ref, r.ref)) + r.receiveMessage() should be(Registered(ServiceKeyB, b.ref)) + val q = TestInbox[Listing]("q") + testkit.run(Find(ServiceKeyA, q.ref)) + q.receiveMessage() should be(Listing(ServiceKeyA, Set(a.ref))) + testkit.run(Find(ServiceKeyB, q.ref)) + q.receiveMessage() should be(Listing(ServiceKeyB, Set(b.ref))) + assertEmpty(a, b, r, q) + } + + "register two services with the same key" in { + val testkit = BehaviorTestKit(LocalReceptionist.behavior) + val a1 = TestInbox[ServiceA]("a1") + val r = TestInbox[Registered]("r") + testkit.run(Register(ServiceKeyA, a1.ref, r.ref)) + r.receiveMessage() should be(Registered(ServiceKeyA, a1.ref)) + val a2 = TestInbox[ServiceA]("a2") + testkit.run(Register(ServiceKeyA, a2.ref, r.ref)) + r.receiveMessage() should be(Registered(ServiceKeyA, a2.ref)) + val q = TestInbox[Listing]("q") + testkit.run(Find(ServiceKeyA, q.ref)) + q.receiveMessage() should be(Listing(ServiceKeyA, Set(a1.ref, a2.ref))) + testkit.run(Find(ServiceKeyB, q.ref)) + q.receiveMessage() should be(Listing(ServiceKeyB, Set.empty[ActorRef[ServiceB]])) + assertEmpty(a1, a2, r, q) + } + + } +} diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/ServiceKeySerializationSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/ServiceKeySerializationSpec.scala index 71216a6ae4..2dd73ba825 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/ServiceKeySerializationSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/internal/receptionist/ServiceKeySerializationSpec.scala @@ -4,16 +4,13 @@ package akka.actor.typed.internal.receptionist -import akka.actor.typed.TypedAkkaSpecWithShutdown import akka.actor.typed.internal.ActorRefSerializationSpec import akka.actor.typed.receptionist.ServiceKey import akka.actor.typed.scaladsl.adapter._ import akka.serialization.SerializationExtension -import akka.actor.testkit.typed.scaladsl.ActorTestKit +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec -class ServiceKeySerializationSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { - - override def config = ActorRefSerializationSpec.config +class ServiceKeySerializationSpec extends ActorTestKitWordSpec(ActorRefSerializationSpec.config) { val serialization = SerializationExtension(system.toUntyped) diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorContextAskSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorContextAskSpec.scala index 75c4c6920d..80c433b2dc 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorContextAskSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorContextAskSpec.scala @@ -5,16 +5,17 @@ package akka.actor.typed.scaladsl import akka.actor.typed.scaladsl.adapter._ -import akka.actor.typed.{ ActorRef, PostStop, Props, TypedAkkaSpecWithShutdown } +import akka.actor.typed.{ ActorRef, PostStop, Props } import akka.testkit.EventFilter -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import com.typesafe.config.ConfigFactory - import scala.concurrent.TimeoutException import scala.concurrent.duration._ import scala.reflect.ClassTag import scala.util.{ Failure, Success } +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + object ActorContextAskSpec { val config = ConfigFactory.parseString( """ @@ -30,9 +31,7 @@ object ActorContextAskSpec { """) } -class ActorContextAskSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { - - override def config = ActorContextAskSpec.config +class ActorContextAskSpec extends ActorTestKitWordSpec(ActorContextAskSpec.config) { implicit val untyped = system.toUntyped // FIXME no typed event filter yet diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala index c50fa9ebbc..f7f785c97b 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ActorLoggingSpec.scala @@ -6,21 +6,18 @@ package akka.actor.typed.scaladsl import java.util.concurrent.atomic.AtomicInteger +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.testkit.typed.TestException import akka.actor.typed.scaladsl.adapter._ -import akka.actor.typed.{ Behavior, LogMarker, TestException, TypedAkkaSpec } +import akka.actor.typed.{ Behavior, LogMarker } import akka.event.Logging import akka.event.Logging.{ LogEventWithCause, LogEventWithMarker } import akka.testkit.EventFilter -import akka.actor.testkit.typed.scaladsl.ActorTestKit -import com.typesafe.config.ConfigFactory -class ActorLoggingSpec extends ActorTestKit with TypedAkkaSpec { - - override def config = ConfigFactory.parseString( - """ +class ActorLoggingSpec extends ActorTestKitWordSpec(""" akka.loglevel = DEBUG # test verifies debug akka.loggers = ["akka.testkit.TestEventListener"] - """) + """) { val marker = LogMarker("marker") val cause = new TestException("böö") diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/GracefulStopSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/GracefulStopSpec.scala index 8497fc510b..f721baedce 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/GracefulStopSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/GracefulStopSpec.scala @@ -7,9 +7,10 @@ package scaladsl import akka.Done import akka.NotUsed -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.testkit.typed.scaladsl.TestProbe -final class GracefulStopSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +final class GracefulStopSpec extends ActorTestKitWordSpec { "Graceful stop" must { diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/MessageAdapterSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/MessageAdapterSpec.scala index 91ec9d9cb3..3090d0e5d6 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/MessageAdapterSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/MessageAdapterSpec.scala @@ -4,15 +4,15 @@ package akka.actor.typed.scaladsl +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.testkit.typed.TestException import akka.actor.typed.scaladsl.adapter._ import akka.actor.typed.ActorRef import akka.actor.typed.Behavior import akka.actor.typed.PostStop import akka.actor.typed.Props -import akka.actor.typed.TestException -import akka.actor.typed.TypedAkkaSpecWithShutdown import akka.testkit.EventFilter -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import com.typesafe.config.ConfigFactory object MessageAdapterSpec { @@ -31,9 +31,8 @@ object MessageAdapterSpec { """) } -class MessageAdapterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class MessageAdapterSpec extends ActorTestKitWordSpec(MessageAdapterSpec.config) { - override def config = MessageAdapterSpec.config implicit val untyped = system.toUntyped // FIXME no typed event filter yet "Message adapters" must { diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/OnSignalSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/OnSignalSpec.scala index 609fd55ce4..094f4205cb 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/OnSignalSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/OnSignalSpec.scala @@ -6,9 +6,10 @@ package akka.actor.typed package scaladsl import akka.Done -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.testkit.typed.scaladsl.TestProbe -final class OnSignalSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +final class OnSignalSpec extends ActorTestKitWordSpec { "An Actor.OnSignal behavior" must { "must correctly install the signal handler" in { diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ReceivePartialSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ReceivePartialSpec.scala index dd7e03e1d1..26c975ec0a 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ReceivePartialSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ReceivePartialSpec.scala @@ -5,9 +5,10 @@ package akka.actor.typed package scaladsl -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.testkit.typed.scaladsl.TestProbe -class ReceivePartialSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class ReceivePartialSpec extends ActorTestKitWordSpec { "An immutable partial" must { diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashSpec.scala index 10d661e858..f06f513ad9 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StashSpec.scala @@ -5,7 +5,8 @@ package akka.actor.typed package scaladsl -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.testkit.typed.scaladsl.TestProbe object StashSpec { sealed trait Command @@ -186,7 +187,7 @@ class MutableStashSpec extends StashSpec { def behaviorUnderTest: Behavior[Command] = Behaviors.setup(ctx ⇒ new MutableStash(ctx)) } -abstract class StashSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +abstract class StashSpec extends ActorTestKitWordSpec { import StashSpec._ def testQualifier: String diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StopSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StopSpec.scala index 6e2e3adb6c..51671f4cb3 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StopSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/StopSpec.scala @@ -4,15 +4,18 @@ package akka.actor.typed.scaladsl -import akka.Done -import akka.actor.testkit.typed.TE -import akka.actor.testkit.typed.scaladsl.ActorTestKit -import akka.actor.typed -import akka.actor.typed.{ Behavior, BehaviorInterceptor, PostStop, Signal, TypedAkkaSpecWithShutdown } - import scala.concurrent.Promise -class StopSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +import akka.Done +import akka.actor.testkit.typed.TE +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.typed +import akka.actor.typed.Behavior +import akka.actor.typed.BehaviorInterceptor +import akka.actor.typed.PostStop +import akka.actor.typed.Signal + +class StopSpec extends ActorTestKitWordSpec { import BehaviorInterceptor._ "Stopping an actor" should { diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/DispatchersDocSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/DispatchersDocSpec.scala index d16ec62cc5..321788b311 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/DispatchersDocSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/DispatchersDocSpec.scala @@ -4,15 +4,15 @@ package docs.akka.typed -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import akka.actor.typed.scaladsl.AskPattern._ import akka.actor.typed.SpawnProtocol.Spawn import akka.actor.typed.scaladsl.Behaviors -import akka.actor.typed.{ ActorRef, Behavior, SpawnProtocol, TypedAkkaSpecWithShutdown, Props, DispatcherSelector } +import akka.actor.typed.{ ActorRef, Behavior, DispatcherSelector, Props, SpawnProtocol } import akka.dispatch.Dispatcher -import org.scalatest.concurrent.ScalaFutures import DispatchersDocSpec._ -import com.typesafe.config.{ Config, ConfigFactory } +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import com.typesafe.config.ConfigFactory object DispatchersDocSpec { @@ -55,9 +55,7 @@ object DispatchersDocSpec { } -class DispatchersDocSpec extends ActorTestKit with TypedAkkaSpecWithShutdown with ScalaFutures { - - override def config: Config = DispatchersDocSpec.config +class DispatchersDocSpec extends ActorTestKitWordSpec(DispatchersDocSpec.config) { "Actor Dispatchers" should { "support default and blocking dispatcher" in { diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FSMDocSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FSMDocSpec.scala index b5a7678030..b33176a004 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FSMDocSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FSMDocSpec.scala @@ -4,13 +4,14 @@ package docs.akka.typed -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import akka.actor.typed.scaladsl.Behaviors -import akka.actor.typed.{ ActorRef, Behavior, TypedAkkaSpecWithShutdown } - +import akka.actor.typed.{ ActorRef, Behavior } import scala.collection.immutable import scala.concurrent.duration._ +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + object FSMDocSpec { // messages and data types @@ -65,7 +66,7 @@ object FSMDocSpec { //#test-code } -class FSMDocSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class FSMDocSpec extends ActorTestKitWordSpec { import FSMDocSpec._ diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FaultToleranceDocSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FaultToleranceDocSpec.scala index f8656b3641..5b880d7680 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FaultToleranceDocSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/FaultToleranceDocSpec.scala @@ -5,17 +5,14 @@ package docs.akka.typed import akka.actor.typed.scaladsl.Behaviors -import akka.actor.typed.{ DeathPactException, SupervisorStrategy, TypedAkkaSpecWithShutdown } -import akka.actor.testkit.typed.scaladsl.ActorTestKit -import com.typesafe.config.ConfigFactory +import akka.actor.typed.{ DeathPactException, SupervisorStrategy } +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec -class FaultToleranceDocSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { - - override def config = ConfigFactory.parseString( - """ +class FaultToleranceDocSpec extends ActorTestKitWordSpec( + """ # silenced to not put noise in test logs akka.loglevel = OFF - """) + """) { "Bubbling of failures" must { diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/GracefulStopDocSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/GracefulStopDocSpec.scala index 26717df7bb..a445966525 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/GracefulStopDocSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/GracefulStopDocSpec.scala @@ -7,13 +7,12 @@ package docs.akka.typed //#imports import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.{ ActorSystem, Logger, PostStop } -import akka.actor.testkit.typed.scaladsl.ActorTestKit - import scala.concurrent.Await import scala.concurrent.duration._ + //#imports -import akka.actor.typed.TypedAkkaSpecWithShutdown +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec object GracefulStopDocSpec { @@ -68,7 +67,7 @@ object GracefulStopDocSpec { } -class GracefulStopDocSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class GracefulStopDocSpec extends ActorTestKitWordSpec { import GracefulStopDocSpec._ diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/InteractionPatternsSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/InteractionPatternsSpec.scala index 69ae971e46..f98f573cb5 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/InteractionPatternsSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/InteractionPatternsSpec.scala @@ -7,16 +7,17 @@ package docs.akka.typed import java.net.URI import akka.NotUsed -import akka.actor.typed.{ ActorRef, ActorSystem, Behavior, TypedAkkaSpecWithShutdown } +import akka.actor.typed.{ ActorRef, ActorSystem, Behavior } import akka.actor.typed.scaladsl.{ Behaviors, TimerScheduler } -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import akka.util.Timeout - import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.{ Failure, Success } -class InteractionPatternsSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + +class InteractionPatternsSpec extends ActorTestKitWordSpec { "The interaction patterns docs" must { diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/IntroSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/IntroSpec.scala index 476a07fb9d..5772e7b49c 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/IntroSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/IntroSpec.scala @@ -13,13 +13,12 @@ import akka.actor.typed.{ ActorRef, ActorSystem, Behavior, DispatcherSelector, T //#fiddle_code import akka.actor.testkit.typed.scaladsl.ActorTestKit +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec import java.net.URLEncoder import java.nio.charset.StandardCharsets import scala.concurrent.Await import scala.concurrent.duration._ -import akka.actor.typed.TypedAkkaSpecWithShutdown - object IntroSpec { //format: OFF //#fiddle_code @@ -171,7 +170,7 @@ object IntroSpec { } -class IntroSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class IntroSpec extends ActorTestKitWordSpec { import IntroSpec._ diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/MutableIntroSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/MutableIntroSpec.scala index 195e0ce4c4..f7bd4ae800 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/MutableIntroSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/MutableIntroSpec.scala @@ -10,10 +10,10 @@ import java.nio.charset.StandardCharsets import akka.actor.typed._ import akka.actor.typed.scaladsl.{ ActorContext, Behaviors, MutableBehavior } -import akka.actor.testkit.typed.scaladsl.ActorTestKit - import scala.concurrent.duration._ import scala.concurrent.Await + +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec //#imports object MutableIntroSpec { @@ -86,7 +86,7 @@ object MutableIntroSpec { } -class MutableIntroSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class MutableIntroSpec extends ActorTestKitWordSpec { import MutableIntroSpec._ diff --git a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/SpawnProtocolDocSpec.scala b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/SpawnProtocolDocSpec.scala index 85b22b232b..eb7d184834 100644 --- a/akka-actor-typed-tests/src/test/scala/docs/akka/typed/SpawnProtocolDocSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/docs/akka/typed/SpawnProtocolDocSpec.scala @@ -9,7 +9,7 @@ import scala.concurrent.Future import scala.concurrent.duration._ import akka.actor.testkit.typed.scaladsl.ActorTestKit -import akka.actor.typed.TypedAkkaSpecWithShutdown +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec import docs.akka.typed.IntroSpec.HelloWorld //#imports1 @@ -43,7 +43,7 @@ object SpawnProtocolDocSpec { //#main } -class SpawnProtocolDocSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class SpawnProtocolDocSpec extends ActorTestKitWordSpec { import SpawnProtocolDocSpec._ diff --git a/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/ShardingSerializerSpec.scala b/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/ShardingSerializerSpec.scala index 4941a35cf3..03cba9487b 100644 --- a/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/ShardingSerializerSpec.scala +++ b/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/ShardingSerializerSpec.scala @@ -4,13 +4,12 @@ package akka.cluster.sharding.typed -import akka.actor.typed.TypedAkkaSpecWithShutdown import akka.actor.typed.internal.adapter.ActorSystemAdapter import akka.cluster.sharding.typed.internal.ShardingSerializer import akka.serialization.SerializationExtension -import akka.actor.testkit.typed.scaladsl.ActorTestKit +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec -class ShardingSerializerSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class ShardingSerializerSpec extends ActorTestKitWordSpec { "The typed ShardingSerializer" must { diff --git a/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingPersistenceSpec.scala b/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingPersistenceSpec.scala index 17b9cf46e2..a916cab492 100644 --- a/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingPersistenceSpec.scala +++ b/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingPersistenceSpec.scala @@ -4,15 +4,15 @@ package akka.cluster.sharding.typed.scaladsl +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec import akka.actor.typed.ActorRef import akka.actor.typed.Behavior import akka.actor.typed.Props -import akka.actor.typed.TypedAkkaSpecWithShutdown import akka.cluster.sharding.typed.ClusterShardingSettings import akka.cluster.typed.Cluster import akka.cluster.typed.Join import akka.persistence.typed.scaladsl.{ Effect, PersistentBehaviors } -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import com.typesafe.config.ConfigFactory object ClusterShardingPersistenceSpec { @@ -59,12 +59,9 @@ object ClusterShardingPersistenceSpec { } -class ClusterShardingPersistenceSpec extends ActorTestKit - with TypedAkkaSpecWithShutdown { +class ClusterShardingPersistenceSpec extends ActorTestKitWordSpec(ClusterShardingPersistenceSpec.config) { import ClusterShardingPersistenceSpec._ - override def config = ClusterShardingPersistenceSpec.config - val sharding = ClusterSharding(system) "Typed cluster sharding with persistent actor" must { diff --git a/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingSpec.scala b/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingSpec.scala index 93ae68c8b9..ffe576a0f9 100644 --- a/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingSpec.scala +++ b/akka-cluster-sharding-typed/src/test/scala/akka/cluster/sharding/typed/scaladsl/ClusterShardingSpec.scala @@ -7,12 +7,14 @@ package akka.cluster.sharding.typed.scaladsl import java.nio.charset.StandardCharsets import scala.concurrent.duration._ + import akka.actor.ExtendedActorSystem +import akka.actor.testkit.typed.scaladsl.ActorTestKit +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec import akka.actor.typed.ActorRef import akka.actor.typed.ActorRefResolver import akka.actor.typed.ActorSystem import akka.actor.typed.Props -import akka.actor.typed.TypedAkkaSpecWithShutdown import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.adapter._ import akka.cluster.MemberStatus @@ -21,7 +23,7 @@ import akka.cluster.typed.Cluster import akka.cluster.typed.Join import akka.cluster.typed.Leave import akka.serialization.SerializerWithStringManifest -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import akka.util.Timeout import com.typesafe.config.ConfigFactory import org.scalatest.time.Span @@ -119,11 +121,9 @@ object ClusterShardingSpec { } -class ClusterShardingSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class ClusterShardingSpec extends ActorTestKitWordSpec(ClusterShardingSpec.config) { import ClusterShardingSpec._ - override def config = ClusterShardingSpec.config - val sharding = ClusterSharding(system) val system2 = ActorSystem(Behaviors.ignore[Any], name = system.name, config = system.settings.config) @@ -326,7 +326,6 @@ class ClusterShardingSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { Cluster(system2).manager ! Leave(Cluster(system2).selfMember.address) - implicit val patienceConfig: PatienceConfig = PatienceConfig(5.seconds, Span(100, org.scalatest.time.Millis)) eventually { // if it wouldn't receive the StopPlz and stop the leaving would take longer and this would fail Cluster(system2).isTerminated should ===(true) diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/ddata/typed/scaladsl/ReplicatorSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/ddata/typed/scaladsl/ReplicatorSpec.scala index d112228148..de989e7784 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/ddata/typed/scaladsl/ReplicatorSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/ddata/typed/scaladsl/ReplicatorSpec.scala @@ -5,7 +5,7 @@ package akka.cluster.ddata.typed.scaladsl import akka.actor.Scheduler -import akka.actor.typed.{ ActorRef, Behavior, TypedAkkaSpecWithShutdown } +import akka.actor.typed.{ ActorRef, Behavior } import akka.actor.typed.scaladsl.AskPattern._ import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.adapter._ @@ -16,7 +16,6 @@ import akka.actor.testkit.typed.scaladsl._ import akka.actor.testkit.typed.TestKitSettings import akka.util.Timeout import com.typesafe.config.ConfigFactory -import org.scalatest.concurrent.Eventually import scala.concurrent.Future import scala.concurrent.duration._ @@ -116,9 +115,7 @@ object ReplicatorSpec { } -class ReplicatorSpec extends ActorTestKit with TypedAkkaSpecWithShutdown with Eventually { - - override def config = ReplicatorSpec.config +class ReplicatorSpec extends ActorTestKitWordSpec(ReplicatorSpec.config) { import ReplicatorSpec._ diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterApiSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterApiSpec.scala index d3aaacfd7e..88f67c2675 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterApiSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterApiSpec.scala @@ -4,17 +4,14 @@ package akka.cluster.typed -import akka.actor.typed.TypedAkkaSpecWithShutdown import akka.actor.typed.scaladsl.adapter._ import akka.cluster.ClusterEvent._ import akka.cluster.MemberStatus -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import akka.actor.testkit.typed.TestKitSettings +import akka.actor.testkit.typed.scaladsl.ActorTestKit import com.typesafe.config.ConfigFactory -import org.scalatest.concurrent.ScalaFutures - -import scala.concurrent.Await -import scala.concurrent.duration._ +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec object ClusterApiSpec { val config = ConfigFactory.parseString( @@ -37,9 +34,7 @@ object ClusterApiSpec { """) } -class ClusterApiSpec extends ActorTestKit with TypedAkkaSpecWithShutdown with ScalaFutures { - - override def config = ClusterApiSpec.config +class ClusterApiSpec extends ActorTestKitWordSpec(ClusterApiSpec.config) { val testSettings = TestKitSettings(system) val clusterNode1 = Cluster(system) diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonApiSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonApiSpec.scala index e63288d94f..e2e62e29b5 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonApiSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonApiSpec.scala @@ -10,13 +10,15 @@ import akka.actor.ExtendedActorSystem import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.adapter._ import akka.actor.testkit.typed.TestKitSettings -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } -import akka.actor.typed.{ ActorRef, ActorRefResolver, Props, TypedAkkaSpecWithShutdown } +import akka.actor.testkit.typed.scaladsl.TestProbe +import akka.actor.typed.{ ActorRef, ActorRefResolver, Props } import akka.serialization.SerializerWithStringManifest import com.typesafe.config.ConfigFactory import scala.concurrent.Await import scala.concurrent.duration._ +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + object ClusterSingletonApiSpec { val config = ConfigFactory.parseString( @@ -86,11 +88,9 @@ object ClusterSingletonApiSpec { } } -class ClusterSingletonApiSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class ClusterSingletonApiSpec extends ActorTestKitWordSpec(ClusterSingletonApiSpec.config) { import ClusterSingletonApiSpec._ - override def config = ClusterSingletonApiSpec.config - implicit val testSettings = TestKitSettings(system) val clusterNode1 = Cluster(system) val untypedSystem1 = system.toUntyped diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonPersistenceSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonPersistenceSpec.scala index 029c7636f6..d3f8c2150d 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonPersistenceSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/ClusterSingletonPersistenceSpec.scala @@ -4,9 +4,10 @@ package akka.cluster.typed -import akka.actor.typed.{ ActorRef, Behavior, Props, TypedAkkaSpecWithShutdown } +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec +import akka.actor.typed.{ ActorRef, Behavior, Props } import akka.persistence.typed.scaladsl.{ Effect, PersistentBehaviors } -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import com.typesafe.config.ConfigFactory object ClusterSingletonPersistenceSpec { @@ -48,12 +49,10 @@ object ClusterSingletonPersistenceSpec { } -class ClusterSingletonPersistenceSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class ClusterSingletonPersistenceSpec extends ActorTestKitWordSpec(ClusterSingletonPersistenceSpec.config) { import ClusterSingletonPersistenceSpec._ import akka.actor.typed.scaladsl.adapter._ - override def config = ClusterSingletonPersistenceSpec.config - implicit val s = system implicit val untypedSystem = system.toUntyped diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteContextAskSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteContextAskSpec.scala index 6b2f641f0a..7e2dae6444 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteContextAskSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteContextAskSpec.scala @@ -10,16 +10,17 @@ import akka.actor.ExtendedActorSystem import akka.actor.typed.receptionist.Receptionist.Registered import akka.actor.typed.receptionist.{ Receptionist, ServiceKey } import akka.actor.typed.scaladsl.Behaviors -import akka.actor.typed.{ ActorRef, ActorRefResolver, ActorSystem, TypedAkkaSpecWithShutdown } +import akka.actor.typed.{ ActorRef, ActorRefResolver, ActorSystem } import akka.serialization.SerializerWithStringManifest -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import akka.actor.typed.scaladsl.adapter._ import akka.util.Timeout import com.typesafe.config.ConfigFactory - import scala.concurrent.duration._ import scala.util.{ Failure, Success } +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + class RemoteContextAskSpecSerializer(system: ExtendedActorSystem) extends SerializerWithStringManifest { override def identifier = 41 override def manifest(o: AnyRef) = o match { @@ -82,12 +83,10 @@ object RemoteContextAskSpec { } -class RemoteContextAskSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class RemoteContextAskSpec extends ActorTestKitWordSpec(RemoteContextAskSpec.config) { import RemoteContextAskSpec._ - override def config = RemoteContextAskSpec.config - "Asking another actor through the ActorContext across remoting" must { "work" in { diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteDeployNotAllowedSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteDeployNotAllowedSpec.scala index 940a045fdc..c4dd7599a0 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteDeployNotAllowedSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/RemoteDeployNotAllowedSpec.scala @@ -5,13 +5,14 @@ package akka.cluster.typed import akka.actor.typed.ActorSystem -import akka.actor.typed.TypedAkkaSpecWithShutdown import akka.actor.typed.scaladsl.Behaviors -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import com.typesafe.config.ConfigFactory - import scala.concurrent.duration._ +import akka.actor.testkit.typed.scaladsl.ActorTestKit +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + object RemoteDeployNotAllowedSpec { def config = ConfigFactory.parseString( s""" @@ -43,9 +44,7 @@ object RemoteDeployNotAllowedSpec { """).withFallback(config) } -class RemoteDeployNotAllowedSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { - - override def config = RemoteDeployNotAllowedSpec.config +class RemoteDeployNotAllowedSpec extends ActorTestKitWordSpec(RemoteDeployNotAllowedSpec.config) { "Typed cluster" must { diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/AkkaClusterTypedSerializerSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/AkkaClusterTypedSerializerSpec.scala index 120cc0689e..fb18408270 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/AkkaClusterTypedSerializerSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/AkkaClusterTypedSerializerSpec.scala @@ -6,12 +6,12 @@ package akka.cluster.typed.internal import akka.actor.ExtendedActorSystem import akka.actor.typed.scaladsl.adapter._ -import akka.actor.typed.{ Behavior, TypedAkkaSpecWithShutdown } +import akka.actor.typed.Behavior import akka.cluster.typed.internal.receptionist.ClusterReceptionist import akka.serialization.SerializationExtension -import akka.actor.testkit.typed.scaladsl.ActorTestKit +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec -class AkkaClusterTypedSerializerSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class AkkaClusterTypedSerializerSpec extends ActorTestKitWordSpec { val ref = spawn(Behavior.empty[String]) val untypedSystem = system.toUntyped diff --git a/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionistSpec.scala b/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionistSpec.scala index 19e817faf1..37c67a23dd 100644 --- a/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionistSpec.scala +++ b/akka-cluster-typed/src/test/scala/akka/cluster/typed/internal/receptionist/ClusterReceptionistSpec.scala @@ -15,13 +15,14 @@ import akka.cluster.MemberStatus import akka.cluster.typed.{ Cluster, Join } import akka.serialization.SerializerWithStringManifest import akka.actor.testkit.typed.FishingOutcome -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } -import com.typesafe.config.{ Config, ConfigFactory } +import akka.actor.testkit.typed.scaladsl.TestProbe +import com.typesafe.config.ConfigFactory import org.scalatest.{ Matchers, WordSpec } - import scala.concurrent.Await import scala.concurrent.duration._ +import akka.actor.testkit.typed.scaladsl.ActorTestKit + object ClusterReceptionistSpec { val config = ConfigFactory.parseString( s""" @@ -96,15 +97,9 @@ class ClusterReceptionistSpec extends WordSpec with Matchers { "The cluster receptionist" must { "eventually replicate registrations to the other side" in { - val testKit1 = new ActorTestKit { - override def name = super.name + "-test-1" - override def config = ClusterReceptionistSpec.config - } + val testKit1 = ActorTestKit("ClusterReceptionistSpec-test-1", ClusterReceptionistSpec.config) val system1 = testKit1.system - val testKit2 = new ActorTestKit { - override def name = system1.name - override def config = testKit1.system.settings.config - } + val testKit2 = ActorTestKit(system1.name, system1.settings.config) val system2 = testKit2.system try { val clusterNode1 = Cluster(system1) @@ -137,15 +132,9 @@ class ClusterReceptionistSpec extends WordSpec with Matchers { } "remove registrations when node dies" in { - val testKit1 = new ActorTestKit { - override def name = super.name + "-test-2" - override def config = ClusterReceptionistSpec.config - } + val testKit1 = ActorTestKit("ClusterReceptionistSpec-test-2", ClusterReceptionistSpec.config) val system1 = testKit1.system - val testKit2 = new ActorTestKit { - override def name = system1.name - override def config = testKit1.system.settings.config - } + val testKit2 = ActorTestKit(system1.name, system1.settings.config) val system2 = testKit2.system try { @@ -181,15 +170,9 @@ class ClusterReceptionistSpec extends WordSpec with Matchers { } "work with services registered before node joins cluster" in { - val testKit1 = new ActorTestKit { - override def name = super.name + "-test-2" - override def config = ClusterReceptionistSpec.config - } + val testKit1 = ActorTestKit("ClusterReceptionistSpec-test-3", ClusterReceptionistSpec.config) val system1 = testKit1.system - val testKit2 = new ActorTestKit { - override def name = system1.name - override def config = testKit1.system.settings.config - } + val testKit2 = ActorTestKit(system1.name, system1.settings.config) val system2 = testKit2.system try { @@ -229,15 +212,9 @@ class ClusterReceptionistSpec extends WordSpec with Matchers { } "handle a new incarnation of the same node well" in { - val testKit1 = new ActorTestKit { - override def name = super.name + "-test-3" - override def config = ClusterReceptionistSpec.config - } + val testKit1 = ActorTestKit("ClusterReceptionistSpec-test-4", ClusterReceptionistSpec.config) val system1 = testKit1.system - val testKit2 = new ActorTestKit { - override def name = system1.name - override def config = testKit1.system.settings.config - } + val testKit2 = ActorTestKit(system1.name, system1.settings.config) val system2 = testKit2.system try { @@ -271,10 +248,8 @@ class ClusterReceptionistSpec extends WordSpec with Matchers { system1.log.debug("Terminating system2, uid: [{}]", clusterNode2.selfMember.uniqueAddress.longUid) Await.ready(system2.terminate(), 10.seconds) - val testKit3 = new ActorTestKit { - override protected def name: String = system1.name - override def config: Config = testKit1.config - } + val testKit3 = ActorTestKit(system1.name, testKit1.config) + try { val system3 = testKit3.system system1.log.debug("Starting system3 at same hostname port as system2, uid: [{}]", Cluster(system3).selfMember.uniqueAddress.longUid) diff --git a/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/BasicClusterExampleSpec.scala b/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/BasicClusterExampleSpec.scala index a189b23d00..9bebde1a45 100644 --- a/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/BasicClusterExampleSpec.scala +++ b/akka-cluster-typed/src/test/scala/docs/akka/cluster/typed/BasicClusterExampleSpec.scala @@ -51,10 +51,12 @@ akka { ).withFallback(configSystem1) } -class BasicClusterConfigSpec extends TypedAkkaSpec { - +class BasicClusterConfigSpec extends WordSpec with ScalaFutures with Eventually with Matchers { import BasicClusterExampleSpec._ + implicit override val patienceConfig = + PatienceConfig(timeout = scaled(Span(10, Seconds)), interval = scaled(Span(100, Millis))) + "Cluster API" must { "init cluster" in { // config is pulled into docs, but we don't want to hardcode ports because that makes for brittle tests diff --git a/akka-docs/src/main/paradox/typed/testing.md b/akka-docs/src/main/paradox/typed/testing.md index f17d68564f..1568a1b03e 100644 --- a/akka-docs/src/main/paradox/typed/testing.md +++ b/akka-docs/src/main/paradox/typed/testing.md @@ -54,27 +54,27 @@ The following demonstrates how to test: The examples below require the following imports: Scala -: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #imports } +: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #imports } Java -: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #imports } +: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #imports } Each of the tests are testing an actor that based on the message executes a different effect to be tested: Scala -: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #under-test } +: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #under-test } Java -: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #under-test } +: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #under-test } For creating a child actor a noop actor is created: Scala -: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #child } +: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #child } Java -: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #child } +: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #child } All of the tests make use of the `BehaviorTestkit` to avoid the need for a real `ActorContext`. Some of the tests make use of the `TestInbox` which allows the creation of an `ActorRef` that can be used for synchronous testing, similar to the @@ -86,18 +86,18 @@ make use of the `TestInbox` which allows the creation of an `ActorRef` that can With a name: Scala -: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #test-child } +: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #test-child } Java -: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #test-child } +: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #test-child } Anonymously: Scala -: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #test-anonymous-child } +: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #test-anonymous-child } Java -: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #test-anonymous-child } +: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #test-anonymous-child } ### Sending messages @@ -105,27 +105,27 @@ For testing sending a message a `TestInbox` is created that provides an `ActorRe messages that have been sent to it. Scala -: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #test-message } +: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #test-message } Java -: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #test-message } +: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #test-message } Another use case is sending a message to a child actor you can do this by looking up the 'TestInbox' for a child actor from the 'BehaviorTestKit': Scala -: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #test-child-message } +: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #test-child-message } Java -: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #test-child-message } +: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #test-child-message } For anonymous children the actor names are generated in a deterministic way: Scala -: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #test-child-message-anonymous } +: @@snip [SyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala) { #test-child-message-anonymous } Java -: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #test-child-message-anonymous } +: @@snip [SyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/SyncTestingExampleTest.java) { #test-child-message-anonymous } ### Testing other effects @@ -153,10 +153,10 @@ the same in that a single procedure drives the test. Actor under test: Scala -: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala) { #under-test } +: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala) { #under-test } Java -: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java) { #under-test } +: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java) { #under-test } @scala[Tests extend `ActorTestKit`. This provides access to]@java[Tests create an instance of `ActorTestKit`. This provides access to] @@ -165,18 +165,18 @@ Java * A hook to shut down the ActorSystem from the test suite Scala -: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala) { #test-header } +: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala) { #test-header } Java -: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java) { #test-header } +: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java) { #test-header } -Your test is responsible for shutting down the `ActorSystem` e.g. using `BeforeAndAfterAll` when using ScalaTest +Your test is responsible for shutting down the `ActorSystem` e.g. using @scala[`BeforeAndAfterAll` when using ScalaTest]@java[`@AfterClass` when using JUnit]. Scala -: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala) { #test-shutdown } +: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala) { #test-shutdown } Java -: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java) { #test-shutdown } +: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java) { #test-shutdown } The following demonstrates: @@ -185,18 +185,21 @@ The following demonstrates: * Verifying that the actor under test responds via the `TestProbe` Scala -: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala) { #test-spawn } +: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala) { #test-spawn } Java -: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java) { #test-spawn } +: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java) { #test-spawn } Actors can also be spawned anonymously: Scala -: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala) { #test-spawn-anonymous } +: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala) { #test-spawn-anonymous } Java -: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java) { #test-spawn-anonymous } +: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java) { #test-spawn-anonymous } + +Note that you can add `import testKit._` to get access to the `spawn` and `createTestProbe` methods at the top level +without prefixing them with `testKit`. ### Test framework integration @@ -212,16 +215,19 @@ a dependency on JUnit to use this. @@@ div { .group-scala } -It often makes sense to introduce a common base class for all tests using the async test kit, here is an example how to -hook it into a ScalaTest test suite. +If you are using ScalaTest you can extend `akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec` to +have the async test kit automatically shutdown when the test is complete. + +Note that the dependency on ScalaTest is marked as optional from the test kit module, so your project must explicitly include +a dependency on ScalaTest to use this. @@@ Scala -: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/AbstractActorSpec.scala) { #scalatest-glue } +: @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/ScalaTestIntegrationExampleSpec.scala) { #scalatest-integration } Java -: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/JunitIntegrationExampleTest.java) { #junit-integration } +: @@snip [AsyncTestingExampleTest.java](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/JunitIntegrationExampleTest.java) { #junit-integration } ### Controlling the scheduler @@ -232,7 +238,7 @@ Making such tests more reliable by using generous timeouts make the tests take a For such situations, we provide a scheduler where you can manually, explicitly advance the clock. Scala -: @@snip [ManualTimerExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/ManualTimerExampleSpec.scala) { #manual-scheduling-simple } +: @@snip [ManualTimerExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/ManualTimerExampleSpec.scala) { #manual-scheduling-simple } Java -: @@snip [ManualTimerExampleTest.scala](/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/ManualTimerExampleTest.java) { #manual-scheduling-simple } +: @@snip [ManualTimerExampleTest.scala](/akka-actor-testkit-typed/src/test/java/jdocs/akka/actor/testkit/typed/javadsl/ManualTimerExampleTest.java) { #manual-scheduling-simple } diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/ManyRecoveriesSpec.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/ManyRecoveriesSpec.scala index e46e76d05f..be3541fc9c 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/ManyRecoveriesSpec.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/ManyRecoveriesSpec.scala @@ -4,19 +4,17 @@ package akka.persistence.typed -import akka.actor.typed.TypedAkkaSpecWithShutdown import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.adapter.TypedActorSystemOps import akka.persistence.typed.scaladsl.PersistentBehaviors.CommandHandler import akka.persistence.typed.scaladsl.{ Effect, PersistentBehavior, PersistentBehaviors } import akka.testkit.TestLatch -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } -import com.typesafe.config.{ Config, ConfigFactory } -import org.scalatest.concurrent.Eventually - +import akka.actor.testkit.typed.scaladsl.TestProbe import scala.concurrent.Await import scala.concurrent.duration._ +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + object ManyRecoveriesSpec { sealed case class Cmd(s: String) @@ -49,12 +47,7 @@ object ManyRecoveriesSpec { (1 to n).map(mapper).toSet } -class ManyRecoveriesSpec extends ActorTestKit with TypedAkkaSpecWithShutdown with Eventually { - - import ManyRecoveriesSpec._ - - override def config: Config = ConfigFactory.parseString( - s""" +class ManyRecoveriesSpec extends ActorTestKitWordSpec(s""" akka.actor.default-dispatcher { type = Dispatcher executor = "thread-pool-executor" @@ -65,7 +58,9 @@ class ManyRecoveriesSpec extends ActorTestKit with TypedAkkaSpecWithShutdown wit akka.persistence.max-concurrent-recoveries = 3 akka.persistence.journal.plugin = "akka.persistence.journal.inmem" akka.actor.warn-about-java-serializer-usage = off - """) + """) { + + import ManyRecoveriesSpec._ "Many persistent actors" must { "be able to recover without overloading" in { diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/EventsourcedStashReferenceManagementTest.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/EventsourcedStashReferenceManagementTest.scala index 88a183d5fb..eb2f92d773 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/EventsourcedStashReferenceManagementTest.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/EventsourcedStashReferenceManagementTest.scala @@ -5,15 +5,14 @@ package akka.persistence.typed.internal import akka.actor.typed.scaladsl.Behaviors -import akka.actor.typed.{ Behavior, Signal, TypedAkkaSpecWithShutdown } +import akka.actor.typed.{ Behavior, Signal } import akka.persistence.typed.internal.EventsourcedBehavior.InternalProtocol import akka.persistence.typed.internal.EventsourcedBehavior.InternalProtocol.{ IncomingCommand, RecoveryPermitGranted } -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } -import scala.concurrent.duration.{ FiniteDuration, _ } +import akka.actor.testkit.typed.scaladsl.TestProbe +import scala.concurrent.duration._ +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec -import com.typesafe.config.ConfigFactory - -class EventsourcedStashReferenceManagementTest extends ActorTestKit with TypedAkkaSpecWithShutdown { +class EventsourcedStashReferenceManagementTest extends ActorTestKitWordSpec { case class Impl() extends EventsourcedStashReferenceManagement diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/RecoveryPermitterSpec.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/RecoveryPermitterSpec.scala index 93e65a9beb..9a08159d09 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/RecoveryPermitterSpec.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/internal/RecoveryPermitterSpec.scala @@ -5,21 +5,20 @@ package akka.persistence.typed.internal import akka.actor.PoisonPill -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } +import akka.actor.testkit.typed.scaladsl.TestProbe import akka.actor.typed.scaladsl.Behaviors import akka.actor.typed.scaladsl.adapter.{ TypedActorRefOps, TypedActorSystemOps } -import akka.actor.typed.{ ActorRef, Behavior, TypedAkkaSpecWithShutdown } +import akka.actor.typed.{ ActorRef, Behavior } import akka.persistence.Persistence import akka.persistence.RecoveryPermitter.{ RecoveryPermitGranted, RequestRecoveryPermit, ReturnRecoveryPermit } import akka.persistence.typed.scaladsl.PersistentBehaviors.CommandHandler import akka.persistence.typed.scaladsl.{ Effect, PersistentBehaviors } import akka.testkit.EventFilter -import com.typesafe.config.{ Config, ConfigFactory } -import org.scalatest.concurrent.Eventually - import scala.concurrent.duration._ import scala.util.control.NoStackTrace +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + object RecoveryPermitterSpec { class TE extends RuntimeException("Boom!") with NoStackTrace @@ -63,17 +62,15 @@ object RecoveryPermitterSpec { } } -class RecoveryPermitterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown with Eventually { +class RecoveryPermitterSpec extends ActorTestKitWordSpec(s""" + akka.persistence.max-concurrent-recoveries = 3 + akka.persistence.journal.plugin = "akka.persistence.journal.inmem" + akka.actor.warn-about-java-serializer-usage = off + akka.loggers = ["akka.testkit.TestEventListener"] + """) { import RecoveryPermitterSpec._ - override def config: Config = ConfigFactory.parseString( - s""" - akka.persistence.max-concurrent-recoveries = 3 - akka.persistence.journal.plugin = "akka.persistence.journal.inmem" - akka.actor.warn-about-java-serializer-usage = off - akka.loggers = ["akka.testkit.TestEventListener"] - """) implicit val untypedSystem = system.toUntyped private val permitter = Persistence(untypedSystem).recoveryPermitter @@ -83,11 +80,11 @@ class RecoveryPermitterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown p.expectMessage(RecoveryPermitGranted) } - val p1 = TestProbe[Any]() - val p2 = TestProbe[Any]() - val p3 = TestProbe[Any]() - val p4 = TestProbe[Any]() - val p5 = TestProbe[Any]() + val p1 = createTestProbe[Any]() + val p2 = createTestProbe[Any]() + val p3 = createTestProbe[Any]() + val p4 = createTestProbe[Any]() + val p5 = createTestProbe[Any]() "RecoveryPermitter" must { "grant permits up to the limit" in { @@ -185,7 +182,7 @@ class RecoveryPermitterSpec extends ActorTestKit with TypedAkkaSpecWithShutdown requestPermit(p1) requestPermit(p2) - val stopProbe = TestProbe[ActorRef[Command]]() + val stopProbe = createTestProbe[ActorRef[Command]]() val parent = EventFilter.error(occurrences = 1, start = "Exception during recovery.").intercept { spawn( diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/OptionalSnapshotStoreSpec.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/OptionalSnapshotStoreSpec.scala index 42d07bb6b5..c662c11712 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/OptionalSnapshotStoreSpec.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/OptionalSnapshotStoreSpec.scala @@ -6,13 +6,11 @@ package akka.persistence.typed.scaladsl import java.util.UUID -import akka.actor.typed.TypedAkkaSpecWithShutdown +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec import akka.actor.typed.scaladsl.adapter.{ TypedActorRefOps, TypedActorSystemOps } import akka.event.Logging import akka.persistence.typed.scaladsl.PersistentBehaviors.CommandHandler -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } -import com.typesafe.config.{ Config, ConfigFactory } -import org.scalatest.concurrent.Eventually +import akka.actor.testkit.typed.scaladsl.TestProbe object OptionalSnapshotStoreSpec { @@ -43,12 +41,7 @@ object OptionalSnapshotStoreSpec { } -class OptionalSnapshotStoreSpec extends ActorTestKit with TypedAkkaSpecWithShutdown with Eventually { - - import OptionalSnapshotStoreSpec._ - - override def config: Config = ConfigFactory.parseString( - s""" +class OptionalSnapshotStoreSpec extends ActorTestKitWordSpec(s""" akka.persistence.publish-plugin-commands = on akka.persistence.journal.plugin = "akka.persistence.journal.inmem" akka.persistence.journal.leveldb.dir = "target/journal-${classOf[OptionalSnapshotStoreSpec].getName}" @@ -57,7 +50,9 @@ class OptionalSnapshotStoreSpec extends ActorTestKit with TypedAkkaSpecWithShutd # snapshot store plugin is NOT defined, things should still work akka.persistence.snapshot-store.local.dir = "target/snapshots-${classOf[OptionalSnapshotStoreSpec].getName}/" - """) + """) { + + import OptionalSnapshotStoreSpec._ private def logProbe[T](cl: Class[T]) = { val logProbe = TestProbe[Any] diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PerformanceSpec.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PerformanceSpec.scala index a100fa2465..f9375b1072 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PerformanceSpec.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PerformanceSpec.scala @@ -7,15 +7,15 @@ package akka.persistence.typed.scaladsl import java.util.UUID import akka.actor.typed.scaladsl.Behaviors -import akka.actor.typed.{ ActorRef, SupervisorStrategy, TypedAkkaSpecWithShutdown } +import akka.actor.typed.{ ActorRef, SupervisorStrategy } import akka.persistence.typed.scaladsl.PersistentBehaviors.CommandHandler import akka.actor.testkit.typed.TE -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, TestProbe } -import com.typesafe.config.{ Config, ConfigFactory } -import org.scalatest.concurrent.Eventually - +import akka.actor.testkit.typed.scaladsl.TestProbe +import com.typesafe.config.ConfigFactory import scala.concurrent.duration._ +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + object PerformanceSpec { val config = @@ -89,15 +89,7 @@ object PerformanceSpec { } } -class PerformanceSpec extends ActorTestKit with TypedAkkaSpecWithShutdown with Eventually { - - import PerformanceSpec._ - - val loadCycles = system.settings.config.getInt("akka.persistence.performance.cycles.load") - - override def config: Config = - ConfigFactory.parseString( - s""" +class PerformanceSpec extends ActorTestKitWordSpec(ConfigFactory.parseString(s""" akka.actor.serialize-creators = off akka.actor.serialize-messages = off akka.actor.warn-about-java-serializer-usage = off @@ -107,7 +99,11 @@ class PerformanceSpec extends ActorTestKit with TypedAkkaSpecWithShutdown with E akka.persistence.snapshot-store.plugin = "akka.persistence.snapshot-store.local" akka.persistence.snapshot-store.local.dir = "target/snapshots-PerformanceSpec/" akka.test.single-expect-default = 10s - """).withFallback(ConfigFactory.parseString(PerformanceSpec.config)) + """).withFallback(ConfigFactory.parseString(PerformanceSpec.config))) { + + import PerformanceSpec._ + + val loadCycles = system.settings.config.getInt("akka.persistence.performance.cycles.load") def stressPersistentActor(persistentActor: ActorRef[Command], probe: TestProbe[Command], failAt: Option[Long], description: String): Unit = { diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorFailureSpec.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorFailureSpec.scala index 76477a32ad..0cdd67157c 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorFailureSpec.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorFailureSpec.scala @@ -7,13 +7,12 @@ package akka.persistence.typed.scaladsl import akka.actor.testkit.typed.TestKitSettings import akka.actor.testkit.typed.scaladsl._ import akka.actor.typed.scaladsl.Behaviors -import akka.actor.typed.{ ActorRef, Behavior, SupervisorStrategy, TypedAkkaSpecWithShutdown } +import akka.actor.typed.{ ActorRef, Behavior, SupervisorStrategy } import akka.actor.testkit.typed.TE import akka.persistence.AtomicWrite import akka.persistence.journal.inmem.InmemJournal import akka.persistence.typed.EventRejectedException -import com.typesafe.config.{ Config, ConfigFactory } -import org.scalatest.concurrent.Eventually +import com.typesafe.config.ConfigFactory import scala.collection.immutable import scala.concurrent.Future @@ -62,12 +61,10 @@ object PersistentBehaviorFailureSpec { """).withFallback(ConfigFactory.load("reference.conf")).resolve() } -class PersistentBehaviorFailureSpec extends ActorTestKit with TypedAkkaSpecWithShutdown with Eventually { +class PersistentBehaviorFailureSpec extends ActorTestKitWordSpec(PersistentBehaviorFailureSpec.conf) { import PersistentBehaviorSpec._ - override lazy val config: Config = PersistentBehaviorFailureSpec.conf - implicit val testSettings = TestKitSettings(system) def failingPersistentActor(pid: String, probe: ActorRef[String]): Behavior[String] = PersistentBehaviors.receive[String, String, String]( diff --git a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala index 89960f3908..12e1f2bc3a 100644 --- a/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala +++ b/akka-persistence-typed/src/test/scala/akka/persistence/typed/scaladsl/PersistentBehaviorSpec.scala @@ -9,7 +9,7 @@ import java.util.concurrent.atomic.AtomicInteger import akka.Done import akka.actor.typed.scaladsl.Behaviors -import akka.actor.typed.{ ActorRef, ActorSystem, Behavior, SupervisorStrategy, Terminated, TypedAkkaSpecWithShutdown } +import akka.actor.typed.{ ActorRef, ActorSystem, Behavior, SupervisorStrategy, Terminated } import akka.persistence.query.{ EventEnvelope, PersistenceQuery, Sequence } import akka.persistence.query.journal.leveldb.scaladsl.LeveldbReadJournal import akka.persistence.snapshot.SnapshotStore @@ -20,8 +20,6 @@ import akka.stream.scaladsl.Sink import akka.actor.testkit.typed.TestKitSettings import akka.actor.testkit.typed.scaladsl._ import com.typesafe.config.{ Config, ConfigFactory } -import org.scalatest.concurrent.Eventually - import scala.concurrent.Future import scala.concurrent.Promise import scala.concurrent.duration._ @@ -213,12 +211,10 @@ object PersistentBehaviorSpec { } -class PersistentBehaviorSpec extends ActorTestKit with TypedAkkaSpecWithShutdown with Eventually { +class PersistentBehaviorSpec extends ActorTestKitWordSpec(PersistentBehaviorSpec.conf) { import PersistentBehaviorSpec._ - override lazy val config: Config = PersistentBehaviorSpec.conf - implicit val testSettings = TestKitSettings(system) import akka.actor.typed.scaladsl.adapter._ diff --git a/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorFlowSpec.scala b/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorFlowSpec.scala index 80b9dcdb7d..c95b2b2d6f 100644 --- a/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorFlowSpec.scala +++ b/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorFlowSpec.scala @@ -7,16 +7,15 @@ package akka.stream.typed.scaladsl //#imports import akka.stream.typed.scaladsl.ActorMaterializer import akka.stream.scaladsl._ - import akka.actor.typed.ActorRef import akka.actor.typed.scaladsl.Behaviors - import scala.concurrent.duration._ +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec + //#imports -import akka.actor.typed.{ DispatcherSelector, TypedAkkaSpecWithShutdown } +import akka.actor.typed.DispatcherSelector import akka.stream.testkit.TestSubscriber -import akka.actor.testkit.typed.scaladsl.ActorTestKit import scala.collection.immutable import scala.concurrent.{ Await, Future } @@ -26,7 +25,7 @@ object ActorFlowSpec { final case class Reply(s: String) } -class ActorFlowSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class ActorFlowSpec extends ActorTestKitWordSpec { import ActorFlowSpec._ implicit val mat = ActorMaterializer() diff --git a/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorSourceSinkSpec.scala b/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorSourceSinkSpec.scala index a54b6bf55a..7bd97ebdc2 100644 --- a/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorSourceSinkSpec.scala +++ b/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/ActorSourceSinkSpec.scala @@ -5,13 +5,12 @@ package akka.stream.typed.scaladsl import akka.actor.typed.ActorRef -import akka.actor.typed.TypedAkkaSpecWithShutdown import akka.actor.typed.scaladsl.Behaviors import akka.stream.OverflowStrategy import akka.stream.scaladsl.Keep import akka.stream.scaladsl.Sink import akka.stream.scaladsl.Source -import akka.actor.testkit.typed.scaladsl.{ ActorTestKit, _ } +import akka.actor.testkit.typed.scaladsl._ object ActorSourceSinkSpec { @@ -22,7 +21,7 @@ object ActorSourceSinkSpec { case object Failed extends AckProto } -class ActorSourceSinkSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class ActorSourceSinkSpec extends ActorTestKitWordSpec { import ActorSourceSinkSpec._ implicit val mat = ActorMaterializer() diff --git a/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/CustomGuardianAndMaterializerSpec.scala b/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/CustomGuardianAndMaterializerSpec.scala index 898531fa7c..ccfe9dbcd9 100644 --- a/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/CustomGuardianAndMaterializerSpec.scala +++ b/akka-stream-typed/src/test/scala/akka/stream/typed/scaladsl/CustomGuardianAndMaterializerSpec.scala @@ -5,12 +5,12 @@ package akka.stream.typed.scaladsl import scala.concurrent.Future + import akka.actor.typed.ActorRef -import akka.actor.typed.TypedAkkaSpecWithShutdown import akka.actor.typed.scaladsl.Behaviors import akka.stream.scaladsl.Sink import akka.stream.scaladsl.Source -import akka.actor.testkit.typed.scaladsl.ActorTestKit +import akka.actor.testkit.typed.scaladsl.ActorTestKitWordSpec object CustomGuardianAndMaterializerSpec { @@ -21,7 +21,7 @@ object CustomGuardianAndMaterializerSpec { case object Failed extends GuardianProtocol } -class CustomGuardianAndMaterializerSpec extends ActorTestKit with TypedAkkaSpecWithShutdown { +class CustomGuardianAndMaterializerSpec extends ActorTestKitWordSpec { import CustomGuardianAndMaterializerSpec._ val guardian = Behaviors.receive[GuardianProtocol] { diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 2e369bcde1..a9b56e1f1e 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -132,6 +132,9 @@ object Dependencies { val junit = Compile.junit % "optional;provided;test" + + val scalatest = Def.setting { "org.scalatest" %% "scalatest" % scalaTestVersion.value % "optional;provided;test" } // ApacheV2 + } } @@ -147,7 +150,7 @@ object Dependencies { val actorTests = l ++= Seq(Test.junit, Test.scalatest.value, Test.commonsCodec, Test.commonsMath, Test.mockito, Test.scalacheck.value, Test.jimfs) - val actorTestkitTyped = l ++= Seq(Provided.junit) + val actorTestkitTyped = l ++= Seq(Provided.junit, Provided.scalatest.value) val remote = l ++= Seq(netty, aeronDriver, aeronClient, Test.junit, Test.scalatest.value, Test.jimfs)