2

我在使用 Wiremock 的项目中有这个测试,它在 Jenkins 中正常工作(在构建期间,测试正在执行并通过),但它在我的本地机器上失败,并出现以下错误(Eclipse 控制台):

14:06:17,692  INFO [org.mortbay.log] - Logging to org.slf4j.impl.Log4jLoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog
14:06:17,751  INFO [org.mortbay.log] - jetty-6.1.26
14:06:17,767  WARN [org.mortbay.log] - failed DelayableSocketConnector@0.0.0.0:62344: java.net.BindException: Address already in use: JVM_Bind
14:06:17,767  WARN [org.mortbay.log] - failed Server@549b6220: java.net.BindException: Address already in use: JVM_Bind
14:06:17,775  INFO [org.mortbay.log] - jetty-6.1.26
14:06:17,776  WARN [org.mortbay.log] - failed DelayableSocketConnector@0.0.0.0:62344: java.net.BindException: Address already in use: JVM_Bind
14:06:17,776  WARN [org.mortbay.log] - failed Server@1c1474ea: java.net.BindException: Address already in use: JVM_Bind
14:06:17,777  INFO [org.mortbay.log] - jetty-6.1.26
14:06:17,778  WARN [org.mortbay.log] - failed DelayableSocketConnector@0.0.0.0:62344: java.net.BindException: Address already in use: JVM_Bind
14:06:17,778  WARN [org.mortbay.log] - failed Server@34c7e8a7: java.net.BindException: Address already in use: JVM_Bind

(马文)

Tests run: 3, Failures: 0, Errors: 3, Skipped: 0, Time elapsed: 0.408 sec <<< FAILURE!
Time elapsed: 0.377 sec  <<< ERROR!
java.lang.RuntimeException: java.net.BindException: Address already in use: JVM_Bind
    at com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:188)
    at com.github.tomakehurst.wiremock.junit.WireMockClassRule$1.evaluate(WireMockClassRule.java:75)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
    at com.sun.proxy.$Proxy0.invoke(Unknown Source)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)
Caused by: java.net.BindException: Address already in use: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
    at java.net.ServerSocket.bind(ServerSocket.java:328)
    at java.net.ServerSocket.<init>(ServerSocket.java:194)
    at org.mortbay.jetty.bio.SocketConnector.newServerSocket(SocketConnector.java:80)
    at org.mortbay.jetty.bio.SocketConnector.open(SocketConnector.java:73)
    at org.mortbay.jetty.AbstractConnector.doStart(AbstractConnector.java:283)
    at org.mortbay.jetty.bio.SocketConnector.doStart(SocketConnector.java:147)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.Server.doStart(Server.java:235)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:186)
    ... 21 more

我调试了代码以检查测试中的错误,但我什至无法到达那里,因为它在创建 WireMockClassRule 时失败(请参见下面的代码)。

private static int VVS_PORT_MOCK;
static {
    try {
        VVS_PORT_MOCK = (new ServerSocket(0)).getLocalPort();
    } catch (IOException e) {
        //
    }
}

@Rule
public static WireMockClassRule wireMockRule = new WireMockClassRule(VVS_PORT_MOCK);

问题似乎出在端口的动态分配中,如果我硬编码一个端口值:

 private static int VVS_PORT_MOCK = 8989; //using this it works

所有测试都顺利通过(但由于这是一个单元测试,我试图避免对其进行硬编码,因为我想确保测试在任何地方都能通过)。

(我用netstat检查了分配的端口是否之前被使用过,但那里没有问题)

我认为这与我缺少的一些本地配置有关,但此时我迷路了。

对可能发生的事情有任何想法吗?

另外,如果需要更多信息来解决此问题,请告诉我。

谢谢!

4

1 回答 1

4

当您运行方法“getLocalPort()”时,ServerSocket 将保留端口,并且当代码实例化 WireMockClassRule 时,端口已经绑定,导致您面临的异常。

您可以更改提供的代码块,如下所示:

ServerSocket ss = null;
try {
    ss = new ServerSocket(0);
    PORT = ss.getLocalPort();
} catch (IOException e) {
    LOG.error("Socket Exception while opening socket:", e);
} finally {
    try {
        ss.close();
    } catch (IOException e) {
        LOG.error("Socket Exception while closing socket:", e);
    }
}

让我知道它是否有效

于 2014-09-16T18:49:50.007 回答