Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/com/aliyun/tea/okhttp/OkHttpClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public OkHttpClientBuilder connectionPool(Map<String, Object> map) {
} catch (Exception e) {
maxIdleConnections = 5;
}
ConnectionPool connectionPool = new ConnectionPool(maxIdleConnections, 10000L, TimeUnit.MILLISECONDS);
long keepAliveDuration = 10000L;
if (map.containsKey("keepAliveDuration") && null != map.get("keepAliveDuration")) {
keepAliveDuration = Long.parseLong(String.valueOf(map.get("keepAliveDuration")));
}
ConnectionPool connectionPool = new ConnectionPool(maxIdleConnections, keepAliveDuration, TimeUnit.MILLISECONDS);
this.builder.connectionPool(connectionPool);
return this;
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/com/aliyun/tea/okhttp/OkHttpClientBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ public void connectionPoolTest() {
map.put("maxIdleConns", 666);
clientBuilder.connectionPool(map);
Mockito.verify(clientBuilder, Mockito.times(2)).connectionPool(map);

map.put("keepAliveDuration", null);
clientBuilder.connectionPool(map);

map.put("keepAliveDuration", "");
try {
clientBuilder.connectionPool(map);
Assert.fail();
} catch (NumberFormatException e) {
Assert.assertTrue(e.getMessage().contains("For input string: \"\""));
}

map.put("keepAliveDuration", "str");
try {
clientBuilder.connectionPool(map);
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("For input string: \"str\""));
}

map.put("keepAliveDuration", 0);
try {
clientBuilder.connectionPool(map);
Assert.fail();
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("keepAliveDuration <= 0: 0"));
}

map.put("keepAliveDuration", 100L);
clientBuilder.connectionPool(map);
Mockito.verify(clientBuilder, Mockito.times(7)).connectionPool(map);
}

@Test
Expand Down