Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final class ManualTime(delegate: akka.testkit.ExplicitlyTriggeredScheduler) {
@varargs
def expectNoMessageFor(duration: Duration, on: TestProbe[_]*): Unit = {
delegate.timePasses(duration.asScala)
on.foreach(_.expectNoMessage(Duration.ZERO))
on.foreach(_.expectNoMessage())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import akka.actor.typed.internal.adapter.SchedulerAdapter
import com.typesafe.config.{ Config, ConfigFactory }

import scala.annotation.varargs
import scala.concurrent.duration.{ Duration, FiniteDuration }
import scala.concurrent.duration.FiniteDuration

/**
* Manual time allows you to do async tests while controlling the scheduler of the system.
Expand Down Expand Up @@ -63,7 +63,7 @@ final class ManualTime(delegate: akka.testkit.ExplicitlyTriggeredScheduler) {
@varargs
def expectNoMessageFor(duration: FiniteDuration, on: TestProbe[_]*): Unit = {
delegate.timePasses(duration)
on.foreach(_.expectNoMessage(Duration.Zero))
on.foreach(_.expectNoMessage())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ class ManualTimerExampleSpec
}
}

"detect a message received during specified period" in {
case object Tick
case object Tock

val probe = TestProbe[Tock.type]()
val behavior = Behaviors.withTimers[Tick.type] { timer =>
timer.startSingleTimer(Tick, 10.millis)
Behaviors.receiveMessage { _ =>
probe.ref ! Tock
Behaviors.same
}
}

spawn(behavior)

assertThrows[AssertionError] {
manualTime.expectNoMessageFor(10.millis, probe)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tricky because we are dealing with two timelines, the manual timer time, which the duration is about, but then also actual time where the actual actor is running and interacting with the timer.

I'm not quite convinced this change is the right thing to do. @raboof wdyt?

Copy link
Contributor

@raboof raboof Nov 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test looks good.

It's tricky: ideally the change wouldn't be needed, but there's indeed a race between effects that are triggered by the timePasses but scheduled asynchronously (but immediately) so they can't be observed immediately.

In #24243 (comment) we discussed also using a custom/wrapped dispatcher to detect when there's at least no more work scheduled there. Until that time it's probably OK to 'fudge it a bit' and allow for some time to let the dispatchers clear.

The default timeout of expectNoMessage is rather long (3 seconds IIRC?), which rather goes against the idea of tests with a manually controlled clock. Could we make it shorter? And perhaps add a comment pointing to this issue and/or #24243 (comment) so we remember we might want a more elegant solution in the future?

}

"replace timer" in {
sealed trait Command
case class Tick(n: Int) extends Command
Expand Down