【问题标题】:vertx-lang-kotlin-coroutines test failedvertx-lang-kotlin-coroutines 测试失败
【发布时间】:2020-05-29 07:19:04
【问题描述】:

我一直在写一个测试类:

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()
      }

    }
  }
}

如果Verticle的start()方法写成“普通”方式,则Test肯定通过:

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

不同的是,如果我使用 vertx-lang-kotlin-coroutines API 实现不同的解决方案,测试会抛出 java.util.concurrent.TimeoutException

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

我做错了什么?

【问题讨论】:

    标签: kotlin vert.x vertx-eventbus


    【解决方案1】:

    通道上的循环阻塞协程。在这种情况下,它会阻止您的verticle的开始。

    将您的 for loop 包装在 launch 块中:

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

    【讨论】:

    • 我同意这个答案,您必须退出 start() 方法才能真正启动 Verticle。您正在暂停该方法,因此它实际上并没有启动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 2022-01-11
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    相关资源
    最近更新 更多