【问题标题】:Why am I getting "connection refused" error after restart Spark server?为什么重新启动 Spark 服务器后出现“连接被拒绝”错误?
【发布时间】:2018-08-15 22:15:39
【问题描述】:

我有这个测试类:

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

}

Server对象:

object Server {

    fun start() {
        Spark.init()
        prepareRoutes()
    }

    fun stop() {
        Spark.stop()
    }

    private fun prepareRoutes() {
        get("/users", whatever)
        //more routes
    }        

}

当我分别运行两个测试类时,它工作正常。但是,当我告诉 IDE 运行两个测试类时,我会在运行第二个测试类时得到connection refused error

似乎当服务器停止时,它再也不会启动了。就像Spark.init() 在服务器停止后不起作用。

我也尝试在Spark.init() 之后调用Spark.awaitInitialization()

我错过了什么?

【问题讨论】:

    标签: junit kotlin integration-testing spark-java


    【解决方案1】:

    解决了!实际上,问题不在于停止后的服务器初始化。我们必须等到服务器停止。我找到了解决方案here

    fun stop() {
        try {
            Spark.stop()
            while (true) {
                try {
                    Spark.port()
                    Thread.sleep(500)
                } catch (ignored: IllegalStateException) {
                    break
                }
    
            }
        } catch (ex: Exception) {
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 2017-04-28
      相关资源
      最近更新 更多