New implementation of expectNoMsg in TestKit #23224

This commit is contained in:
Kirill Yankov 2017-09-25 17:37:47 +03:00 committed by Johan Andrén
parent cd4ee59cc8
commit dfd94d3aea
3 changed files with 64 additions and 3 deletions

View file

@ -124,7 +124,9 @@ object TestPublisher {
/** /**
* Expect no messages. * Expect no messages.
* NOTE! Timeout value is automatically multiplied by timeFactor.
*/ */
@deprecated(message = "Use expectNoMessage instead", since = "2.5.5")
def expectNoMsg(): Self = executeAfterSubscription { def expectNoMsg(): Self = executeAfterSubscription {
probe.expectNoMsg() probe.expectNoMsg()
self self
@ -132,12 +134,22 @@ object TestPublisher {
/** /**
* Expect no messages for a given duration. * Expect no messages for a given duration.
* NOTE! Timeout value is automatically multiplied by timeFactor.
*/ */
@deprecated(message = "Use expectNoMessage instead", since = "2.5.5")
def expectNoMsg(max: FiniteDuration): Self = executeAfterSubscription { def expectNoMsg(max: FiniteDuration): Self = executeAfterSubscription {
probe.expectNoMsg(max) probe.expectNoMsg(max)
self self
} }
/**
* Expect no messages for a given duration.
*/
def expectNoMessage(max: FiniteDuration): Self = executeAfterSubscription {
probe.expectNoMessage(max)
self
}
/** /**
* Receive messages for a given duration or until one does not match a given partial function. * Receive messages for a given duration or until one does not match a given partial function.
*/ */
@ -557,7 +569,9 @@ object TestSubscriber {
* Fluent DSL * Fluent DSL
* *
* Same as `expectNoMsg(remaining)`, but correctly treating the timeFactor. * Same as `expectNoMsg(remaining)`, but correctly treating the timeFactor.
* NOTE! Timeout value is automatically multiplied by timeFactor.
*/ */
@deprecated(message = "Use expectNoMessage instead", since = "2.5.5")
def expectNoMsg(): Self = { def expectNoMsg(): Self = {
probe.expectNoMsg() probe.expectNoMsg()
self self
@ -567,12 +581,24 @@ object TestSubscriber {
* Fluent DSL * Fluent DSL
* *
* Assert that no message is received for the specified time. * Assert that no message is received for the specified time.
* NOTE! Timeout value is automatically multiplied by timeFactor.
*/ */
@deprecated(message = "Use expectNoMessage instead", since = "2.5.5")
def expectNoMsg(remaining: FiniteDuration): Self = { def expectNoMsg(remaining: FiniteDuration): Self = {
probe.expectNoMsg(remaining) probe.expectNoMsg(remaining)
self self
} }
/**
* Fluent DSL
*
* Assert that no message is received for the specified time.
*/
def expectNoMessage(remaining: FiniteDuration): Self = {
probe.expectNoMessage(remaining)
self
}
/** /**
* Expect a stream element and test it with partial function. * Expect a stream element and test it with partial function.
* *

View file

@ -0,0 +1,3 @@
# #23224 TestKit expectNoMsg fix
ProblemFilters.exclude[ReversedMissingMethodProblem]("akka.testkit.TestKitBase.expectNoMessage")
ProblemFilters.exclude[ReversedMissingMethodProblem]("akka.testkit.TestKitBase.expectNoMessage$default$1")

View file

@ -640,16 +640,48 @@ trait TestKitBase {
/** /**
* Same as `expectNoMsg(remainingOrDefault)`, but correctly treating the timeFactor. * Same as `expectNoMsg(remainingOrDefault)`, but correctly treating the timeFactor.
*/ */
@deprecated(message = "Use expectNoMessage instead", since = "2.5.5")
def expectNoMsg() { expectNoMsg_internal(remainingOrDefault) } def expectNoMsg() { expectNoMsg_internal(remainingOrDefault) }
/** /**
* Assert that no message is received for the specified time. * Assert that no message is received for the specified time.
* NOTE! Supplied value is always dilated.
*/ */
def expectNoMsg(max: FiniteDuration) { expectNoMsg_internal(max.dilated) } @deprecated(message = "Use expectNoMessage instead", since = "2.5.5")
def expectNoMsg(max: FiniteDuration) {
expectNoMsg_internal(max.dilated)
}
/**
* Assert that no message is received for the specified time.
* Supplied value is not dilated.
*/
def expectNoMessage(max: FiniteDuration) = {
expectNoMsg_internal(max)
}
private def expectNoMsg_internal(max: FiniteDuration) { private def expectNoMsg_internal(max: FiniteDuration) {
val o = receiveOne(max) val finish = System.nanoTime() + max.toNanos
assert(o eq null, s"received unexpected message $o") val pollInterval = 100.millis
def leftNow = (finish - System.nanoTime()).nanos
var elem: AnyRef = queue.peekFirst()
var left = leftNow
while (left.toNanos > 0 && elem == null) {
//Use of (left / 2) gives geometric series limited by finish time similar to (1/2)^n limited by 1,
//so it is very precise
Thread.sleep(
pollInterval.toMillis min (left / 2).toMillis
)
left = leftNow
if (left.toNanos > 0) {
elem = queue.peekFirst()
}
}
val diff = (max.toNanos - left.toNanos).nanos
val m = s"received unexpected message $elem after ${diff.toMillis} millis"
assert(elem eq null, m)
lastWasNoMsg = true lastWasNoMsg = true
} }