Commit 70e4540
authored
MINOR: Ignore unassigned records in MockConsumer (#21631)
In `poll()`, `MockConsumer` iterates the added `records`, and only
checks that they belong to an assigned partition quite late in the
process.
This can cause an error if records were added for a partition that was
later removed from the assignment:
```java
consumer.assign(Arrays.asList(new TopicPartition("t1", 0), new
TopicPartition("t2", 0)));
consumer.addRecord(new ConsumerRecord("t1", 0, 0, "a", 123));
consumer.addRecord(new ConsumerRecord("t2", 0, 0, "b", 123));
consumer.assign(Collections.singleton(new TopicPartition("t1", 0)));
// throws IllegalStateException
consumer.poll(Duration.seconds(1));
```
Moving this check earlier in the process avoids this error in `poll()`,
instead discarding records that do not belong to the assignment.
This enables tests to proactively add all their records and then modify
the assignment multiple times, if necessary, to test specific
combinations of partitions.
This is particularly useful in some of the `streams` tests, where we
need to test with multiple state store changelogs, but the assignment
gets changed internally by Kafka Streams (notably,
`GlobalStateManagerImplTest`).
Reviewers: Bill Bejeck <bbejeck@apache.org>1 parent 4e18307 commit 70e4540
File tree
2 files changed
+3
-3
lines changed- clients/src/main/java/org/apache/kafka/clients/consumer
- streams/src/test/java/org/apache/kafka/streams/processor/internals
2 files changed
+3
-3
lines changedLines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
287 | | - | |
| 287 | + | |
288 | 288 | | |
289 | 289 | | |
290 | 290 | | |
| |||
298 | 298 | | |
299 | 299 | | |
300 | 300 | | |
301 | | - | |
| 301 | + | |
302 | 302 | | |
303 | 303 | | |
304 | 304 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1456 | 1456 | | |
1457 | 1457 | | |
1458 | 1458 | | |
1459 | | - | |
| 1459 | + | |
1460 | 1460 | | |
1461 | 1461 | | |
1462 | 1462 | | |
| |||
0 commit comments