package de.juplo.kafka;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.LongSerializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.common.utils.Bytes;
+import org.springframework.boot.test.context.TestConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Primary;
+import org.springframework.test.context.ContextConfiguration;
import java.util.Set;
import java.util.function.Consumer;
+@ContextConfiguration(classes = ApplicationTest.Configuration.class)
public class ApplicationTest extends GenericApplicationTest<String, Long>
{
public ApplicationTest()
public void generate(
int numberOfMessagesToGenerate,
Set<Integer> poisonPills,
+ Set<Integer> logicErrors,
Consumer<ProducerRecord<Bytes, Bytes>> messageSender)
{
int i = 0;
if (++i > numberOfMessagesToGenerate)
return;
- Bytes value =
- poisonPills.contains(i)
- ? new Bytes(stringSerializer.serialize(TOPIC, "BOOM!"))
- : new Bytes(longSerializer.serialize(TOPIC, (long)i));
+ Bytes value = new Bytes(longSerializer.serialize(TOPIC, (long)i));
+ if (logicErrors.contains(i))
+ {
+ value = new Bytes(longSerializer.serialize(TOPIC, Long.MIN_VALUE));
+ }
+ if (poisonPills.contains(i))
+ {
+ value = new Bytes(stringSerializer.serialize(TOPIC, "BOOM (Poison-Pill)!"));
+ }
ProducerRecord<Bytes, Bytes> record =
new ProducerRecord<>(
}
});
}
+
+
+ @TestConfiguration
+ public static class Configuration
+ {
+ @Primary
+ @Bean
+ public Consumer<ConsumerRecord<String, Long>> consumer()
+ {
+ return (record) ->
+ {
+ if (record.value() == Long.MIN_VALUE)
+ throw new RuntimeException("BOOM (Logic-Error)!");
+ };
+ }
+ }
}
@Autowired
KafkaConsumer<K, V> kafkaConsumer;
@Autowired
+ Consumer<ConsumerRecord<K, V>> consumer;
+ @Autowired
ApplicationProperties properties;
@Autowired
ExecutorService executor;
KafkaProducer<Bytes, Bytes> testRecordProducer;
KafkaConsumer<Bytes, Bytes> offsetConsumer;
- Consumer<ConsumerRecord<K, V>> testHandler;
EndlessConsumer<K, V> endlessConsumer;
Map<TopicPartition, Long> oldOffsets;
Map<TopicPartition, Long> newOffsets;
@Test
void commitsCurrentOffsetsOnSuccess()
{
- recordGenerator.generate(100, Set.of(), messageSender);
+ recordGenerator.generate(100, Set.of(), Set.of(), messageSender);
await("100 records received")
.atMost(Duration.ofSeconds(30))
@Test
void commitsOffsetOfErrorForReprocessingOnDeserializationError()
{
- recordGenerator.generate(100, Set.of(77), messageSender);
+ recordGenerator.generate(100, Set.of(77), Set.of(), messageSender);
await("Consumer failed")
.atMost(Duration.ofSeconds(30))
.containsInstanceOf(RecordDeserializationException.class);
}
+ @Test
+ void doesNotCommitOffsetsOnLogicError()
+ {
+ recordGenerator.generate(100, Set.of(), Set.of(77), messageSender);
+
+ await("Consumer failed")
+ .atMost(Duration.ofSeconds(30))
+ .pollInterval(Duration.ofSeconds(1))
+ .until(() -> !endlessConsumer.running());
+
+ checkSeenOffsetsForProgress();
+ compareToCommitedOffsets(oldOffsets);
+
+ endlessConsumer.start();
+ await("Consumer failed")
+ .atMost(Duration.ofSeconds(30))
+ .pollInterval(Duration.ofSeconds(1))
+ .until(() -> !endlessConsumer.running());
+
+ checkSeenOffsetsForProgress();
+ compareToCommitedOffsets(oldOffsets);
+ assertThat(receivedRecords.size())
+ .describedAs("Received not all sent events")
+ .isLessThan(100);
+
+ assertThatNoException()
+ .describedAs("Consumer should not be running")
+ .isThrownBy(() -> endlessConsumer.exitStatus());
+ assertThat(endlessConsumer.exitStatus())
+ .describedAs("Consumer should have exited abnormally")
+ .containsInstanceOf(RuntimeException.class);
+ }
+
/** Helper methods for the verification of expectations */
{
void generate(
int numberOfMessagesToGenerate,
- Set<Integer> poistionPills,
+ Set<Integer> poisonPills,
+ Set<Integer> logicErrors,
Consumer<ProducerRecord<Bytes, Bytes>> messageSender);
}
props.put("value.deserializer", BytesDeserializer.class.getName());
offsetConsumer = new KafkaConsumer<>(props);
- testHandler = record -> {} ;
-
seekToEnd();
oldOffsets = new HashMap<>();
new TopicPartition(record.topic(), record.partition()),
record.offset());
receivedRecords.add(record);
- testHandler.accept(record);
+ consumer.accept(record);
};
endlessConsumer =
@TestConfiguration
@Import(ApplicationConfiguration.class)
- public static class Configuration {}
+ public static class Configuration
+ {
+ }
}