Skip to content
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

Fix NPE when only batchHandler is specified #281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ private void run(Handler<ConsumerRecord<K, V>> handler, Handler<ConsumerRecords<
this.pollRecords(records -> {

if (records != null && records.count() > 0) {
this.current = records.iterator();
if (handler != null) {
// only set iterator if records are going to be consumed by individual record handler
this.current = records.iterator();
}
if (multiHandler != null) {
multiHandler.handle(records);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,8 @@ public void testConsumerBatchHandler(TestContext ctx) throws Exception {
String consumerId = topicName;
Async batch1 = ctx.async();
AtomicInteger index = new AtomicInteger();
int numMessages = 500;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This resulted in a single iteration of the consumer loop, which effectively hid the NPE. I've bumped numMessages such that it actually triggers the issue.

int batchSize = 500;
int numMessages = 1000;
kafkaCluster.useTo().produceStrings(numMessages, batch1::complete, () ->
new ProducerRecord<>(topicName, 0, "key-" + index.get(), "value-" + index.getAndIncrement()));
batch1.awaitSuccess(10000);
Expand All @@ -1346,7 +1347,7 @@ public void testConsumerBatchHandler(TestContext ctx) throws Exception {
Async batchHandler = ctx.async();
batchHandler.handler(ar -> wrappedConsumer.close());
wrappedConsumer.batchHandler(records -> {
ctx.assertEquals(numMessages, records.size());
ctx.assertEquals(batchSize, records.size());
for (int i = 0; i < records.size(); i++) {
KafkaConsumerRecord<Object, Object> record = records.recordAt(i);
int dec = count.decrementAndGet();
Expand All @@ -1355,8 +1356,10 @@ public void testConsumerBatchHandler(TestContext ctx) throws Exception {
} else {
ctx.assertEquals("key-" + (-1 - dec), record.key());
}
if (dec == 0) {
batchHandler.complete();
}
}
batchHandler.complete();
});
wrappedConsumer.subscribe(Collections.singleton(topicName));
}
Expand Down