warn against using TestKitBase trait
This commit is contained in:
parent
df479a0bf0
commit
b840624b78
2 changed files with 59 additions and 36 deletions
|
|
@ -685,6 +685,11 @@ The ``implicit lazy val system`` must be declared exactly like that (you can
|
|||
course pass arguments to the actor system factory as needed) because trait
|
||||
:class:`TestKitBase` needs the system during its construction.
|
||||
|
||||
.. warning::
|
||||
|
||||
Use of the trait is discouraged because of potential issues with binary
|
||||
backwards compatibility in the future, use at own risk.
|
||||
|
||||
Specs2
|
||||
------
|
||||
|
||||
|
|
|
|||
|
|
@ -62,44 +62,22 @@ class TestActor(queue: BlockingDeque[TestActor.Message]) extends Actor {
|
|||
}
|
||||
|
||||
/**
|
||||
* Test kit for testing actors. Inheriting from this trait enables reception of
|
||||
* replies from actors, which are queued by an internal actor and can be
|
||||
* examined using the `expectMsg...` methods. Assertions and bounds concerning
|
||||
* timing are available in the form of `within` blocks.
|
||||
* Implementation trait behind the [[akka.testkit.TestKit]] class: you may use
|
||||
* this if inheriting from a concrete class is not possible.
|
||||
*
|
||||
* <pre>
|
||||
* class Test extends TestKit(ActorSystem()) {
|
||||
* try {
|
||||
*
|
||||
* val test = system.actorOf(Props[SomeActor]
|
||||
*
|
||||
* within (1 second) {
|
||||
* test ! SomeWork
|
||||
* expectMsg(Result1) // bounded to 1 second
|
||||
* expectMsg(Result2) // bounded to the remainder of the 1 second
|
||||
* }
|
||||
*
|
||||
* } finally {
|
||||
* system.shutdown()
|
||||
* }
|
||||
* <b>Use of the trait is discouraged because of potential issues with binary
|
||||
* backwards compatibility in the future, use at own risk.</b>
|
||||
*
|
||||
* This trait requires the concrete class mixing it in to provide an
|
||||
* [[akka.actor.ActorSystem]] which is available before this traits’s
|
||||
* constructor is run. The recommended way is this:
|
||||
*
|
||||
* {{{
|
||||
* class MyTest extends TestKitBase {
|
||||
* implicit lazy val system = ActorSystem() // may add arguments here
|
||||
* ...
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* Beware of two points:
|
||||
*
|
||||
* - the ActorSystem passed into the constructor needs to be shutdown,
|
||||
* otherwise thread pools and memory will be leaked
|
||||
* - this trait is not thread-safe (only one actor with one queue, one stack
|
||||
* of `within` blocks); it is expected that the code is executed from a
|
||||
* constructor as shown above, which makes this a non-issue, otherwise take
|
||||
* care not to run tests within a single test class instance in parallel.
|
||||
*
|
||||
* It should be noted that for CI servers and the like all maximum Durations
|
||||
* are scaled using their Duration.dilated method, which uses the
|
||||
* TestKitExtension.Settings.TestTimeFactor settable via akka.conf entry "akka.test.timefactor".
|
||||
*
|
||||
* @author Roland Kuhn
|
||||
* @since 1.1
|
||||
* }}}
|
||||
*/
|
||||
trait TestKitBase {
|
||||
|
||||
|
|
@ -579,6 +557,46 @@ trait TestKitBase {
|
|||
private def format(u: TimeUnit, d: Duration) = "%.3f %s".format(d.toUnit(u), u.toString.toLowerCase)
|
||||
}
|
||||
|
||||
/**
|
||||
* Test kit for testing actors. Inheriting from this trait enables reception of
|
||||
* replies from actors, which are queued by an internal actor and can be
|
||||
* examined using the `expectMsg...` methods. Assertions and bounds concerning
|
||||
* timing are available in the form of `within` blocks.
|
||||
*
|
||||
* <pre>
|
||||
* class Test extends TestKit(ActorSystem()) {
|
||||
* try {
|
||||
*
|
||||
* val test = system.actorOf(Props[SomeActor]
|
||||
*
|
||||
* within (1 second) {
|
||||
* test ! SomeWork
|
||||
* expectMsg(Result1) // bounded to 1 second
|
||||
* expectMsg(Result2) // bounded to the remainder of the 1 second
|
||||
* }
|
||||
*
|
||||
* } finally {
|
||||
* system.shutdown()
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* Beware of two points:
|
||||
*
|
||||
* - the ActorSystem passed into the constructor needs to be shutdown,
|
||||
* otherwise thread pools and memory will be leaked
|
||||
* - this trait is not thread-safe (only one actor with one queue, one stack
|
||||
* of `within` blocks); it is expected that the code is executed from a
|
||||
* constructor as shown above, which makes this a non-issue, otherwise take
|
||||
* care not to run tests within a single test class instance in parallel.
|
||||
*
|
||||
* It should be noted that for CI servers and the like all maximum Durations
|
||||
* are scaled using their Duration.dilated method, which uses the
|
||||
* TestKitExtension.Settings.TestTimeFactor settable via akka.conf entry "akka.test.timefactor".
|
||||
*
|
||||
* @author Roland Kuhn
|
||||
* @since 1.1
|
||||
*/
|
||||
class TestKit(_system: ActorSystem) extends { implicit val system = _system } with TestKitBase
|
||||
|
||||
object TestKit {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue