-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-2762: Fix Topic Check with Consumer Overrides
* GH-2762: Fix Topic Check with Consumer Overrides Resolves #2762 When checking for missing topics, the admin was created with the raw consumer factory properties and did not apply any overriding consumer properties in the `ContainerProperties`. Apply the overrides before creating the admin. Pull up a method that merges default properties (if any) because we have to iterate over the hash table because the user might have used `put()` instead of `setProperty()` to set properties. **cherry-pick to 2.9.x** * Fix unrelated test. * Fix test class name. * Fix test class name.
- Loading branch information
1 parent
f76bc09
commit 08e76d3
Showing
4 changed files
with
139 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...st/java/org/springframework/kafka/listener/MissingTopicCheckOverrideAdminConfigTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.kafka.listener; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.DirectFieldAccessor; | ||
import org.springframework.core.log.LogAccessor; | ||
import org.springframework.kafka.core.DefaultKafkaConsumerFactory; | ||
import org.springframework.kafka.test.EmbeddedKafkaBroker; | ||
import org.springframework.kafka.test.context.EmbeddedKafka; | ||
import org.springframework.kafka.test.utils.KafkaTestUtils; | ||
|
||
/** | ||
* @author Gary Russell | ||
* @since 3.0 | ||
* | ||
*/ | ||
@EmbeddedKafka(topics = "mtccac") | ||
public class MissingTopicCheckOverrideAdminConfigTests { | ||
|
||
@Test | ||
void configOverride(EmbeddedKafkaBroker broker) { | ||
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("grp", "false", broker); | ||
consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "junkjunk"); | ||
DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps); | ||
ContainerProperties props = new ContainerProperties("mtccac"); | ||
props.setMissingTopicsFatal(true); | ||
props.getKafkaConsumerProperties().setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, | ||
broker.getBrokersAsString()); | ||
KafkaMessageListenerContainer<Integer, String> container = new KafkaMessageListenerContainer<>(cf, props) { | ||
|
||
@Override | ||
public void checkTopics() { | ||
super.checkTopics(); | ||
} | ||
|
||
}; | ||
LogAccessor logger = spy(new LogAccessor(LogFactory.getLog(getClass()))); | ||
new DirectFieldAccessor(container).setPropertyValue("logger", logger); | ||
assertThatNoException().isThrownBy(() -> container.checkTopics()); | ||
verify(logger, never()).error(any(), anyString()); | ||
} | ||
|
||
@Test | ||
void configOverrideDefault(EmbeddedKafkaBroker broker) { | ||
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("grp", "false", broker); | ||
consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "junkjunk"); | ||
DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps); | ||
ContainerProperties props = new ContainerProperties("mtccac"); | ||
props.setMissingTopicsFatal(true); | ||
/* | ||
* Ensure this works if there are property defaults. | ||
* We have to iterate over the hash table because the user might have | ||
* used put() instead of setProperty(). | ||
*/ | ||
Properties defaultProperties = new Properties(); | ||
defaultProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, broker.getBrokersAsString()); | ||
Properties properties = new Properties(defaultProperties); | ||
props.setKafkaConsumerProperties(properties); | ||
KafkaMessageListenerContainer<Integer, String> container = new KafkaMessageListenerContainer<>(cf, props) { | ||
|
||
@Override | ||
public void checkTopics() { | ||
super.checkTopics(); | ||
} | ||
|
||
}; | ||
LogAccessor logger = spy(new LogAccessor(LogFactory.getLog(getClass()))); | ||
new DirectFieldAccessor(container).setPropertyValue("logger", logger); | ||
assertThatNoException().isThrownBy(() -> container.checkTopics()); | ||
verify(logger, never()).error(any(), anyString()); | ||
} | ||
|
||
} |