我有下面的C#代码来测试AKKA.NET演员的行为。语句productActor.Tell(new ChangeActiveStatus(true));应该是一个阻塞调用(根据这个博客,TestKit使它成为一个同步调用),但我看到它会立即返回。结果,第二个测试失败了,尽管ActiveStatus稍后将被更改。
[TestMethod]
public void ProductActorUnitTests_CheckFor_ActiveStatusChanged()
{
var productActor = ActorOfAsTestActorRef<ProductActor>();
Assert.IsTrue(ProductActor.UnderlyingActor.ActiveStatus == false, "The initial ActiveStatus is expected to be FALSE.");
productActor.Tell(new ChangeActiveStatus(true));
Assert.IsTrue(productActor.UnderlyingActor.ActiveStatus == true, "The new ActiveStatus is expected to be TRUE.");
}*更新*
使用Thread.Sleep(10000)的下列代码成功:
[TestMethod]
public void ProductActorUnitTests_CheckFor_ActiveStatusChanged()
{
var productActor = ActorOfAsTestActorRef<ProductActor>();
Assert.IsTrue(ProductActor.UnderlyingActor.ActiveStatus == false, "The initial ActiveStatus is expected to be FALSE.");
productActor.Tell(new ChangeActiveStatus(true));
Thread.Sleep(10000);
Assert.IsTrue(productActor.UnderlyingActor.ActiveStatus == true, "The new ActiveStatus is expected to be TRUE.");
}发布于 2015-05-15 12:06:03
我看到的大多数AKKA.NET单元测试只使用两个参与者。显示示例的博客(在最初的问题中)只有两个参与者,因此它将工作,因为操作是同步的。如果系统中有多个参与者,例如Actor A消息Actor B,它将Actor C消息再次传递给Actor A,则消息传递将异步进行,因此必须等到所有操作完成。
https://stackoverflow.com/questions/30228024
复制相似问题