{"action":"created","issue":{"url":"https://api.github.com/repos/jamesdbloom/mockserver/issues/509","repository_url":"https://api.github.com/repos/jamesdbloom/mockserver","labels_url":"https://api.github.com/repos/jamesdbloom/mockserver/issues/509/labels{/name}","comments_url":"https://api.github.com/repos/jamesdbloom/mockserver/issues/509/comments","events_url":"https://api.github.com/repos/jamesdbloom/mockserver/issues/509/events","html_url":"https://github.com/jamesdbloom/mockserver/issues/509","id":348918887,"node_id":"MDU6SXNzdWUzNDg5MTg4ODc=","number":509,"title":"NoHttpResponseException when multiple tests run sequentially ","user":{"login":"yigangzhang","id":2107221,"node_id":"MDQ6VXNlcjIxMDcyMjE=","avatar_url":"https://avatars3.githubusercontent.com/u/2107221?v=4","gravatar_id":"","url":"https://api.github.com/users/yigangzhang","html_url":"https://github.com/yigangzhang","followers_url":"https://api.github.com/users/yigangzhang/followers","following_url":"https://api.github.com/users/yigangzhang/following{/other_user}","gists_url":"https://api.github.com/users/yigangzhang/gists{/gist_id}","starred_url":"https://api.github.com/users/yigangzhang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yigangzhang/subscriptions","organizations_url":"https://api.github.com/users/yigangzhang/orgs","repos_url":"https://api.github.com/users/yigangzhang/repos","events_url":"https://api.github.com/users/yigangzhang/events{/privacy}","received_events_url":"https://api.github.com/users/yigangzhang/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/jamesdbloom/mockserver/milestones/8","html_url":"https://github.com/jamesdbloom/mockserver/milestone/8","labels_url":"https://api.github.com/repos/jamesdbloom/mockserver/milestones/8/labels","id":1070244,"node_id":"MDk6TWlsZXN0b25lMTA3MDI0NA==","number":8,"title":"5.5.0-RELEASE","description":"","creator":{"login":"jamesdbloom","id":733179,"node_id":"MDQ6VXNlcjczMzE3OQ==","avatar_url":"https://avatars1.githubusercontent.com/u/733179?v=4","gravatar_id":"","url":"https://api.github.com/users/jamesdbloom","html_url":"https://github.com/jamesdbloom","followers_url":"https://api.github.com/users/jamesdbloom/followers","following_url":"https://api.github.com/users/jamesdbloom/following{/other_user}","gists_url":"https://api.github.com/users/jamesdbloom/gists{/gist_id}","starred_url":"https://api.github.com/users/jamesdbloom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jamesdbloom/subscriptions","organizations_url":"https://api.github.com/users/jamesdbloom/orgs","repos_url":"https://api.github.com/users/jamesdbloom/repos","events_url":"https://api.github.com/users/jamesdbloom/events{/privacy}","received_events_url":"https://api.github.com/users/jamesdbloom/received_events","type":"User","site_admin":false},"open_issues":11,"closed_issues":19,"state":"open","created_at":"2015-04-16T07:35:22Z","updated_at":"2018-11-14T17:22:51Z","due_on":"2018-12-03T08:00:00Z","closed_at":null},"comments":1,"created_at":"2018-08-08T22:46:42Z","updated_at":"2018-11-14T17:22:51Z","closed_at":"2018-11-14T17:22:51Z","author_association":"NONE","body":"I got the similar problem as [issue 200](https://github.com/jamesdbloom/mockserver/issues/200). \r\n\r\nIt works fine if run each test individually but when 2 test cases running sequentially I got NoHttpResponseException.\r\n\r\n```\r\norg.apache.http.NoHttpResponseException: localhost:9992 failed to respond\r\n\tat org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:141)\r\n\tat org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)\r\n\tat org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)\r\n\tat org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)\r\n\tat org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:165)\r\n\tat org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)\r\n\tat org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)\r\n\tat org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)\r\n\tat org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)\r\n\tat org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)\r\n\tat org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)\r\n\tat org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)\r\n\tat org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)\r\n\tat org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)\r\n\tat com.mytest.mockserver.SampleTest.test2(SampleTest.java:90)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:498)\r\n\tat org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)\r\n\tat org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)\r\n\tat org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)\r\n\tat org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)\r\n\tat org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)\r\n\tat org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)\r\n\tat org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)\r\n\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)\r\n\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)\r\n\tat org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)\r\n\tat org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)\r\n\tat org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)\r\n\tat org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)\r\n\tat org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)\r\n\tat org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)\r\n\tat org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)\r\n\tat org.junit.runners.ParentRunner.run(ParentRunner.java:363)\r\n\tat org.junit.runner.JUnitCore.run(JUnitCore.java:137)\r\n\tat com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)\r\n\tat com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)\r\n\tat com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)\r\n\tat com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)\r\n```\r\n\r\nTest Code\r\n```java\r\nimport org.apache.http.client.methods.HttpPost;\r\nimport org.apache.http.impl.client.CloseableHttpClient;\r\nimport org.apache.http.impl.client.HttpClients;\r\nimport org.junit.*;\r\nimport org.junit.runners.MethodSorters;\r\nimport org.mockserver.client.server.MockServerClient;\r\nimport org.mockserver.integration.ClientAndServer;\r\n\r\nimport java.io.IOException;\r\n\r\nimport static org.mockserver.integration.ClientAndServer.startClientAndServer;\r\nimport static org.mockserver.model.HttpRequest.request;\r\nimport static org.mockserver.model.HttpResponse.response;\r\n\r\n@FixMethodOrder(MethodSorters.NAME_ASCENDING)\r\npublic class SampleTest {\r\n\r\n    final private String host = \"localhost\";\r\n    final private int serverPort = 9992;\r\n    final private String path = \"/somepath\";\r\n    final private String url = \"http://\" + host + \":\" + serverPort + path;\r\n\r\n    private ClientAndServer mockServer;\r\n    static private CloseableHttpClient client;\r\n\r\n    @BeforeClass\r\n    public static void start() {\r\n        client = HttpClients.createDefault();\r\n    }\r\n\r\n    @AfterClass\r\n    public static void close() throws IOException {\r\n        client.close();\r\n    }\r\n\r\n    @Before\r\n    public void startMockServer() {\r\n        mockServer = startClientAndServer(serverPort);\r\n    }\r\n\r\n    @After\r\n    public void stopMockServer() {\r\n        mockServer.stop();\r\n    }\r\n\r\n    @Test\r\n    public void test1() throws IOException {\r\n\r\n        new MockServerClient(host, serverPort)\r\n                .when(\r\n                        request()\r\n                                .withPath(path))\r\n                .respond(\r\n                        response()\r\n                                .withStatusCode(200));\r\n\r\n        HttpPost post = new HttpPost(url);\r\n        client.execute(post);\r\n    }\r\n\r\n    @Test\r\n    public void test2() throws IOException {\r\n\r\n        new MockServerClient(host, serverPort)\r\n                .when(\r\n                        request()\r\n                                .withPath(path))\r\n                .respond(\r\n                        response()\r\n                                .withStatusCode(200));\r\n\r\n\r\n        HttpPost post = new HttpPost(url);\r\n        client.execute(post);\r\n    }\r\n}\r\n```\r\n\r\nDependencies\r\n```xml\r\n    <dependencies>\r\n        <dependency>\r\n            <groupId>junit</groupId>\r\n            <artifactId>junit</artifactId>\r\n            <version>4.12</version>\r\n            <scope>test</scope>\r\n        </dependency>\r\n        <dependency>\r\n            <groupId>org.apache.httpcomponents</groupId>\r\n            <artifactId>httpclient</artifactId>\r\n            <version>4.5.5</version>\r\n        </dependency>\r\n        <dependency>\r\n            <groupId>org.mock-server</groupId>\r\n            <artifactId>mockserver-netty</artifactId>\r\n            <version>5.3.0</version>\r\n            <scope>test</scope>\r\n        </dependency>\r\n    </dependencies>\r\n```\r\n\r\nThe fix works for me is to wait for mockServer stop completely.\r\n```java\r\n    @After\r\n    public void stopMockServer() throws InterruptedException {\r\n        mockServer.stop();\r\n\r\n        // Wait for mockServer to stop fully\r\n        while (mockServer.isRunning()) {\r\n            Thread.sleep(1000L);\r\n        }\r\n    }\r\n```"},"comment":{"url":"https://api.github.com/repos/jamesdbloom/mockserver/issues/comments/438745547","html_url":"https://github.com/jamesdbloom/mockserver/issues/509#issuecomment-438745547","issue_url":"https://api.github.com/repos/jamesdbloom/mockserver/issues/509","id":438745547,"node_id":"MDEyOklzc3VlQ29tbWVudDQzODc0NTU0Nw==","user":{"login":"jamesdbloom","id":733179,"node_id":"MDQ6VXNlcjczMzE3OQ==","avatar_url":"https://avatars1.githubusercontent.com/u/733179?v=4","gravatar_id":"","url":"https://api.github.com/users/jamesdbloom","html_url":"https://github.com/jamesdbloom","followers_url":"https://api.github.com/users/jamesdbloom/followers","following_url":"https://api.github.com/users/jamesdbloom/following{/other_user}","gists_url":"https://api.github.com/users/jamesdbloom/gists{/gist_id}","starred_url":"https://api.github.com/users/jamesdbloom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jamesdbloom/subscriptions","organizations_url":"https://api.github.com/users/jamesdbloom/orgs","repos_url":"https://api.github.com/users/jamesdbloom/repos","events_url":"https://api.github.com/users/jamesdbloom/events{/privacy}","received_events_url":"https://api.github.com/users/jamesdbloom/received_events","type":"User","site_admin":false},"created_at":"2018-11-14T17:22:51Z","updated_at":"2018-11-14T17:22:51Z","author_association":"OWNER","body":"This issue is now fixed and will be in the next release 5.5.0.  It seems to be related to sockets staying open after Netty has reported they are closed and everything is shutdown.  I have fixed this issue by adding a 2 second delay after this which seems to work reliably when tested.  However I wouldn't recommend recreating a new instance of MockServer for each test because it is completely unnecessary and is much more expensive then just calling reset.  If you just call reset (or clear) before each test and only start and stop the MockServer at the start and end of the suite your tests will run faster and this issue will completely go away.  \r\nAlthough MockServer is very fast at starting up as an analogy if you were testing a web application you wouldn't typically redeploy the web application between each test method how ever fast it was because that is overkill and as your test number increase the time taken to deploy would be significant."}}
{"actor":{"display_login":"jamesdbloom"}}