kotlinvert.xvertx-eventbus

vertx-lang-kotlin-coroutines test failed


I have been writing a test class:

class TestVerticle {

  @BeforeEach
  fun deploy_verticle(vertx: Vertx, testContext: VertxTestContext) {
    vertx.deployVerticle(Verticle(), testContext.completing())
  }

  @Test
  fun test(vertx: Vertx, testContext: VertxTestContext) {
    testContext.verify {
      GlobalScope.launch(vertx.dispatcher()) {

        val reply = vertx.eventBus().requestAwait<Long>(AVIOEXTDMZAddr, "1")

        assert(reply.body() == 1010L)
        testContext.completeNow()
      }

    }
  }
}

If the method start() of Verticle is written in the "common" way, the Test is passed positively:

override suspend fun start() { 
   vertx.eventBus().consumer<String>(AVIOEXTDMZAddr){
         it.reply(1010L) 
      }
    }

Differently, if I implement a different solution, with the use of vertx-lang-kotlin-coroutines API, the test throws a java.util.concurrent.TimeoutException

override suspend fun start() { 
 val consumerChannel = vertx.eventBus().consumer<String>(AVIOEXTDMZAddr).toChannel(vertx)
    for (msg in consumerChannel) {
        msg.reply(1010L)
      }
    }

what am I doing wrong?


Solution

  • Loop on channel blocks the coroutine. In this case, it blocks start of your verticle.

    Wrap your for loop in launch block:

    async {
        for (msg in consumerChannel) {
            msg.reply(1010L)
          }
        }
    }