1

I wrote a test class in which I'would test topic deletion

I found for EmbeddedKafkaBroker this method

embeddedKafkaBroker.addTopics("new-topic");

that make me able to create a new topic, but I can't find anything to delete a topic.

So, how can I test topic deletion?

Vin
  • 701
  • 1
  • 9
  • 30

2 Answers2

1

See this one:

/**
 * Create an {@link AdminClient}; invoke the callback and reliably close the admin.
 * @param callback the callback.
 */
public void doWithAdmin(java.util.function.Consumer<AdminClient> callback) {

where you can use:

/**
 * This is a convenience method for {@link #deleteTopics(TopicCollection, DeleteTopicsOptions)}
 * with default options. See the overload for more details.
 * <p>
 * This operation is supported by brokers with version 0.10.1.0 or higher.
 *
 * @param topics The topic names to delete.
 * @return The DeleteTopicsResult.
 */
default DeleteTopicsResult deleteTopics(Collection<String> topics) {
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
1

To build on Artem's answer, this can be a bit tricky because of the asynchronicity of the events involved.

If the topic doesn't exist in the Kafka broker the describeTopics method in KafkaAdminClient throws an UnknownTopicOrPartitionException wrapped in an ExecutionException, so we can leverage that to check the topic's existence and wait for its creation or deletion.

EDIT: While the above strategy does work, the proper way to do this is to just wait for the future to complete, for example with admin.deleteTopics(List.of(topicName)).all().get()

But since the Embedded broker is not aware that the topic has been deleted, the embeddedKafkaBroker.getTopics() result doesn't get updated, so in order to find out if the topic has actually been deleted we need to use the admin.describeTopics(List.of(topicName)).all().get() method instead.

Tomaz Fernandes
  • 2,429
  • 2
  • 14
  • 18
  • 1
    Thanks, Tomaz! Tell me, please, why don't you use that `DeleteTopicsResult.all().get()` instead? – Artem Bilan Feb 28 '22 at 22:00
  • You're right, thanks. In my first attempt at this I relied on `embeddedKafkaBroker.getTopics()`, and even though I waited for the deletion future to complete, the topic was never removed, so I thought it didn't work as expected. That led me to this different path. Now using the describeTopics instead I see waiting for the Future works. I'll update my answer. – Tomaz Fernandes Feb 28 '22 at 22:38
  • Running admin.deleteTopics(admin.listTopics().names().get()).all().get() (so delete all known topics) before each test, after a while seems to cause: Expected 3 but got 0 partitions java.lang.IllegalStateException: Expected 3 but got 0 partitions at org.springframework.kafka.test.utils.ContainerTestUtils.waitForSingleContainerAssignment(ContainerTestUtils.java:115) any idea why? The proposed solution is to use @DirtiesContext, but this makes tests slower. – la00 Jul 26 '22 at 07:49