KAFKA-20677: Enhance test coverage of testCannotSendToInternalTopic#22575
KAFKA-20677: Enhance test coverage of testCannotSendToInternalTopic#22575aravind-kvs wants to merge 1 commit into
Conversation
Replace broken testCannotSendToInternalTopic with four separate test cases covering all scenarios when sending to an internal topic. The original test incorrectly deleted the internal topic before sending, accidentally testing the wrong scenario.
9c96b0e to
490309c
Compare
|
@aravind-kvs thanks for this patch. However, this ticket is already assigned to someone else, so it would be good to ask before opening a PR. Otherwise, it is a bit of a waste to have two talented people working on the same thing |
Thanks for the feedback! |
|
@aravind-kvs This was assigned to me, but since you've already opened a PR, you can take it — I've reassigned it to you. Please check with the assignee before opening a PR next time. I'll review the PR later. |
|
A label of 'needs-attention' was automatically added to this PR in order to raise the |
| @ClusterConfigProperty(key = OFFSETS_TOPIC_REPLICATION_FACTOR_CONFIG, value = "1") | ||
| }) | ||
| public void testCannotSendToInternalTopicWhenAutoCreateTrueAndTopicNotExists(ClusterInstance clusterInstance) throws Exception { | ||
| clusterInstance.deleteTopic(Topic.GROUP_METADATA_TOPIC_NAME); |
There was a problem hiding this comment.
clusterInstance.deleteTopic(Topic.GROUP_METADATA_TOPIC_NAME) is a no-op here — __consumer_offsets doesn't exist yet at this point, and deleteTopic discards the result without awaiting it. Looks like leftover from the old test; safe to drop.
| @ClusterConfigProperty(key = OFFSETS_TOPIC_REPLICATION_FACTOR_CONFIG, value = "1") | ||
| }) | ||
| public void testCannotSendToInternalTopicWhenAutoCreateFalseAndTopicNotExists(ClusterInstance clusterInstance) throws Exception { | ||
| clusterInstance.deleteTopic(Topic.GROUP_METADATA_TOPIC_NAME); |
| @ClusterConfigProperty(key = AUTO_CREATE_TOPICS_ENABLE_CONFIG, value = "true") | ||
| }) | ||
| public void testCannotSendToInternalTopicWhenAutoCreateTrueAndTopicExists(ClusterInstance clusterInstance) throws Exception { | ||
| // explicitly create __consumer_offsets to satisfy the "topic exists" precondition |
There was a problem hiding this comment.
The four new tests repeat a lot of boilerplate — ...TrueAndTopicExists/...FalseAndTopicExists share nearly identical topic-creation code, and all four repeat the same send+assert block with only the expected exception differing. Worth extracting two helpers:
private void createInternalTopic(ClusterInstance clusterInstance) throws Exception {
try (Admin admin = clusterInstance.admin()) {
Map<String, String> topicConfig = clusterInstance.brokers().get(0)
.groupCoordinator()
.groupMetadataTopicConfigs();
admin.createTopics(List.of(new NewTopic(Topic.GROUP_METADATA_TOPIC_NAME, 1, (short) 1).configs(topicConfig)));
clusterInstance.waitTopicCreation(Topic.GROUP_METADATA_TOPIC_NAME, 1);
}
}
private void assertSendToInternalTopicFails(ClusterInstance clusterInstance, Class<? extends Throwable> expectedCause) {
try (Producer<byte[], byte[]> producer = clusterInstance.producer(producerConfig(1))) {
Exception thrown = assertThrows(ExecutionException.class,
() -> producer.send(new ProducerRecord<>(
Topic.GROUP_METADATA_TOPIC_NAME, "test".getBytes(), "test".getBytes())).get());
assertInstanceOf(expectedCause, thrown.getCause(),
() -> "Expected " + expectedCause.getSimpleName() + " but got " + thrown.getCause());
}
}Then each test shrinks to:
@ClusterTest(serverProperties = {@ClusterConfigProperty(key = AUTO_CREATE_TOPICS_ENABLE_CONFIG, value = "true")})
public void testCannotSendToInternalTopicWhenAutoCreateTrueAndTopicExists(ClusterInstance clusterInstance) throws Exception {
createInternalTopic(clusterInstance);
assertSendToInternalTopicFails(clusterInstance, InvalidTopicException.class);
}
JIRA: https://issues.apache.org/jira/browse/KAFKA-20677
Problem
The existing
testCannotSendToInternalTopictest was broken. It attemptedto cover case 2 (auto.create.topics.enable=false, topic exists) but
incorrectly created and then deleted the internal topic before sending,
accidentally testing the wrong scenario.
Changes
Replaced the broken test with four separate test cases covering all scenarios
when sending to an internal topic:
auto.create.topics.enable=true, topic exists →InvalidTopicExceptionauto.create.topics.enable=false, topic exists →InvalidTopicExceptionauto.create.topics.enable=true, topic not exists →InvalidTopicExceptionand topic gets auto-createdauto.create.topics.enable=false, topic not exists →TimeoutExceptionand topic is NOT createdNote on Case 4
The JIRA describes case 4 as throwing
UnknownTopicOrPartitionException.However, the actual broker behavior is to return
UNKNOWN_TOPIC_OR_PARTITIONas a recoverable error, causing the producer to retry until
TimeoutException.The test reflects actual broker behavior.
Test
Ran the new test cases locally and all 4 passed:
./gradlew :clients:clients-integration-tests:test --tests "org.apache.kafka.clients.producer.ProducerFailureHandlingTest.testCannotSendToInternalTopic*"
Results: 4 tests completed, 0 failed
Reviewers: Chia-Ping Tsai chia7712@gmail.com, Ming-Yen Chung mingyen066@gmail.com