projects
/
demos
/
kafka
/
training
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Springify: Merge des verschärften Tests aus der Vanilla-Version
[demos/kafka/training]
/
src
/
test
/
java
/
de
/
juplo
/
kafka
/
ApplicationTests.java
diff --git
a/src/test/java/de/juplo/kafka/ApplicationTests.java
b/src/test/java/de/juplo/kafka/ApplicationTests.java
index
92074ff
..
1d3546c
100644
(file)
--- a/
src/test/java/de/juplo/kafka/ApplicationTests.java
+++ b/
src/test/java/de/juplo/kafka/ApplicationTests.java
@@
-9,12
+9,15
@@
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.errors.RecordDeserializationException;
import org.apache.kafka.common.serialization.*;
import org.apache.kafka.common.utils.Bytes;
import org.apache.kafka.common.errors.RecordDeserializationException;
import org.apache.kafka.common.serialization.*;
import org.apache.kafka.common.utils.Bytes;
+import org.assertj.core.api.OptionalAssert;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
+import org.springframework.context.annotation.Primary;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@
-22,7
+25,6
@@
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
@@
-31,13
+33,16
@@
import java.util.stream.IntStream;
import static de.juplo.kafka.ApplicationTests.PARTITIONS;
import static de.juplo.kafka.ApplicationTests.TOPIC;
import static de.juplo.kafka.ApplicationTests.PARTITIONS;
import static de.juplo.kafka.ApplicationTests.TOPIC;
-import static org.assertj.core.api.Assertions.
assertThat
;
+import static org.assertj.core.api.Assertions.
*
;
import static org.awaitility.Awaitility.*;
import static org.awaitility.Awaitility.*;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-@SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class)
+@SpringJUnitConfig(
+ initializers = ConfigDataApplicationContextInitializer.class,
+ classes = {
+ EndlessConsumer.class,
+ KafkaAutoConfiguration.class,
+ ApplicationTests.Configuration.class })
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestPropertySource(
properties = {
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestPropertySource(
properties = {
@@
-58,16
+63,14
@@
class ApplicationTests
@Autowired
KafkaProducer<String, Bytes> kafkaProducer;
@Autowired
@Autowired
KafkaProducer<String, Bytes> kafkaProducer;
@Autowired
- KafkaConsumer<String, Long> kafkaConsumer;
- @Autowired
KafkaConsumer<Bytes, Bytes> offsetConsumer;
@Autowired
ApplicationProperties properties;
@Autowired
KafkaConsumer<Bytes, Bytes> offsetConsumer;
@Autowired
ApplicationProperties properties;
@Autowired
- ExecutorService executor;
+ EndlessConsumer endlessConsumer;
+ @Autowired
+ RecordHandler recordHandler;
- Consumer<ConsumerRecord<String, Long>> testHandler;
- EndlessConsumer<String, Long> endlessConsumer;
Map<TopicPartition, Long> oldOffsets;
Map<TopicPartition, Long> newOffsets;
Set<ConsumerRecord<String, Long>> receivedRecords;
Map<TopicPartition, Long> oldOffsets;
Map<TopicPartition, Long> newOffsets;
Set<ConsumerRecord<String, Long>> receivedRecords;
@@
-93,10
+96,9
@@
class ApplicationTests
compareToCommitedOffsets(newOffsets);
});
compareToCommitedOffsets(newOffsets);
});
- assertThrows(
- IllegalStateException.class,
- () -> endlessConsumer.exitStatus(),
- "Consumer should still be running");
+ assertThatExceptionOfType(IllegalStateException.class)
+ .isThrownBy(() -> endlessConsumer.exitStatus())
+ .describedAs("Consumer should still be running");
}
@Test
}
@Test
@@
-110,26
+112,17
@@
class ApplicationTests
await("Consumer failed")
.atMost(Duration.ofSeconds(30))
await("Consumer failed")
.atMost(Duration.ofSeconds(30))
- .until
(() -> !endlessConsumer.running
());
+ .until
Asserted(() -> checkSeenOffsetsForProgress
());
- checkSeenOffsetsForProgress();
- compareToCommitedOffsets(newOffsets);
-
- endlessConsumer.start();
- await("Consumer failed")
- .atMost(Duration.ofSeconds(30))
- .until(() -> !endlessConsumer.running());
-
- checkSeenOffsetsForProgress();
compareToCommitedOffsets(newOffsets);
assertThat(receivedRecords.size())
.describedAs("Received not all sent events")
.isLessThan(100);
compareToCommitedOffsets(newOffsets);
assertThat(receivedRecords.size())
.describedAs("Received not all sent events")
.isLessThan(100);
- assert
DoesNotThrow(
- () -> endlessConsumer.exitStatus(),
-
"Consumer should not be running"
);
-
assertThat(endlessConsumer.exitStatus(
))
+ assert
ThatNoException()
+ .describedAs("Consumer should not be running")
+
.isThrownBy(() -> endlessConsumer.exitStatus()
);
+
((OptionalAssert<Exception>)assertThat(endlessConsumer.exitStatus()
))
.describedAs("Consumer should have exited abnormally")
.containsInstanceOf(RecordDeserializationException.class);
}
.describedAs("Consumer should have exited abnormally")
.containsInstanceOf(RecordDeserializationException.class);
}
@@
-233,7
+226,7
@@
class ApplicationTests
@BeforeEach
public void init()
{
@BeforeEach
public void init()
{
-
testHandler = record -> {}
;
+
recordHandler.testHandler = (record) -> {}
;
oldOffsets = new HashMap<>();
newOffsets = new HashMap<>();
oldOffsets = new HashMap<>();
newOffsets = new HashMap<>();
@@
-245,24
+238,15
@@
class ApplicationTests
newOffsets.put(tp, offset - 1);
});
newOffsets.put(tp, offset - 1);
});
-
Consumer<ConsumerRecord<String, Long>> captureOffsetAndExecuteTestHandler
=
+
recordHandler.captureOffsets
=
record ->
{
record ->
{
+ receivedRecords.add(record);
newOffsets.put(
new TopicPartition(record.topic(), record.partition()),
record.offset());
newOffsets.put(
new TopicPartition(record.topic(), record.partition()),
record.offset());
- receivedRecords.add(record);
- testHandler.accept(record);
};
};
- endlessConsumer =
- new EndlessConsumer<>(
- executor,
- properties.getClientId(),
- properties.getTopic(),
- kafkaConsumer,
- captureOffsetAndExecuteTestHandler);
-
endlessConsumer.start();
}
endlessConsumer.start();
}
@@
-279,11
+263,32
@@
class ApplicationTests
}
}
}
}
+ public static class RecordHandler implements Consumer<ConsumerRecord<String, Long>>
+ {
+ Consumer<ConsumerRecord<String, Long>> captureOffsets;
+ Consumer<ConsumerRecord<String, Long>> testHandler;
+
+
+ @Override
+ public void accept(ConsumerRecord<String, Long> record)
+ {
+ captureOffsets
+ .andThen(testHandler)
+ .accept(record);
+ }
+ }
@TestConfiguration
@Import(ApplicationConfiguration.class)
public static class Configuration
{
@TestConfiguration
@Import(ApplicationConfiguration.class)
public static class Configuration
{
+ @Primary
+ @Bean
+ public Consumer<ConsumerRecord<String, Long>> testHandler()
+ {
+ return new RecordHandler();
+ }
+
@Bean
Serializer<Long> serializer()
{
@Bean
Serializer<Long> serializer()
{