use of org.mockito.ArgumentCaptor in project dropwizard by dropwizard.
the class HealthCheckManagerTest method shouldMarkServerHealthyWhenCriticalHealthCheckRecovers.
@Test
void shouldMarkServerHealthyWhenCriticalHealthCheckRecovers() {
final HealthCheckConfiguration config = new HealthCheckConfiguration();
config.setName(NAME);
config.setCritical(true);
config.setSchedule(new Schedule());
final HealthCheckManager manager = new HealthCheckManager(singletonList(config), scheduler, new MetricRegistry(), SHUTDOWN_WAIT, true, Collections.emptyList());
final HealthCheck check = mock(HealthCheck.class);
manager.onHealthCheckAdded(NAME, check);
manager.onStateChanged(NAME, false);
assertThat(manager.isHealthy()).isFalse();
manager.onStateChanged(NAME, true);
assertThat(manager.isHealthy()).isTrue();
ArgumentCaptor<ScheduledHealthCheck> checkCaptor = ArgumentCaptor.forClass(ScheduledHealthCheck.class);
ArgumentCaptor<Boolean> healthyCaptor = ArgumentCaptor.forClass(Boolean.class);
verify(scheduler).scheduleInitial(checkCaptor.capture());
verify(scheduler, times(2)).schedule(checkCaptor.capture(), healthyCaptor.capture());
assertThat(checkCaptor.getAllValues()).hasSize(3).allMatch(value -> NAME.equals(value.getName())).allMatch(ScheduledHealthCheck::isCritical);
assertThat(healthyCaptor.getAllValues()).containsExactly(false, true);
}
use of org.mockito.ArgumentCaptor in project dropwizard by dropwizard.
the class ScheduledExecutorServiceBuilderTest method testBasicInvocation.
@Test
void testBasicInvocation() {
final String poolName = this.getClass().getSimpleName();
final ScheduledExecutorServiceBuilder test = new ScheduledExecutorServiceBuilder(this.le, poolName, false);
this.execTracker = test.build();
assertThat(this.execTracker).isInstanceOfSatisfying(ScheduledThreadPoolExecutor.class, exe -> assertThat(exe).satisfies(castedExec -> assertThat(castedExec.getRemoveOnCancelPolicy()).isFalse()).satisfies(castedExec -> assertThat(castedExec.getThreadFactory()).isInstanceOf(InstrumentedThreadFactory.class)));
final ArgumentCaptor<ExecutorServiceManager> esmCaptor = ArgumentCaptor.forClass(ExecutorServiceManager.class);
verify(this.le).manage(esmCaptor.capture());
final ExecutorServiceManager esmCaptured = esmCaptor.getValue();
assertThat(esmCaptured.getExecutor()).isSameAs(this.execTracker);
assertThat(esmCaptured.getShutdownPeriod()).isEqualTo(DEFAULT_SHUTDOWN_PERIOD);
assertThat(esmCaptured.getPoolName()).isSameAs(poolName);
}
use of org.mockito.ArgumentCaptor in project kafka by apache.
the class KafkaChannelTest method testReceiving.
@Test
public void testReceiving() throws IOException {
Authenticator authenticator = Mockito.mock(Authenticator.class);
TransportLayer transport = Mockito.mock(TransportLayer.class);
MemoryPool pool = Mockito.mock(MemoryPool.class);
ChannelMetadataRegistry metadataRegistry = Mockito.mock(ChannelMetadataRegistry.class);
ArgumentCaptor<Integer> sizeCaptor = ArgumentCaptor.forClass(Integer.class);
Mockito.when(pool.tryAllocate(sizeCaptor.capture())).thenAnswer(invocation -> {
return ByteBuffer.allocate(sizeCaptor.getValue());
});
KafkaChannel channel = new KafkaChannel("0", transport, () -> authenticator, 1024, pool, metadataRegistry);
ArgumentCaptor<ByteBuffer> bufferCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
Mockito.when(transport.read(bufferCaptor.capture())).thenAnswer(invocation -> {
bufferCaptor.getValue().putInt(128);
return 4;
}).thenReturn(0);
assertEquals(4, channel.read());
assertEquals(4, channel.currentReceive().bytesRead());
assertNull(channel.maybeCompleteReceive());
Mockito.reset(transport);
Mockito.when(transport.read(bufferCaptor.capture())).thenAnswer(invocation -> {
bufferCaptor.getValue().put(TestUtils.randomBytes(64));
return 64;
});
assertEquals(64, channel.read());
assertEquals(68, channel.currentReceive().bytesRead());
assertNull(channel.maybeCompleteReceive());
Mockito.reset(transport);
Mockito.when(transport.read(bufferCaptor.capture())).thenAnswer(invocation -> {
bufferCaptor.getValue().put(TestUtils.randomBytes(64));
return 64;
});
assertEquals(64, channel.read());
assertEquals(132, channel.currentReceive().bytesRead());
assertNotNull(channel.maybeCompleteReceive());
assertNull(channel.currentReceive());
}
use of org.mockito.ArgumentCaptor in project kafka by apache.
the class NetworkReceiveTest method testBytesRead.
@Test
public void testBytesRead() throws IOException {
NetworkReceive receive = new NetworkReceive(128, "0");
assertEquals(0, receive.bytesRead());
ScatteringByteChannel channel = Mockito.mock(ScatteringByteChannel.class);
ArgumentCaptor<ByteBuffer> bufferCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
Mockito.when(channel.read(bufferCaptor.capture())).thenAnswer(invocation -> {
bufferCaptor.getValue().putInt(128);
return 4;
}).thenReturn(0);
assertEquals(4, receive.readFrom(channel));
assertEquals(4, receive.bytesRead());
assertFalse(receive.complete());
Mockito.reset(channel);
Mockito.when(channel.read(bufferCaptor.capture())).thenAnswer(invocation -> {
bufferCaptor.getValue().put(TestUtils.randomBytes(64));
return 64;
});
assertEquals(64, receive.readFrom(channel));
assertEquals(68, receive.bytesRead());
assertFalse(receive.complete());
Mockito.reset(channel);
Mockito.when(channel.read(bufferCaptor.capture())).thenAnswer(invocation -> {
bufferCaptor.getValue().put(TestUtils.randomBytes(64));
return 64;
});
assertEquals(64, receive.readFrom(channel));
assertEquals(132, receive.bytesRead());
assertTrue(receive.complete());
}
use of org.mockito.ArgumentCaptor in project kafka by apache.
the class KafkaStatusBackingStoreFormatTest method putTopicStateRetriableFailure.
@Test
public void putTopicStateRetriableFailure() {
TopicStatus topicStatus = new TopicStatus(FOO_TOPIC, new ConnectorTaskId(FOO_CONNECTOR, 0), time.milliseconds());
String key = TOPIC_STATUS_PREFIX + FOO_TOPIC + TOPIC_STATUS_SEPARATOR + FOO_CONNECTOR;
ArgumentCaptor<byte[]> valueCaptor = ArgumentCaptor.forClass(byte[].class);
doAnswer(invocation -> {
((Callback) invocation.getArgument(2)).onCompletion(null, new TimeoutException());
return null;
}).doAnswer(invocation -> {
((Callback) invocation.getArgument(2)).onCompletion(null, null);
return null;
}).when(kafkaBasedLog).send(eq(key), valueCaptor.capture(), any(Callback.class));
store.put(topicStatus);
verify(kafkaBasedLog, times(2)).send(any(), any(), any());
// check capture state
assertEquals(topicStatus, store.parseTopicStatus(valueCaptor.getValue()));
// state is not visible until read back from the log
assertNull(store.getTopic(FOO_CONNECTOR, FOO_TOPIC));
}
Aggregations