简体   繁体   English

重新启动Spark服务器后,为什么出现“拒绝连接”错误?

[英]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.

相关问题 为什么我从 Java 中的 Mock Server 得到 Assert Not Equal? - Why am i getting Assert Not Equal from the Mock Server in Java? 为什么应该有匹配的 JUnit 时出现断言错误? - Why am I getting an assertion error when there should be a match JUnit? 为什么我的测试中出现NullPointerException? - Why am I getting NullPointerException in my tests? 为什么我在通过eclipse运行Selenium Grid时遇到错误? - Why am I getting an error while running Selenium Grid through eclipse? 为什么会出现以下错误“元素类型侦听器的内容必须匹配(侦听器)*”? - Why am I getting the following error “the content of element type listener must match (listener)*”? junit + spring-侦听器拒绝连接,并出现以下错误 - junit + spring - Listener refused the connection with the following error 尝试运行JUnit测试时,为什么会出现“在使用ClassLoader搜索持久性归档时抛出异常”错误? - Why am I getting an “An exception was thrown while searching for persistence archives with ClassLoader” error when I attempt to run a JUnit test? 返回模拟值时为什么会出现NullPointerException? - Why am I getting a NullPointerException when I return mocked values? 为什么我得到“无法加载ApplicationContext”Spring,jUnit和JavaConfig - Why am I getting “Failed to load ApplicationContext” Spring, jUnit with JavaConfig 当预期和实际外观相同时,为什么会出现AssertionError? - Why am I getting an AssertionError when the expected and actual look identical?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM