-
Notifications
You must be signed in to change notification settings - Fork 1.1k
GH-3696: DeserializationEx support for KafkaMS #8689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -24,6 +24,8 @@ | |
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.consumer.ConsumerRebalanceListener; | ||
import org.apache.kafka.common.TopicPartition; | ||
import org.apache.kafka.common.serialization.Deserializer; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.integration.channel.NullChannel; | ||
|
@@ -32,11 +34,17 @@ | |
import org.springframework.kafka.core.DefaultKafkaProducerFactory; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.kafka.listener.ConsumerProperties; | ||
import org.springframework.kafka.support.serializer.DeserializationException; | ||
import org.springframework.kafka.support.serializer.ErrorHandlingDeserializer; | ||
import org.springframework.kafka.test.utils.KafkaTestUtils; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.support.GenericMessage; | ||
import org.springframework.util.ClassUtils; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.mockito.Mockito.mock; | ||
|
||
/** | ||
* @author Gary Russell | ||
|
@@ -50,9 +58,17 @@ class MessageSourceIntegrationTests { | |
|
||
static final String TOPIC1 = "MessageSourceIntegrationTests1"; | ||
|
||
static final String TOPIC2 = "MessageSourceIntegrationTests2"; | ||
|
||
static String brokers; | ||
|
||
@BeforeAll | ||
static void setup() { | ||
brokers = System.getProperty("spring.global.embedded.kafka.brokers"); | ||
} | ||
|
||
@Test | ||
void testSource() throws Exception { | ||
String brokers = System.getProperty("spring.global.embedded.kafka.brokers"); | ||
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(brokers, "testSource", "false"); | ||
consumerProps.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 2); | ||
consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||
|
@@ -122,4 +138,66 @@ public void onPartitionsAssigned(Collection<TopicPartition> partitions) { | |
template.destroy(); | ||
} | ||
|
||
@Test | ||
void deserializationErrorIsThrownFromSource() throws Exception { | ||
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(brokers, "testErrorChannelSource", "false"); | ||
consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||
consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ErrorHandlingDeserializer.class); | ||
consumerProps.put(ErrorHandlingDeserializer.VALUE_DESERIALIZER_CLASS, FailingDeserializer.class); | ||
|
||
DefaultKafkaConsumerFactory<Integer, String> consumerFactory = new DefaultKafkaConsumerFactory<>(consumerProps); | ||
ConsumerProperties consumerProperties = new ConsumerProperties(TOPIC2); | ||
CountDownLatch assigned = new CountDownLatch(1); | ||
consumerProperties.setConsumerRebalanceListener( | ||
new ConsumerRebalanceListener() { | ||
|
||
@Override | ||
public void onPartitionsRevoked(Collection<TopicPartition> partitions) { | ||
} | ||
|
||
@Override | ||
public void onPartitionsAssigned(Collection<TopicPartition> partitions) { | ||
assigned.countDown(); | ||
} | ||
|
||
}); | ||
|
||
consumerProperties.setPollTimeout(10); | ||
|
||
KafkaMessageSource<Integer, String> source = new KafkaMessageSource<>(consumerFactory, consumerProperties); | ||
source.setBeanClassLoader(ClassUtils.getDefaultClassLoader()); | ||
source.setBeanFactory(mock()); | ||
source.afterPropertiesSet(); | ||
source.start(); | ||
|
||
Map<String, Object> producerProps = KafkaTestUtils.producerProps(brokers); | ||
DefaultKafkaProducerFactory<Object, Object> producerFactory = new DefaultKafkaProducerFactory<>(producerProps); | ||
KafkaTemplate<Object, Object> template = new KafkaTemplate<>(producerFactory); | ||
|
||
String testData = "test data"; | ||
template.send(TOPIC2, testData); | ||
|
||
source.receive(); // Trigger Kafka Consumer creation and poll() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not entirely sure, but the record might be returned by this initial poll. It would be safer to remove this and move the following assertion into the |
||
assertThat(assigned.await(10, TimeUnit.SECONDS)).isTrue(); | ||
|
||
await().untilAsserted(() -> | ||
assertThatExceptionOfType(DeserializationException.class) | ||
.isThrownBy(source::receive) | ||
.hasFieldOrPropertyWithValue("data", testData.getBytes()) | ||
.withMessage("failed to deserialize") | ||
.withStackTraceContaining("failed deserialization")); | ||
|
||
source.destroy(); | ||
template.destroy(); | ||
} | ||
|
||
public static class FailingDeserializer implements Deserializer<String> { | ||
|
||
@Override | ||
public String deserialize(String topic, byte[] data) { | ||
throw new RuntimeException("failed deserialization"); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, this is now the default in KTU.