2019-04-01 18:18:24 +02:00
|
|
|
package com.deviceinsight.kafka.health;
|
|
|
|
|
|
|
|
import static com.deviceinsight.kafka.health.KafkaConsumingHealthIndicatorTest.TOPIC;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
import kafka.server.KafkaServer;
|
|
|
|
import org.apache.kafka.clients.consumer.Consumer;
|
|
|
|
import org.apache.kafka.common.serialization.StringDeserializer;
|
2019-05-16 09:25:22 +02:00
|
|
|
import org.awaitility.Awaitility;
|
2019-04-01 21:17:35 +02:00
|
|
|
import org.junit.jupiter.api.*;
|
2019-04-01 18:18:24 +02:00
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.actuate.health.Health;
|
|
|
|
import org.springframework.boot.actuate.health.Status;
|
|
|
|
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
|
|
|
|
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
|
|
|
|
import org.springframework.kafka.test.EmbeddedKafkaBroker;
|
|
|
|
import org.springframework.kafka.test.context.EmbeddedKafka;
|
|
|
|
import org.springframework.kafka.test.core.BrokerAddress;
|
|
|
|
import org.springframework.kafka.test.utils.KafkaTestUtils;
|
|
|
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
@ExtendWith(SpringExtension.class)
|
|
|
|
@EmbeddedKafka(topics = {TOPIC})
|
|
|
|
public class KafkaConsumingHealthIndicatorTest {
|
|
|
|
|
|
|
|
static final String TOPIC = "health-checks";
|
|
|
|
|
|
|
|
private Consumer<String, String> consumer;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private EmbeddedKafkaBroker embeddedKafkaBroker;
|
|
|
|
|
2019-04-01 21:17:35 +02:00
|
|
|
@BeforeEach
|
2019-04-01 18:18:24 +02:00
|
|
|
public void setUp() {
|
|
|
|
Map<String, Object> consumerConfigs =
|
|
|
|
new HashMap<>(KafkaTestUtils.consumerProps("consumer", "false", embeddedKafkaBroker));
|
|
|
|
consumer = new DefaultKafkaConsumerFactory<>(consumerConfigs, new StringDeserializer(),
|
|
|
|
new StringDeserializer()).createConsumer();
|
|
|
|
consumer.subscribe(Collections.singletonList(TOPIC));
|
|
|
|
consumer.poll(Duration.ofSeconds(1));
|
|
|
|
}
|
|
|
|
|
2019-04-01 21:17:35 +02:00
|
|
|
@AfterEach
|
2019-04-01 18:18:24 +02:00
|
|
|
public void tearDown() {
|
|
|
|
consumer.close();
|
|
|
|
embeddedKafkaBroker.getKafkaServers().forEach(KafkaServer::shutdown);
|
|
|
|
embeddedKafkaBroker.getKafkaServers().forEach(KafkaServer::awaitShutdown);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void kafkaIsDown() throws Exception {
|
2019-04-01 21:17:35 +02:00
|
|
|
final KafkaHealthProperties kafkaHealthProperties = new KafkaHealthProperties();
|
2019-04-01 18:18:24 +02:00
|
|
|
kafkaHealthProperties.setTopic(TOPIC);
|
|
|
|
|
|
|
|
final KafkaProperties kafkaProperties = new KafkaProperties();
|
2019-04-01 21:17:35 +02:00
|
|
|
final BrokerAddress[] brokerAddresses = embeddedKafkaBroker.getBrokerAddresses();
|
2019-04-01 18:18:24 +02:00
|
|
|
kafkaProperties.setBootstrapServers(Collections.singletonList(brokerAddresses[0].toString()));
|
|
|
|
|
2019-04-01 21:17:35 +02:00
|
|
|
final KafkaConsumingHealthIndicator healthIndicator =
|
2019-04-01 18:18:24 +02:00
|
|
|
new KafkaConsumingHealthIndicator(kafkaHealthProperties, kafkaProperties.buildConsumerProperties(),
|
|
|
|
kafkaProperties.buildProducerProperties());
|
2019-05-16 09:25:22 +02:00
|
|
|
healthIndicator.subscribeAndSendMessage();
|
2019-04-01 18:18:24 +02:00
|
|
|
|
|
|
|
Health health = healthIndicator.health();
|
|
|
|
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
|
|
|
|
|
|
|
shutdownKafka();
|
2019-04-01 21:17:35 +02:00
|
|
|
|
2019-05-16 09:25:22 +02:00
|
|
|
Awaitility.await().untilAsserted(() -> assertThat(healthIndicator.health().getStatus()).isEqualTo(Status.DOWN));
|
2019-04-01 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void shutdownKafka() {
|
|
|
|
this.embeddedKafkaBroker.destroy();
|
|
|
|
}
|
|
|
|
}
|