When I separately run the runAsyncWithMock test, it waits for 3 seconds until the mock's execution is finalised, rather than get terminated like the other 2 tests.
I was not able to figure out why.
It is interesting that:
- When multiple
Runnablesare executed byCompletableFuture.runAsyncin a row in therunAsyncWithMocktest, only the first one waits, the others not. - When having multiple duplicated
runAsyncWithMocktests, all of them run for 3s when the whole specification is executed. - When using Class instance rather than a Mock, the test is finalised immediately.
Any idea what I got wrong?
My configuration:
- Spock 1.3-groovy-2.4
- Groovy 2.4.15
The code:
class SpockCompletableFutureTest extends Specification {
def runnable = Stub(Runnable) {
run() >> {
println "${Date.newInstance()} BEGIN1 in thread ${Thread.currentThread()}"
sleep(3000)
println "${Date.newInstance()} END1 in thread ${Thread.currentThread()}"
}
}
def "runAsyncWithMock"() {
when:
CompletableFuture.runAsync(runnable)
then:
true
}
def "runAsyncWithMockAndClosure"() {
when:
CompletableFuture.runAsync({ runnable.run() })
then:
true
}
def "runAsyncWithClass"() {
when:
CompletableFuture.runAsync(new Runnable() {
void run() {
println "${Date.newInstance()} BEGIN2 in thread ${Thread.currentThread()}"
sleep(3000)
println "${Date.newInstance()} END2 in thread ${Thread.currentThread()}"
}
})
then:
true
}
}
Aucun commentaire:
Enregistrer un commentaire