ROT: Rückbau auf automatische Commits - Testfälle laufen nicht mehr
[demos/kafka/training] / src / test / java / de / juplo / kafka / GenericApplicationTests.java
index 9a6f812..cff44e2 100644 (file)
@@ -60,8 +60,6 @@ abstract class GenericApplicationTests<K, V>
        @Autowired
        ExecutorService executor;
        @Autowired
-       StateRepository stateRepository;
-       @Autowired
        PollIntervalAwareConsumerRebalanceListener rebalanceListener;
        @Autowired
        RecordHandler<K, V> recordHandler;
@@ -227,29 +225,23 @@ abstract class GenericApplicationTests<K, V>
        void seekToEnd()
        {
                offsetConsumer.assign(partitions());
+               offsetConsumer.seekToEnd(partitions());
                partitions().forEach(tp ->
                {
+                       // seekToEnd() works lazily: it only takes effect on poll()/position()
                        Long offset = offsetConsumer.position(tp);
                        log.info("New position for {}: {}", tp, offset);
-                       Integer partition = tp.partition();
-                       StateDocument document =
-                                       stateRepository
-                                                       .findById(partition.toString())
-                                                       .orElse(new StateDocument(partition));
-                       document.offset = offset;
-                       stateRepository.save(document);
                });
+               // The new positions must be commited!
+               offsetConsumer.commitSync();
                offsetConsumer.unsubscribe();
        }
 
        void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
        {
-               partitions().forEach(tp ->
-               {
-                       String partition = Integer.toString(tp.partition());
-                       Optional<Long> offset = stateRepository.findById(partition).map(document -> document.offset);
-                       consumer.accept(tp, offset.orElse(0l));
-               });
+               offsetConsumer.assign(partitions());
+               partitions().forEach(tp -> consumer.accept(tp, offsetConsumer.position(tp)));
+               offsetConsumer.unsubscribe();
        }
 
        List<TopicPartition> partitions()