From ed9ece49c41d3faa8e47b91237d70d4edffa45b8 Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Mon, 15 Jan 2018 15:08:18 +0100 Subject: [PATCH] Use expectNoMessage in typed testkit (#24250) * Use expectNoMessage in typed testkit Interestingly most calls to expectNoMsg did not appear to expect the parameter to be dilated Might be nice to allow configuring a variant that uses a (configurable) default delay (other than the single-expect-default) * Add a probe.expectNoMessage with no parameters If you want to 'just wait a while' and not care too much about how long exactly, will be dilated. --- .../scala/akka/actor/typed/DeferredSpec.scala | 6 ++--- .../akka/actor/typed/SupervisionSpec.scala | 23 +++++++++---------- .../scala/akka/actor/typed/TimerSpec.scala | 13 +++++------ .../typed/scaladsl/ImmutablePartialSpec.scala | 2 +- .../akka/cluster/typed/ClusterApiSpec.scala | 2 +- .../src/main/resources/reference.conf | 6 ++++- .../akka/testkit/typed/TestKitSettings.scala | 1 + .../testkit/typed/scaladsl/TestProbe.scala | 9 +++++++- .../src/main/resources/reference.conf | 2 +- 9 files changed, 37 insertions(+), 27 deletions(-) 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 9cb0e8d9f2..95b5a9ec79 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 @@ -38,7 +38,7 @@ class DeferredSpec extends TestKit with TypedAkkaSpec { probe.ref ! Started target(probe.ref) } - probe.expectNoMsg(100.millis) // not yet + probe.expectNoMessage() // not yet spawn(behv) // it's supposed to be created immediately (not waiting for first message) probe.expectMsg(Started) @@ -98,7 +98,7 @@ class DeferredSpec extends TestKit with TypedAkkaSpec { }.widen[Command] { case m ⇒ m } - probe.expectNoMsg(100.millis) // not yet + probe.expectNoMessage() // not yet val ref = spawn(behv) // it's supposed to be created immediately (not waiting for first message) probe.expectMsg(Started) @@ -114,7 +114,7 @@ class DeferredSpec extends TestKit with TypedAkkaSpec { probe.ref ! Started target(probe.ref) }) - probe.expectNoMsg(100.millis) // not yet + probe.expectNoMessage() // not yet val ref = spawn(behv) // it's supposed to be created immediately (not waiting for first message) probe.expectMsg(Started) 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 11ce2d5c3b..0cf84bdb79 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 @@ -239,7 +239,6 @@ class SupervisionSpec extends TestKit("SupervisionSpec") with TypedAkkaSpecWithS import SupervisionSpec._ private val nameCounter = Iterator.from(0) private def nextName(prefix: String = "a"): String = s"$prefix-${nameCounter.next()}" - private val waitTime = 50.millis implicit val testSettings = TestKitSettings(system) @@ -304,7 +303,7 @@ class SupervisionSpec extends TestKit("SupervisionSpec") with TypedAkkaSpecWithS // TODO document this difference compared to classic actors, and that // children can be stopped if needed in PreRestart parentProbe.expectMsgType[State].children.keySet should contain(childName) - childProbe.expectNoMsg(waitTime) + childProbe.expectNoMessage() } "resume when handled exception" in { @@ -333,7 +332,7 @@ class SupervisionSpec extends TestKit("SupervisionSpec") with TypedAkkaSpecWithS // resume ref ! Throw(new Exc2) - probe.expectNoMsg(waitTime) + probe.expectNoMessage() ref ! GetState probe.expectMsg(State(1, Map.empty)) @@ -367,8 +366,8 @@ class SupervisionSpec extends TestKit("SupervisionSpec") with TypedAkkaSpecWithS probe.expectMsg(GotSignal(PreRestart)) ref ! Ping // dropped due to backoff - startedProbe.expectNoMsg(minBackoff - 100.millis) - probe.expectNoMsg(minBackoff + 100.millis) + startedProbe.expectNoMessage(minBackoff - 100.millis) + probe.expectNoMessage(minBackoff + 100.millis) startedProbe.expectMsg(Started) ref ! GetState probe.expectMsg(State(0, Map.empty)) @@ -379,8 +378,8 @@ class SupervisionSpec extends TestKit("SupervisionSpec") with TypedAkkaSpecWithS probe.expectMsg(GotSignal(PreRestart)) ref ! Ping // dropped due to backoff - startedProbe.expectNoMsg((minBackoff * 2) - 100.millis) - probe.expectNoMsg((minBackoff * 2) + 100.millis) + startedProbe.expectNoMessage((minBackoff * 2) - 100.millis) + probe.expectNoMessage((minBackoff * 2) + 100.millis) startedProbe.expectMsg(Started) ref ! GetState probe.expectMsg(State(0, Map.empty)) @@ -399,19 +398,19 @@ class SupervisionSpec extends TestKit("SupervisionSpec") with TypedAkkaSpecWithS probe.expectMsg(GotSignal(PreRestart)) ref ! Ping // dropped due to backoff - probe.expectNoMsg(minBackoff + 100.millis) + probe.expectNoMessage(minBackoff + 100.millis.dilated) ref ! GetState probe.expectMsg(State(0, Map.empty)) // one more time after the reset timeout - probe.expectNoMsg(strategy.resetBackoffAfter + 100.millis) + probe.expectNoMessage(strategy.resetBackoffAfter + 100.millis.dilated) ref ! IncrementState ref ! Throw(new Exc1) probe.expectMsg(GotSignal(PreRestart)) ref ! Ping // dropped due to backoff // backoff was reset, so restarted after the minBackoff - probe.expectNoMsg(minBackoff + 100.millis) + probe.expectNoMessage(minBackoff + 100.millis.dilated) ref ! GetState probe.expectMsg(State(0, Map.empty)) } @@ -422,7 +421,7 @@ class SupervisionSpec extends TestKit("SupervisionSpec") with TypedAkkaSpecWithS probe.ref ! Started targetBehavior(probe.ref) }).onFailure[Exception](SupervisorStrategy.restart) - probe.expectNoMsg(100.millis) // not yet + probe.expectNoMessage() // not yet spawn(behv) // it's supposed to be created immediately (not waiting for first message) probe.expectMsg(Started) @@ -435,7 +434,7 @@ class SupervisionSpec extends TestKit("SupervisionSpec") with TypedAkkaSpecWithS val ref = spawn(behv) probe.expectMsg(Started) ref ! Ping - probe.expectNoMsg(100.millis) + probe.expectNoMessage() } } } 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 33ff08f4e1..67f47583e5 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 @@ -38,7 +38,6 @@ class TimerSpec extends TestKit("TimerSpec") implicit val testSettings = TestKitSettings(system) val interval = 1.second - val dilatedInterval = interval.dilated def target(monitor: ActorRef[Event], timer: TimerScheduler[Command], bumpCount: Int): Behavior[Command] = { def bump(): Behavior[Command] = { @@ -88,7 +87,7 @@ class TimerSpec extends TestKit("TimerSpec") val ref = spawn(behv) probe.expectMsg(Tock(1)) - probe.expectNoMsg(100.millis) + probe.expectNoMessage() ref ! End probe.expectMsg(GotPostStop(false)) @@ -124,7 +123,7 @@ class TimerSpec extends TestKit("TimerSpec") val latch = new CountDownLatch(1) // next Tock(1) enqueued in mailboxed, but should be discarded because of new timer ref ! SlowThenBump(latch) - probe.expectNoMsg(interval + 100.millis) + probe.expectNoMessage(interval + 100.millis.dilated) latch.countDown() probe.expectMsg(Tock(2)) @@ -142,7 +141,7 @@ class TimerSpec extends TestKit("TimerSpec") val ref = spawn(behv) probe.expectMsg(Tock(1)) ref ! Cancel - probe.expectNoMsg(dilatedInterval + 100.millis) + probe.expectNoMessage(interval + 100.millis.dilated) ref ! End probe.expectMsg(GotPostStop(false)) @@ -162,10 +161,10 @@ class TimerSpec extends TestKit("TimerSpec") val latch = new CountDownLatch(1) // next Tock(1) is enqueued in mailbox, but should be discarded by new incarnation ref ! SlowThenThrow(latch, new Exc) - probe.expectNoMsg(interval + 100.millis) + probe.expectNoMessage(interval + 100.millis.dilated) latch.countDown() probe.expectMsg(GotPreRestart(false)) - probe.expectNoMsg(interval / 2) + probe.expectNoMessage(interval / 2) probe.expectMsg(Tock(2)) ref ! End @@ -189,7 +188,7 @@ class TimerSpec extends TestKit("TimerSpec") val latch = new CountDownLatch(1) // next Tock(2) is enqueued in mailbox, but should be discarded by new incarnation ref ! SlowThenThrow(latch, new Exc) - probe.expectNoMsg(interval + 100.millis) + probe.expectNoMessage(interval + 100.millis.dilated) latch.countDown() probe.expectMsg(GotPreRestart(false)) probe.expectMsg(Tock(1)) diff --git a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ImmutablePartialSpec.scala b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ImmutablePartialSpec.scala index 44caebf835..7b452fe3d5 100644 --- a/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ImmutablePartialSpec.scala +++ b/akka-actor-typed-tests/src/test/scala/akka/actor/typed/scaladsl/ImmutablePartialSpec.scala @@ -25,7 +25,7 @@ class ImmutablePartialSpec extends TestKit with TypedAkkaSpecWithShutdown { testkit.run(Command1) testkit.currentBehavior shouldBe behavior - probe.expectNoMsg(100.milliseconds) + probe.expectNoMessage() testkit.run(Command2) testkit.currentBehavior shouldBe behavior 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 0f254bef57..9c27e9b918 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 @@ -99,7 +99,7 @@ class ClusterApiSpec extends TestKit("ClusterApiSpec", ClusterApiSpec.config) wi // subscribing to SelfUp when already removed yields nothing clusterNode2.subscriptions ! Subscribe(node2Probe.ref, classOf[SelfUp]) - node2Probe.expectNoMsg(100.millis) + node2Probe.expectNoMessage() } finally { Await.result(system2.terminate(), 3.seconds) diff --git a/akka-testkit-typed/src/main/resources/reference.conf b/akka-testkit-typed/src/main/resources/reference.conf index 173da9cc26..6fb4c43330 100644 --- a/akka-testkit-typed/src/main/resources/reference.conf +++ b/akka-testkit-typed/src/main/resources/reference.conf @@ -11,9 +11,13 @@ akka.actor.typed.test { timefactor = 1.0 # duration to wait in expectMsg and friends outside of within() block - # by default + # by default, will be dilated by the timefactor. single-expect-default = 3s + # duration to wait in expectNoMessage by default, + # will be dilated by the timefactor. + expect-no-message-default = 100ms + # The timeout that is added as an implicit by DefaultTimeout trait default-timeout = 5s diff --git a/akka-testkit-typed/src/main/scala/akka/testkit/typed/TestKitSettings.scala b/akka-testkit-typed/src/main/scala/akka/testkit/typed/TestKitSettings.scala index 831db2bceb..1928ea3b77 100644 --- a/akka-testkit-typed/src/main/scala/akka/testkit/typed/TestKitSettings.scala +++ b/akka-testkit-typed/src/main/scala/akka/testkit/typed/TestKitSettings.scala @@ -30,5 +30,6 @@ class TestKitSettings(val config: Config) { val TestTimeFactor = config.getDouble("akka.actor.typed.test.timefactor"). requiring(tf ⇒ !tf.isInfinite && tf > 0, "akka.actor.typed.test.timefactor must be positive finite double") val SingleExpectDefaultTimeout: FiniteDuration = config.getMillisDuration("akka.actor.typed.test.single-expect-default") + val ExpectNoMessageDefaultTimeout: FiniteDuration = config.getMillisDuration("akka.actor.typed.test.expect-no-message-default") val DefaultTimeout: Timeout = Timeout(config.getMillisDuration("akka.actor.typed.test.default-timeout")) } diff --git a/akka-testkit-typed/src/main/scala/akka/testkit/typed/scaladsl/TestProbe.scala b/akka-testkit-typed/src/main/scala/akka/testkit/typed/scaladsl/TestProbe.scala index a0d4645922..6dae757778 100644 --- a/akka-testkit-typed/src/main/scala/akka/testkit/typed/scaladsl/TestProbe.scala +++ b/akka-testkit-typed/src/main/scala/akka/testkit/typed/scaladsl/TestProbe.scala @@ -202,8 +202,15 @@ class TestProbe[M](name: String)(implicit system: ActorSystem[_]) { /** * Assert that no message is received for the specified time. + * Supplied value is not dilated. */ - def expectNoMsg(max: FiniteDuration) { expectNoMsg_internal(max.dilated) } + def expectNoMessage(max: FiniteDuration) { expectNoMsg_internal(max) } + + /** + * Assert that no message is received. Waits for the default period configured as `akka.actor.typed.test.expect-no-message-default` + * That value is dilated. + */ + def expectNoMessage() { expectNoMsg_internal(settings.ExpectNoMessageDefaultTimeout.dilated) } private def expectNoMsg_internal(max: FiniteDuration) { val o = receiveOne(max) diff --git a/akka-testkit/src/main/resources/reference.conf b/akka-testkit/src/main/resources/reference.conf index 7e8dfbe276..f3d54a25ed 100644 --- a/akka-testkit/src/main/resources/reference.conf +++ b/akka-testkit/src/main/resources/reference.conf @@ -16,7 +16,7 @@ akka { filter-leeway = 3s # duration to wait in expectMsg and friends outside of within() block - # by default + # by default, will be dilated by the timefactor. single-expect-default = 3s # The timeout that is added as an implicit by DefaultTimeout trait