如何使用 akka.net testkit 期待一些消息并忽略其他消息?
How to expect some messages and ignore others with akka.net testkit?
我对一个 actor 进行了测试,该 actor 可能会响应一些意外消息,但最终它必须响应特定的已知消息。
所以本质上我想要一个断言,它会在某个时间跨度内忽略其他消息,但期待一个已知消息,就像这样:
[TestMethod]
[TestCategory("Integration")]
public async Task Should_fetch_fund_shareclass_and_details_from_test_service()
{
var testIsins = new HashSet<string> {"isin1", "isin2", "isin3"};
var props = Props.Create(() => new DataFetchSupervisor());
var actor = Sys.ActorOf(props, "fetchSupervisor");
Within(TimeSpan.FromSeconds(30), () =>
{
actor.Tell(new StartDataFetch(testIsins));
//ignore unexpected messages here
var fetchComplteMsg = ExpectMsg<DataFetchComplete>();
});
}
所以现在这将失败,因为我在 DataFetchComplete 消息之前收到了一些其他消息。
一如既往,在此先感谢您的帮助。
Akka.TestKit class has a number of different versions of ExcpectMsg
or equivalent checkers. The one you're looking for is probably a FishForMessage。它需要一个谓词,并会忽略所有传入的消息,只要它们无法通过谓词的条件。一旦找到传递的消息,此检查将完成,您的代码可能会继续。
您还可以使用 TestKit.IgnoreMessages 方法,该方法将接受一个委托函数,指定您在等待目标消息时希望忽略哪些类型的消息。
我对一个 actor 进行了测试,该 actor 可能会响应一些意外消息,但最终它必须响应特定的已知消息。 所以本质上我想要一个断言,它会在某个时间跨度内忽略其他消息,但期待一个已知消息,就像这样:
[TestMethod]
[TestCategory("Integration")]
public async Task Should_fetch_fund_shareclass_and_details_from_test_service()
{
var testIsins = new HashSet<string> {"isin1", "isin2", "isin3"};
var props = Props.Create(() => new DataFetchSupervisor());
var actor = Sys.ActorOf(props, "fetchSupervisor");
Within(TimeSpan.FromSeconds(30), () =>
{
actor.Tell(new StartDataFetch(testIsins));
//ignore unexpected messages here
var fetchComplteMsg = ExpectMsg<DataFetchComplete>();
});
}
所以现在这将失败,因为我在 DataFetchComplete 消息之前收到了一些其他消息。
一如既往,在此先感谢您的帮助。
Akka.TestKit class has a number of different versions of ExcpectMsg
or equivalent checkers. The one you're looking for is probably a FishForMessage。它需要一个谓词,并会忽略所有传入的消息,只要它们无法通过谓词的条件。一旦找到传递的消息,此检查将完成,您的代码可能会继续。
您还可以使用 TestKit.IgnoreMessages 方法,该方法将接受一个委托函数,指定您在等待目标消息时希望忽略哪些类型的消息。