dreamhead / moco

Easy Setup Stub Server
MIT License
4.36k stars 1.08k forks source link

moco Server - Connection Reset issue. #143

Open kapoora opened 8 years ago

kapoora commented 8 years ago

I think my test cases are running before the mock server starts due to which it fails with 'Connection reset; nested exception is java.net.SocketException: Connection reset'

if i make the thread sleep for 1000/3000 ms in setup method it works fine. Looking for some method which can confirm the server started and acceptation connection.

Gradle

    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-core:1.3'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    testCompile 'com.github.dreamhead:moco-core:0.10.2'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.apache.httpcomponents:fluent-hc:4.5.2'

Test Case

   @Before
    public void setUp() throws Exception {
        HttpHeaders  headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpServer server = httpServer(9393);
        in = this.getClass().getResourceAsStream("foo.json");
        String response = IOUtils.toString(in, "UTF-8");
        server.post(and(by(uri("/test/123"))))
                .response(with(text(response)), header("Content-Type", "application/json"));
        runner = runner(server);
        runner.start();
         //Thread.sleep(1000);
    }
dreamhead commented 8 years ago

Weird. If Moco server is not ready, client should receive Connection refused.

Could you please provide your complete test case?

kiwiwin commented 6 years ago

I have the same issue ...

kiwiwin commented 6 years ago

This may caused by the stale connection state of http client has a connection manager with a connection pool, like apache httpclient.

The moco server has already passed running(server, ...); part, then it will called to stop, to close all the channels and connections associated with it. But the connection pool is still wait to finish the connection. So before the client connection gracefully closed, the client send a request through that connection again, and caused the "connection refused" exception.

To avoid this issue, one thing can be checked if there is any possible that the 'client' has been reused