dropwizard version: 1.3.14
I have written integration test following the document
The test sometime works and randomly fails sometime with javax.ws.rs.ProcessingException: java.net.SocketTimeoutException: Read timed out
Below is my test class
public class Test {
@ClassRule
public static final DropwizardAppRule<MyConfigClass> RULE =
new DropwizardAppRule<MyConfigClass>(
MyAppcalss.class,
ResourceHelpers.resourceFilePath("config.yml"));
private final ObjectMapper mapper = new ObjectMapper();
private final Client client = new JerseyClientBuilder(RULE.getEnvironment())
.build(" REST client");
@Before
public void setup() {
}
@After
public void tearDown() {
client.close();
}
@Test
public void loginTest() {
ObjectNode userCredentials = mapper.createObjectNode();
userCredentials.put("userId", "[email protected]");
userCredentials.put("password", "test");
Response response = client.target(
String.format("http://localhost:%d/api/user/login", RULE.getLocalPort()))
.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(userCredentials.toString(), MediaType.APPLICATION_JSON_TYPE));
Assert.assertEquals(200, response.getStatus());
}
}
The stack trace
javax.ws.rs.ProcessingException: java.net.SocketTimeoutException: Read timed out
at io.dropwizard.client.DropwizardApacheConnector.apply(DropwizardApacheConnector.java:111)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:280)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at com.codahale.metrics.httpclient.InstrumentedHttpRequestExecutor.execute(InstrumentedHttpRequestExecutor.java:46)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at io.dropwizard.client.DropwizardApacheConnector.apply(DropwizardApacheConnector.java:89)
... 44 more
Can some please point out what am I doing wrong?
Or suggest some method to increase timeout?
This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 14 days
I'm having this issue with IT test on dropwizard 2.0.X
Getting socker timeout exception with dropwizard for integration tests
It times out because in the DropwizardAppRule class connectTimeout is hardcoded to 1000 (1s).
protected JerseyClientBuilder clientBuilder() {
return (new JerseyClientBuilder()).register(new JacksonBinder(this.getObjectMapper())).property("jersey.config.client.connectTimeout", 1000).property("jersey.config.client.readTimeout", 5000).property("jersey.config.client.httpUrlConnection.setMethodWorkaround", true);
}
Instead initialise the client directly as below.
Client client = (new org.glassfish.jersey.client.JerseyClientBuilder())
.register(new JacksonBinder(RULE.getObjectMapper()))
.property("jersey.config.client.connectTimeout", 10000)
.property("jersey.config.client.readTimeout", 10000)
.property("jersey.config.client.httpUrlConnection.setMethodWorkaround", true)
.build();
@ankurlucknowi Adding explicit time outs helped!
You can also use the JerseyClientConfiguration object directly to set the timeout.
final JerseyClientConfiguration jerseyClientConfiguration = new JerseyClientConfiguration();
jerseyClientConfiguration.setTimeout(Duration.minutes(1)); // wish this had a builder API
Client client = new JerseyClientBuilder(SUPPORT.getEnvironment())
.using(jerseyClientConfiguration)
.build("test client");
Most helpful comment
It times out because in the DropwizardAppRule class connectTimeout is hardcoded to 1000 (1s).
Instead initialise the client directly as below.