X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fkafka%2FApplicationTests.java;h=3a10cd1b057481f998e651e0ce10076c7a1ddc24;hb=3cf17ad1f308a2cd618c554d1142830469d74978;hp=3f6a6a8b47823a85fc36e6642e6131b7536c36bb;hpb=02bf5ef5c451eab56f01b2bd82941587383c0ede;p=demos%2Fkafka%2Ftraining diff --git a/src/test/java/de/juplo/kafka/ApplicationTests.java b/src/test/java/de/juplo/kafka/ApplicationTests.java index 3f6a6a8..3a10cd1 100644 --- a/src/test/java/de/juplo/kafka/ApplicationTests.java +++ b/src/test/java/de/juplo/kafka/ApplicationTests.java @@ -18,6 +18,7 @@ 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.listener.MessageListenerContainer; import org.springframework.kafka.support.serializer.JsonSerializer; import org.springframework.kafka.test.context.EmbeddedKafka; import org.springframework.test.context.TestPropertySource; @@ -48,6 +49,7 @@ import static org.awaitility.Awaitility.*; @TestPropertySource( properties = { "spring.kafka.consumer.bootstrap-servers=${spring.embedded.kafka.brokers}", + "spring.kafka.producer.bootstrap-servers=${spring.embedded.kafka.brokers}", "consumer.topic=" + TOPIC }) @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS) @Slf4j @@ -91,7 +93,7 @@ class ApplicationTests await("100 records received") .atMost(Duration.ofSeconds(30)) - .until(() -> receivedRecords.size() >= 100); + .until(() -> receivedRecords.size() == 100); await("Offsets committed") .atMost(Duration.ofSeconds(10)) @@ -101,35 +103,42 @@ class ApplicationTests compareToCommitedOffsets(newOffsets); }); - assertThatExceptionOfType(IllegalStateException.class) - .isThrownBy(() -> endlessConsumer.exitStatus()) - .describedAs("Consumer should still be running"); + assertThat(endlessConsumer.isRunning()) + .describedAs("Consumer should still be running") + .isTrue(); } @Test @Order(2) - void commitsOffsetOfErrorForReprocessingOnError() + void commitsCurrentOffsetsOnError() { send100Messages((key, counter) -> counter == 77 ? new Bytes(stringSerializer.serialize(TOPIC, "BOOM!")) : serialize(key, counter)); - await("Consumer failed") + await("99 records received") .atMost(Duration.ofSeconds(30)) - .untilAsserted(() -> checkSeenOffsetsForProgress()); - - compareToCommitedOffsets(newOffsets); - assertThat(receivedRecords.size()) - .describedAs("Received not all sent events") - .isLessThan(100); - - assertThatNoException() - .describedAs("Consumer should not be running") - .isThrownBy(() -> endlessConsumer.exitStatus()); - assertThat(endlessConsumer.exitStatus()) - .containsInstanceOf(RecordDeserializationException.class) - .describedAs("Consumer should have exited abnormally"); + .until(() -> receivedRecords.size() == 99); + + await("Offsets committed") + .atMost(Duration.ofSeconds(10)) + .untilAsserted(() -> + { + // UNSCHÖN: + // Funktioniert nur, weil nach der Nachrichten, die den + // Deserialisierungs-Fehler auslöst noch valide Nachrichten + // gelesen werden. + // GRUND: + // Der MessageHandler sieht den Offset der Fehlerhaften + // Nachricht nicht! + checkSeenOffsetsForProgress(); + compareToCommitedOffsets(newOffsets); + }); + + assertThat(endlessConsumer.isRunning()) + .describedAs("Consumer should still be running") + .isTrue(); } @@ -140,7 +149,7 @@ class ApplicationTests doForCurrentOffsets((tp, offset) -> { Long expected = offsetsToCheck.get(tp) + 1; - log.debug("Checking, if the offset for {} is {}", tp, expected); + log.debug("TEST: Comparing the expected offset of {} for {} to {}", expected, tp, offset); assertThat(offset) .describedAs("Committed offset corresponds to the offset of the consumer") .isEqualTo(expected); @@ -157,10 +166,11 @@ class ApplicationTests Long newOffset = newOffsets.get(tp); if (!oldOffset.equals(newOffset)) { - log.debug("Progress for {}: {} -> {}", tp, oldOffset, newOffset); + log.debug("TEST: Progress for {}: {} -> {}", tp, oldOffset, newOffset); withProgress.add(tp); } }); + log.debug("TEST: Offsets with progress: {}", withProgress); assertThat(withProgress) .describedAs("Some offsets must have changed, compared to the old offset-positions") .isNotEmpty(); @@ -209,7 +219,7 @@ class ApplicationTests if (metadata != null) { log.debug( - "{}|{} - {}={}", + "TEST: Sending partition={}, offset={} - {}={}", metadata.partition(), metadata.offset(), record.key(), @@ -218,7 +228,7 @@ class ApplicationTests else { log.warn( - "Exception for {}={}: {}", + "TEST: Exception for {}={}: {}", record.key(), record.value(), e.toString()); @@ -256,6 +266,7 @@ class ApplicationTests record -> { receivedRecords.add(record); + log.debug("TEST: Processing record #{}: {}", receivedRecords.size(), record.value()); newOffsets.put( new TopicPartition(record.topic(), record.partition()), record.offset()); @@ -273,7 +284,7 @@ class ApplicationTests } catch (Exception e) { - log.info("Exception while stopping the consumer: {}", e.toString()); + log.info("TEST: Exception while stopping the consumer: {}", e.toString()); } }