[英]Why am I getting “connection refused” error after restart Spark server?
I have this test classes: 我有这个测试班:
class PostIT {
companion object {
@BeforeClass
@JvmStatic
fun initialise() {
baseURI = "http://localhost:4567"
Server.start()
}
@AfterClass
@JvmStatic
fun tearDown() {
Server.stop()
}
}
//some test cases
}
class UserIT {
companion object {
@BeforeClass
@JvmStatic
fun initialise() {
baseURI = "http://localhost:4567"
Server.start()
}
@AfterClass
@JvmStatic
fun tearDown() {
Server.stop()
}
}
//some test cases
}
and Server
object: 和
Server
对象:
object Server {
fun start() {
Spark.init()
prepareRoutes()
}
fun stop() {
Spark.stop()
}
private fun prepareRoutes() {
get("/users", whatever)
//more routes
}
}
When I run both test classes separately, it works fine. 当我分别运行两个测试类时,它可以正常工作。 But, when I tell IDE to run both test classes, I'm getting
connection refused error
when second test class is run. 但是,当我告诉IDE运行两个测试类时,运行第二个测试类时
connection refused error
。
It seems like when server is stopped, it never starts again. 好像服务器停止时,它再也不会启动。 It's like
Spark.init()
is not working after server being stopped. 就像
Spark.init()
在服务器停止后无法正常工作。
I've also tried calling Spark.awaitInitialization()
after Spark.init()
. 我也尝试过在
Spark.awaitInitialization()
之后调用Spark.init()
。
What am I missing? 我想念什么?
Solved! 解决了! Actually, problem wasn't the server initialization after stopped.
实际上,问题不在于停止后的服务器初始化。 We must wait until server is stopped.
我们必须等到服务器停止。 I found the solution here .
我在这里找到了解决方案。
fun stop() {
try {
Spark.stop()
while (true) {
try {
Spark.port()
Thread.sleep(500)
} catch (ignored: IllegalStateException) {
break
}
}
} catch (ex: Exception) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.