0

In my Spring Boot application I implemented the following error handling logic when processing event from a Kafka topic:

  1. Retry failed messages defined number of times.
  2. Stop MessageListenerContainer if message couldn't be processed after X number of attempts.

To stop the container I use the approach described in this answer: https://stackoverflow.com/a/67077461/4800887

But in short the container is stopped like this:

new CommonContainerStoppingErrorHandler().handleOtherException(ex, null, registry.getListenerContainer(listenerContainerId), false);

Now I'm trying to test this functionality in my @SpringBootTest test. For that I'm checking container running status like this:

var container = kafkaListenerEndpointRegistry.getListenerContainer(listenerContainerId);
assertThat(container.isRunning()).isFalse();

This works fine. But the issue I'm having is in the other test which is trying to read current offset and send a message to the same EmbeddedKafkaBroker. The error I see in the logs is this:

o.a.k.c.c.internals.ConsumerCoordinator  : [Consumer clientId=consumer-domain_consumer_test-1, groupId=domain_consumer_test] Synchronous auto-commit of offsets
 {my-topic-0=OffsetAndMetadata{offset=0, leaderEpoch=null, metadata=''}} failed: Offset
 commit cannot be completed since the consumer is not part of an active group for auto
 partition assignment; it is likely that the consumer was kicked out of the group.

My question is this: how I can restart normal container operation after it was stopped the way I showed above, so that other tests in the same class can operate normally? Or maybe I can restart EmbeddedKafkaBroker itself, that would be fine as well.

I tried to use advice from this answer: https://stackoverflow.com/a/64157233/4800887

embeddedKafkaBroker.destroy();
embeddedKafkaBroker.afterPropertiesSet();

But that doesn't seem to be working.

dmitryb
  • 281
  • 2
  • 12
  • It's not clear what you mean. I don't understand how you can have multiple tests using the same container. You can use the container registry to restart that container, but the same record (the one that caused it to be stopped) will be redelivered. Stopping one container won't affect the embedded broker, or any other container. – Gary Russell May 01 '23 at 14:21
  • Thank you for your response @GaryRussell! I have multiple tests to test different failure scenarios which result in the listener container being stopped as the message can't be processed. So, I wanted to have all these tests gathered in a single test class. But you're absolutely right, a faulty message from the previous test would still be there when I want to test a different scenario in the next test. So it means that I also need to flush the topic before running the next test. Then, probably having a single failure test in different test classes is the right approach to take here? – dmitryb May 10 '23 at 13:17
  • 1
    Since you are stopping/starting the container between each test, you could add a `ConsumerAwareRebalanceListener` (in your test only) and always seek to end when partitions are assigned. – Gary Russell May 10 '23 at 13:29

0 Answers0