我正在我的Jersey端点上运行并发测试,并且用@POST和@Path注释的端点处理程序有时(不一致地)被调用的次数比实际请求数(例如803而不是800)更多。
我已经用Dropwizard设置了一个Web应用程序(默认设置),使用ExecutorService发出并发请求,并使用AtomicIntegers计数了请求和端点处理程序
正在测试的客户端设置:
private static Client client = new JerseyClientBuilder().build();
测试:
CountDownLatch latch = new CountDownLatch(numberOfDeposits);
ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);
AtomicInteger atomicInteger =new AtomicInteger(0);
for (int i = 0; i < numberOfDeposits; i++) {
service.submit(() -> {
Response response = client.target(format(ACCOUNTS_URI + accountId + "/deposit/" + depositAmount, RULE.getLocalPort()))
.request()
.post(Entity.json(null));
latch.countDown();
counter.getAndIncrement();
assertThat(response.getStatus()).isEqualTo(200);
});
}
latch.await();
端点处理程序:
@POST
@Path("/{id}/deposit/{amount}")
public Account deposit(@PathParam("id") long accountId, @PathParam("amount")BigDecimal depositAmount){
counter2.getAndIncrement();
return accountDao.deposit(accountId, depositAmount);
}
测试中的计数器以N次调用结束,处理程序中的counter2以N,N + 2,N + 3(不一致)结束
为什么处理程序调用不一致?