MINOR: restore `testGetAllTopicMetadataShouldNotCreateTopicOrReturnUnknownTopicPartition` (#18633)

Reviewers: Ismael Juma <ismael@juma.me.uk>, Chia-Ping Tsai <chia7712@gmail.com>
This commit is contained in:
TaiJuWu 2025-01-24 01:27:18 +08:00 committed by GitHub
parent 01afba8fdb
commit ce4eeaa379
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 41 additions and 0 deletions

View File

@ -3562,6 +3562,47 @@ class KafkaApisTest extends Logging {
assertEquals(Set(0), response.brokers.asScala.map(_.id).toSet)
}
/**
* Metadata request to fetch all topics should not result in the followings:
* 1) Auto topic creation
* 2) UNKNOWN_TOPIC_OR_PARTITION
*
* This case is testing the case that a topic is being deleted from MetadataCache right after
* authorization but before checking in MetadataCache.
*/
@Test
def testGetAllTopicMetadataShouldNotCreateTopicOrReturnUnknownTopicPartition(): Unit = {
// Setup: authorizer authorizes 2 topics, but one got deleted in metadata cache
metadataCache = mock(classOf[KRaftMetadataCache])
when(metadataCache.getAliveBrokerNodes(any())).thenReturn(List(new Node(brokerId,"localhost", 0)))
when(metadataCache.getRandomAliveBrokerId).thenReturn(None)
// 2 topics returned for authorization in during handle
val topicsReturnedFromMetadataCacheForAuthorization = Set("remaining-topic", "later-deleted-topic")
when(metadataCache.getAllTopics()).thenReturn(topicsReturnedFromMetadataCacheForAuthorization)
// 1 topic is deleted from metadata right at the time between authorization and the next getTopicMetadata() call
when(metadataCache.getTopicMetadata(
ArgumentMatchers.eq(topicsReturnedFromMetadataCacheForAuthorization),
any[ListenerName],
anyBoolean,
anyBoolean
)).thenReturn(Seq(
new MetadataResponseTopic()
.setErrorCode(Errors.NONE.code)
.setName("remaining-topic")
.setIsInternal(false)
))
val response = sendMetadataRequestWithInconsistentListeners(new ListenerName("PLAINTEXT"))
val responseTopics = response.topicMetadata().asScala.map { metadata => metadata.topic() }
// verify we don't create topic when getAllTopicMetadata
verify(autoTopicCreationManager, never).createTopics(any(), any(), any())
assertEquals(List("remaining-topic"), responseTopics)
assertTrue(response.topicsByError(Errors.UNKNOWN_TOPIC_OR_PARTITION).isEmpty)
}
@Test
def testUnauthorizedTopicMetadataRequest(): Unit = {
// 1. Set up broker information