测试参与者:恰好收到 N 条消息
Test actors: receive exactly N messages
我正在使用 Akka actor 测试应用程序。我正在使用测试探针。我想验证探测器是否收到了 10 条相同类型的消息。一种解决方案可能是写 10 次:
probe.expectMsg(20 seconds, Done)
但我更喜欢更通用的解决方案(例如,我也会针对 n=100 测试我的应用程序)。
我也试过 probe.receiveN(10, 20 seconds) 但如果我收到超过 10 个元素,测试不会失败。相反,我希望它在这种情况下失败。有解决办法吗?
expectMsgAllOf[T](d: Duration, obj: T*): Seq[T]
A number of objects matching the size of the supplied object array
must be received within the given time, and for each of the given
objects there must exist at least one among the received ones which
equals (compared with ==) it. The full sequence of received objects is
returned.
然后使用 expectNoMsg(d: Duration)
来确保所需要的数量是正确的。
使用您的代码:
val numberOfMessages = 10
probe.expectMsgAllOf(20 seconds, Array.fill(numberOfMessages)(Done))
probe.expectNoMsg(20 seconds)
我正在使用 Akka actor 测试应用程序。我正在使用测试探针。我想验证探测器是否收到了 10 条相同类型的消息。一种解决方案可能是写 10 次:
probe.expectMsg(20 seconds, Done)
但我更喜欢更通用的解决方案(例如,我也会针对 n=100 测试我的应用程序)。 我也试过 probe.receiveN(10, 20 seconds) 但如果我收到超过 10 个元素,测试不会失败。相反,我希望它在这种情况下失败。有解决办法吗?
expectMsgAllOf[T](d: Duration, obj: T*): Seq[T]
A number of objects matching the size of the supplied object array must be received within the given time, and for each of the given objects there must exist at least one among the received ones which equals (compared with ==) it. The full sequence of received objects is returned.
然后使用 expectNoMsg(d: Duration)
来确保所需要的数量是正确的。
使用您的代码:
val numberOfMessages = 10
probe.expectMsgAllOf(20 seconds, Array.fill(numberOfMessages)(Done))
probe.expectNoMsg(20 seconds)