【问题标题】:How can await util all messages was processed by actor in unit test?如何在单元测试中等待所有消息都由参与者处理?
【发布时间】:2019-06-06 13:33:09
【问题描述】:

我在我们的产品中使用 Akka actor。我们写了一些代码:

   @Singleton
   public class SingletonObj {
      private Map<String, Integer> cached = new HashMap();
      public void set(String key, Integer value) {
         cached.put(key, value);
      }

      public void delete(String key){
         cached.delete(key}
      }
   }

   public class MyActor  extends AbstractActor implements InjectedActorSupport {
       @Inject SingletonObj singletonObj;

        public static Props props(Injector injector) {
        return Props.create(MyActor.class, injector);
    }

    public MyActor(Injector injector) {
        this.injector = injector;
        receive(ReceiveBuilder.create()
            .match(AddEvent.class, this::addEvent)
            .match(DeteteEvent.class, this::deleteEvent))
            .build());
        }
        private void addEvent(AddEvent addEvent) {singletonObj.set(addEvent.key, addEvent.value);}

        private void deteleEvent(DeteteEvent event){singletonObj.detele(event.key);}
   }

   public class Controller {

       private Injector injector;
       private ActorSystem actorSystem;

       public void handleAdd()...

       public void handleDelete()...


   }

然后当我在junit 中为这些类编写一些测试时

   @Test public void testMethod(){
       sendAddRequest(...);
       sendDeteleRequest(...)
       ...
       ...

       assertThat(singletonObj.get("someString")).isEqual(42)
    }

那么这个测试是不可靠的,因为当我做断言时,所有的事件都没有被处理。

如何等待actor系统中的所有事件完成?

【问题讨论】:

    标签: java playframework akka


    【解决方案1】:

    导入下面的包,然后你可以等待,直到你处理完所有事件,否则超时后测试将失败。

    testCompile 'org.awaitility:awaitility:3.1.0'
    
    import org.awaitility.Awaitility;
    

    在你的 assertThat 使用之前

    await().pollInterval(5, TimeUnit.MILLISECONDS).until(() -> !isProcessed());
    

    isProcessed 方法如下所示

    protected Callable<Boolean> isProcessed() {
            return () -> {
                return singletonObj.getCacheCount()==2;
            };
        }
    

    问候,

    维诺特

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 2015-03-22
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      相关资源
      最近更新 更多