Search in sources :

Example 56 with ArgumentCaptor

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);
}
Also used : Collections.unmodifiableList(java.util.Collections.unmodifiableList) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) Duration(io.dropwizard.util.Duration) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Counter(com.codahale.metrics.Counter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Collections.singletonMap(java.util.Collections.singletonMap) Maps(io.dropwizard.util.Maps) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) OS(org.junit.jupiter.api.condition.OS) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Mockito.times(org.mockito.Mockito.times) HealthCheck(com.codahale.metrics.health.HealthCheck) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) MetricRegistry(com.codahale.metrics.MetricRegistry) HealthCheck(com.codahale.metrics.health.HealthCheck) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

Example 57 with ArgumentCaptor

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);
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) InstrumentedThreadFactory(com.codahale.metrics.InstrumentedThreadFactory) ExecutorServiceManager(io.dropwizard.lifecycle.ExecutorServiceManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Mockito.when(org.mockito.Mockito.when) Duration(io.dropwizard.util.Duration) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) AfterEach(org.junit.jupiter.api.AfterEach) ArgumentCaptor(org.mockito.ArgumentCaptor) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ThreadFactory(java.util.concurrent.ThreadFactory) Nullable(javax.annotation.Nullable) Mockito.mock(org.mockito.Mockito.mock) ExecutorServiceManager(io.dropwizard.lifecycle.ExecutorServiceManager) Test(org.junit.jupiter.api.Test)

Example 58 with ArgumentCaptor

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());
}
Also used : Test(org.junit.jupiter.api.Test) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Mockito(org.mockito.Mockito) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) MemoryPool(org.apache.kafka.common.memory.MemoryPool) ArgumentCaptor(org.mockito.ArgumentCaptor) TestUtils(org.apache.kafka.test.TestUtils) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) IOException(java.io.IOException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteBuffer(java.nio.ByteBuffer) ByteBuffer(java.nio.ByteBuffer) MemoryPool(org.apache.kafka.common.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 59 with ArgumentCaptor

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());
}
Also used : Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ScatteringByteChannel(java.nio.channels.ScatteringByteChannel) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) TestUtils(org.apache.kafka.test.TestUtils) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) IOException(java.io.IOException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteBuffer(java.nio.ByteBuffer) ByteBuffer(java.nio.ByteBuffer) ScatteringByteChannel(java.nio.channels.ScatteringByteChannel) Test(org.junit.jupiter.api.Test)

Example 60 with ArgumentCaptor

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));
}
Also used : KafkaBasedLog(org.apache.kafka.connect.util.KafkaBasedLog) MockTime(org.apache.kafka.common.utils.MockTime) Arrays(java.util.Arrays) TOPIC_STATUS_SEPARATOR(org.apache.kafka.connect.storage.KafkaStatusBackingStore.TOPIC_STATUS_SEPARATOR) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) TopicStatus(org.apache.kafka.connect.runtime.TopicStatus) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Before(org.junit.Before) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Time(org.apache.kafka.common.utils.Time) SCHEMAS_ENABLE_CONFIG(org.apache.kafka.connect.json.JsonConverterConfig.SCHEMAS_ENABLE_CONFIG) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) CONNECTOR_STATUS_PREFIX(org.apache.kafka.connect.storage.KafkaStatusBackingStore.CONNECTOR_STATUS_PREFIX) TASK_STATUS_PREFIX(org.apache.kafka.connect.storage.KafkaStatusBackingStore.TASK_STATUS_PREFIX) Mockito.verify(org.mockito.Mockito.verify) Assert.assertNull(org.junit.Assert.assertNull) TOPIC_STATUS_PREFIX(org.apache.kafka.connect.storage.KafkaStatusBackingStore.TOPIC_STATUS_PREFIX) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Assert.assertFalse(org.junit.Assert.assertFalse) JsonConverter(org.apache.kafka.connect.json.JsonConverter) Mockito.any(org.mockito.Mockito.any) Callback(org.apache.kafka.clients.producer.Callback) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) Callback(org.apache.kafka.clients.producer.Callback) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) TopicStatus(org.apache.kafka.connect.runtime.TopicStatus) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Test(org.junit.Test)

Aggregations

ArgumentCaptor (org.mockito.ArgumentCaptor)231 Mockito.verify (org.mockito.Mockito.verify)156 List (java.util.List)128 Test (org.junit.Test)116 Mockito.mock (org.mockito.Mockito.mock)113 Mockito.when (org.mockito.Mockito.when)100 Test (org.junit.jupiter.api.Test)86 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)85 Mockito (org.mockito.Mockito)85 Map (java.util.Map)76 BeforeEach (org.junit.jupiter.api.BeforeEach)72 Arrays (java.util.Arrays)69 Collectors (java.util.stream.Collectors)69 Collections (java.util.Collections)68 Before (org.junit.Before)65 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)64 Mockito.times (org.mockito.Mockito.times)64 ArrayList (java.util.ArrayList)60 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)60 Mockito.never (org.mockito.Mockito.never)58