代码之家  ›  专栏  ›  技术社区  ›  Troy Daniels

测试akka参与者时检查是否缺少日志消息

  •  0
  • Troy Daniels  · 技术社区  · 7 年前

    在测试Akka演员时 TestKit , https://doc.akka.io/docs/akka/2.5/testing.html 显示如何验证已记录给定消息。

    我已将我的参与者设置为在接收到未处理的消息时调用一个方法,该方法记录类似“接收到意外消息”的内容。在我的测试中,我想验证消息是否从未被记录,即使测试似乎成功了。有办法吗?

    我使用的是Akka 2.5和Java10。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Val    7 年前

    1) 创建TestKit探测器并使其订阅系统的eventStream

    yourActorSystemItemTest.eventStream().subscribe(yourProbe.getRef(),UnhandledMessage.class);

    然后在最后检查探测器接收到多少消息,在您的情况下为0。使用您可以使用的多种“expect…”方法之一。

    2) 文档告诉您如何检查日志消息,因此只需声明您收到“意外消息”的次数为0。

    祝你好运

        2
  •  1
  •   Troy Daniels    7 年前

    为了提供一些细节,我需要做以下几点:

    import akka.event.Logging;
    import akka.testkit.javadsl.EventFilter;
    import akka.testkit.javadsl.TestKit;
    ...
    
    @Test
    public void noUnhandledTest() {
    
      new TestKit(system) {{
        new EventFilter(Logging.Warning.class, system).
           occurrences(0).
           matches("unhandled event").
           intercept(() -> {
             try {
               <actual test code>
    
               // Intercept needs a supplier
               return 0;
             } catch (Exception e) {
               // Suppliers can't throw
               throw new RuntimeException(e);
             }
           });
      }};
    }
    

    src/test/resources/application.conf :

    akka.loggers = [akka.testkit.TestEventListener ]