【发布时间】:2015-09-16 06:01:24
【问题描述】:
我使用 Java 和 Spring 开发 Web 应用程序。为了检查当多个请求到来时系统的行为,我创建了测试:
@Test
public void shouldHandleMultipleRequests() throws Exception {
//given
final String endpoint = "http://localhost:9000/upload";
final File file = new File(format("src/test/resources/files/%s",
"file.txt"));
//when
final CompletableFuture<String> response1 = CompletableFuture.supplyAsync(() ->
Try.ofFailable(() ->
HttpClientBuilder.create().build().execute(
createHttpPost(endpoint, file))).orElse(null).getEntity().toString());
final CompletableFuture<String> response2 = CompletableFuture.supplyAsync(() ->
Try.ofFailable(() ->
HttpClientBuilder.create().build().execute(
createHttpPost(endpoint,file))).orElse(null).getEntity().toString());
assertThat(response1.get().contains("Something"));
assertThat(response2.get().contains("Something"));
}
一切正常,但是我注意到,如果我尝试运行 3 个以上的请求,则前 3 个请求和下一个请求之间会有延迟。
我的问题是
- 我认为这种行为是否与我的处理器上的线程数有关(4 个线程处理器,3 个用于请求和等待响应的线程,以及一个用于应用程序)?
- 如果有任何方法可以同时发送 3 个以上的请求(当然是稍后检索响应)?
【问题讨论】:
标签: java multithreading spring testing integration-testing