mercredi 18 avril 2018

testing forkjoinpool with testng

I want to write some tests that checks my asynchronous calls to rest services using ForkJoinPool. I have the following code:

final ForkJoinTask<List<Something>> someJoinTask1 = new RecursiveTaskLambda<>(
                () -> service1.findAll(filter));
this.forkJoinPool.execute(someJoinTask);
final ForkJoinTask<List<Something>> someJoinTask2 = new RecursiveTaskLambda<>(
                () -> service2.findAll(filter));
this.forkJoinPool.execute(someJoinTask);
// some stuff happening here
try {
    firstList= someJoinTask1.get();
    secondList = someJoinTask2.get();
}
catch (final InterruptedException | ExecutionException e) {
     throw new MyException("Couldn't load mandatory data for screen", e);
}
getView().setSomething1(firstList);
getView().setSomething2(secondList);

In my test I have the following code:

//prepare some stuff for the test   
Mockito.when(this.service1.findAll(Matchers.any(MyFilter.class))).thenReturn(someStuff);                
// ----Call tested method----
someMethod();        
Mockito.verify(this.view).setSomething1(captor.capture());
Mockito.verify(this.view).setSomething2(captor.capture());
// ----Asserts----

The problem is that when i execute the test it runs forever. I tried to add Thread.sleep() just to see if that will change but it doesnt. When i try to debug the test it goes thru all the prepare code calls the tested method and when it reaches the firstList= someJoinTask1.get(); in someMethod(); it hangs.

This is my thread dump (I dont know if that is needed):

"main@1" prio=5 tid=0x1 nid=NA waiting
  java.lang.Thread.State: WAITING
      at java.lang.Object.wait(Object.java:-1)
      at java.util.concurrent.ForkJoinTask.externalInterruptibleAwaitDone(ForkJoinTask.java:367)
      at java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:1001)
      at someMethod(the first get line)

I am using TestNg and Mockito

Aucun commentaire:

Enregistrer un commentaire