I'm using Akka actor in our product. We wrote some code:
@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()...
}
Then when I wrote some test in junit
for these class
@Test public void testMethod(){
sendAddRequest(...);
sendDeteleRequest(...)
...
...
assertThat(singletonObj.get("someString")).isEqual(42)
}
Then this test is unreliable because when I do assertion, then all events was not handled yet.
How can I wait utils all events in actor system finish?
Import the below package and then you can await till you process all the events or the test will fail after timeout.
testCompile 'org.awaitility:awaitility:3.1.0'
import org.awaitility.Awaitility;
And before your assertThat use
await().pollInterval(5, TimeUnit.MILLISECONDS).until(() -> !isProcessed());
The isProcessed method will look something like below
protected Callable<Boolean> isProcessed() {
return () -> {
return singletonObj.getCacheCount()==2;
};
}
Regards,
Vinoth
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.