title: Kotlin Coroutines with timeout
body: <p>I'm currently writing a test-function which should run a block or (when a certain timeout is reached) throws an exception.</p>
  
  <p>I was trying this with <code>Coroutines</code> in Kotlin but ended up with a mixture of <code>Coroutines</code> and <code>CompletableFuture</code>:</p>
  
  <pre><code>fun &lt;T&gt; runBlockWithTimeout(maxTimeout: Long, block: () -&gt; T ): T {
      val future = CompletableFuture&lt;T&gt;()
  
      // runs the coroutine
      launch { block() }
  
      return future.get(maxTimeout, TimeUnit.MILLISECONDS)
  }
  </code></pre>
  
  <p>This works, but I'm not sure if this is the intended way to solve that problem in kotlin.</p>
  
  <p>I also tried other approaches:</p>
  
  <pre><code>runBlocking {
      withTimeout(maxTimeout) {
          block()
      }
  }
  </code></pre>
  
  <p>But this seems not to work as soon as the <code>block</code> calls e.g. <code>Thread.sleep(...)</code></p>
  
  <p>So is the <code>CompletableFuture</code> approach the way to go or is there a better one?</p>
  
  <p><strong>update 1</strong>
  What I want to achieve: </p>
  
  <p>Async Integration-Test code (like receiving data from RabbitMq) should be tested somehow like this:</p>
  
  <pre><code>var rabbitResults: List = ... // are filled async via RabbitListeners
  ...
  waitMax(1000).toSucceed {
      assertThat(rabbitResults).hasSize(1)
  }
  waitMax(1000).toSucceed {
      assertThat(nextQueue).hasSize(3)
  }
  ...
  </code></pre>