title: Sleep in Spock's Mock waits when run by CompletableFuture
body: <p>When I separately run the <code>runAsyncWithMock</code> test, it waits for 3 seconds until the mock's execution is finalised, rather than get terminated like the other 2 tests.</p>
  
  <p>I was not able to figure out why.</p>
  
  <p>It is interesting that:</p>
  
  <ol>
  <li>When multiple <code>Runnables</code> are executed by <code>CompletableFuture.runAsync</code> in a row in the <code>runAsyncWithMock</code> test, only the first one waits, the others not.</li>
  <li>When having multiple duplicated <code>runAsyncWithMock</code> tests, each and every of them runs for 3s when the whole specification is executed.</li>
  <li>When using Class instance rather than a Mock, the test is finalised immediately.</li>
  </ol>
  
  <p>Any idea what I got wrong?</p>
  
  <p>My configuration:</p>
  
  <ul>
  <li>macOS Mojave 10.14.6</li>
  <li>Spock 1.3-groovy-2.4</li>
  <li>Groovy 2.4.15</li>
  <li>JDK 1.8.0_201</li>
  </ul>
  
  <p>The repo containing the whole Gradle project for reproduction:</p>
  
  <p><a href="https://github.com/lobodpav/CompletableFutureMisbehavingTestInSpock" rel="nofollow noreferrer">https://github.com/lobodpav/CompletableFutureMisbehavingTestInSpock</a></p>
  
  <p>The problematic test's code:</p>
  
  <pre><code>@Stepwise
  class SpockCompletableFutureTest extends Specification {
      def runnable = Stub(Runnable) {
          run() &gt;&gt; {
              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
      }
  }
  </code></pre>