下面列出了怎么用org.mockito.internal.verification.AtLeast的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void idleConnectionReaperDoesNotReapActiveConnections() throws InterruptedException {
Duration maxIdleTime = Duration.ofSeconds(2);
try(SdkAsyncHttpClient client = NettyNioAsyncHttpClient.builder()
.connectionMaxIdleTime(maxIdleTime)
.buildWithDefaults(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS)) {
Instant end = Instant.now().plus(maxIdleTime.plusSeconds(1));
// Send requests for longer than the max-idle time, ensuring no connections are closed.
while (Instant.now().isBefore(end)) {
makeRequest(client);
Thread.sleep(100);
verify(TRAFFIC_LISTENER, new Times(0)).closed(any());
}
// Do nothing for longer than the max-idle time, ensuring connections are closed.
Thread.sleep(maxIdleTime.plusSeconds(1).toMillis());
verify(TRAFFIC_LISTENER, new AtLeast(1)).closed(any());
}
}
@Test
public void oldConnectionReaperReapsActiveConnections() throws InterruptedException {
Duration connectionTtl = Duration.ofMillis(200);
try (SdkAsyncHttpClient client = NettyNioAsyncHttpClient.builder()
.connectionTimeToLive(connectionTtl)
.buildWithDefaults(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS)) {
Instant end = Instant.now().plus(Duration.ofSeconds(5));
verify(TRAFFIC_LISTENER, new Times(0)).closed(any());
// Send requests frequently, validating that connections are still being closed.
while (Instant.now().isBefore(end)) {
makeRequest(client);
Thread.sleep(100);
}
verify(TRAFFIC_LISTENER, new AtLeast(20)).closed(any());
}
}
@Test
public void testClose() throws Exception {
CommandFuture<Void> future = new DefaultCommandFuture<>();
when(command.execute()).thenReturn(future);
future.setFailure(new Exception());
RetryCommandFactory<Void> retryCommandFactory = DefaultRetryCommandFactory.retryForever(scheduled, 0);
OneThreadTaskExecutor oneThreadTaskExecutor = new OneThreadTaskExecutor(retryCommandFactory, executors);
oneThreadTaskExecutor.executeCommand(command);
sleep(100);
logger.info("[testClose][destroy]");
oneThreadTaskExecutor.destroy();
retryCommandFactory.destroy();
sleep(50);
verify(command, new AtLeast(1)).execute();
verify(command, new AtLeast(1)).reset();
logger.info("[testClose][sleep verify no more interactions]");
sleep(100);
verifyNoMoreInteractions(command);
}
@Test
public void testClose() throws Exception {
CommandFuture<Void> future = new DefaultCommandFuture<>();
when(command.execute()).thenReturn(future);
future.setFailure(new Exception());
RetryCommandFactory<Void> retryCommandFactory = DefaultRetryCommandFactory.retryForever(scheduled, 0);
OneThreadTaskExecutor oneThreadTaskExecutor = new OneThreadTaskExecutor(retryCommandFactory, executors);
oneThreadTaskExecutor.executeCommand(command);
sleep(100);
logger.info("[testClose][destroy]");
oneThreadTaskExecutor.destroy();
retryCommandFactory.destroy();
sleep(50);
verify(command, new AtLeast(1)).execute();
verify(command, new AtLeast(1)).reset();
logger.info("[testClose][sleep verify no more interactions]");
sleep(100);
verifyNoMoreInteractions(command);
}
@Test
public void shouldCreateCorrectType() {
Timeout t = new Timeout(25, 50, mode);
assertCorrectMode(t.atLeastOnce(), Timeout.class, 50, 25, AtLeast.class);
assertCorrectMode(t.atLeast(5), Timeout.class, 50, 25, AtLeast.class);
assertCorrectMode(t.times(5), Timeout.class, 50, 25, Times.class);
assertCorrectMode(t.never(), Timeout.class, 50, 25, Times.class);
assertCorrectMode(t.only(), Timeout.class, 50, 25, Only.class);
assertCorrectMode(t.atMost(10), Timeout.class, 50, 25, AtMost.class);
}
@Test
public void should_create_correctly_configured_timeout() {
Timeout t = new Timeout(25, 50, mode);
assertTimeoutCorrectlyConfigured(t.atLeastOnce(), Timeout.class, 50, 25, AtLeast.class);
assertTimeoutCorrectlyConfigured(t.atLeast(5), Timeout.class, 50, 25, AtLeast.class);
assertTimeoutCorrectlyConfigured(t.times(5), Timeout.class, 50, 25, Times.class);
assertTimeoutCorrectlyConfigured(t.only(), Timeout.class, 50, 25, Only.class);
}