Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subsequent tests fails in au.com.dius.pact:consumer for a project using ApacheHttpClient #1383

Open
mjureczko opened this issue Jun 15, 2021 · 3 comments

Comments

@mjureczko
Copy link

I created a sample with a failing test: https://github.com/mjureczko/pact-consumer-issue.

When running tests on the consumer side in a series where each test case requires the same provider endpoint, but in a different state, the second test fails without a response from the provider. It fails unless I give it some additional time (sleep for 1-2 seconds). I was tinkering with the debugger and the sleep is apparently required after executing server.stop(0) in the MockHttpServer.kt. So according to my observations, the mock server is restarted between test cases, and during the teardown, something is done asynchronously, unfortunately, not fast enough.

@uglyog
Copy link
Member

uglyog commented Jun 27, 2021

Thanks for the example project.

Please note that this is not a Pact-JVM issue, but an issue with HTTP clients caching connections with HTTP/1.1 and not taking the port into account (i.e. the connections are cached based on host name). The reason the sleep works because it causes the time to live value for the connection in connection pool to be exceeded and it will be evicted when the second test runs.

One way to fix it is to change:

public class MyFeignConfiguration {
    @Bean
    public feign.Client client() {
        CloseableHttpClient httpClient = HttpClientBuilder.create()
          .setConnectionReuseStrategy(new NoConnectionReuseStrategy())
          .build();
        return new ApacheHttpClient(httpClient);
    }
}

Another way is to enable using system properties on the HTTP client and then set the http.keepAlive property to false.

public class MyFeignConfiguration {
    @Bean
    public feign.Client client() {
        CloseableHttpClient httpClient = HttpClientBuilder.create()
          .useSystemProperties()
          .build();
        return new ApacheHttpClient(httpClient);
    }
}

and then in the project POM add:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
                <configuration>
                    <systemProperties>
                        <property>
                            <name>http.keepAlive</name>
                            <value>false</value>
                        </property>
                    </systemProperties>
                </configuration>
            </plugin>

@uglyog
Copy link
Member

uglyog commented Jun 27, 2021

Duplicate of #342

@uglyog uglyog marked this as a duplicate of #342 Jun 27, 2021
@uglyog
Copy link
Member

uglyog commented Jun 27, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants