大佬教程收集整理的这篇文章主要介绍了Ktor 嵌入式服务器未在测试中启动,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的 AcceptanCETests 中,我尝试使用以下非常粗鲁的代码来启动 Ktor 嵌入式服务器,以便能够检查服务器是否已启动并且测试是否可以启动:
fun start() {
val configPath = ClassLoader.getSystemresource("application-acceptanCETest.conf").file
val appEnvironment = commandlineEnvironment(arrayOf("-config=$configPath"))
ENGIne = embeddedServer(CIO,appEnvironment)
ENGIne.addShutdownHook {
stop()
}
ENGIne.start()
val disposable = ENGIne.environment.monitor.subscribe(ApplicationStarted) {
started = true
}
while (!started) {
// the start method should not exit until server is started successfully
Thread.sleep(10)
}
disposable.dispose()
}
不幸的是,这在通过 gradle 运行测试时不起作用,但在 IntelliJ 中的单个测试运行期间确实起作用。任何提示表示赞赏。
我已经尝试添加一些日志语句,我确实看到 while 循环没有退出,因此我相信 ApplicationStarted 事件没有正确处理。
编辑:删除while循环时,测试使用gradle运行,但不在IntelliJ中运行:-(
所以,这完全取决于正确的顺序。它使用以下代码块:
fun start() {
val configPath = ClassLoader.getSystemresource("application-acceptanCETest.conf").file
val appEnvironment = commandLineEnvironment(arrayOf("-config=$configPath"))
ENGIne = embeddedServer(CIO,appEnvironment)
ENGIne.addShutdownHook {
stop()
}
val disposable = ENGIne.environment.monitor.subscribe(ApplicationStarted) {
started = true
}
ENGIne.start()
while (!started) {
// the start method should not exit until server is started successfully
Thread.sleep(10)
}
disposable.dispose()
}
我在这里学到的教训是在启动服务器之前先注册事件监听器:-)
以上是大佬教程为你收集整理的Ktor 嵌入式服务器未在测试中启动全部内容,希望文章能够帮你解决Ktor 嵌入式服务器未在测试中启动所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。