cool9850311 opened a new pull request, #24199: URL: https://github.com/apache/kafka/pull/24199
KIP-1306 defines passing `null` to `Consumer#setRebalanceListener` as removing the current listener, so that no callbacks are invoked on future rebalances. The spec sentence lives in `KafkaConsumer#setRebalanceListener`; `Consumer#setRebalanceListener` only `@see`s it. That behaviour had no test coverage: every `Consumer#setRebalanceListener` call site in test code passed a non-null listener, and the places that did assert no callback runs stubbed `ConsumerSubscriptionState#hasRebalanceListener` directly, so the real null-to-`NULL_LISTENER` transition was never exercised. No production changes. ### Tests added * `ConsumerSubscriptionStateTest`: null removes the listener (`hasRebalanceListener` and `listenerName`), the three `onPartitionsXxx` dispatches stay silent and do not throw, never registering a listener behaves the same as setting null, the single-arg test-only overload still rejects null, and a listener can be restored after null. * `ConsumerRebalanceListenerInvokerTest`: a new test class for the invoker, which had none, covering that each of `invokePartitionsAssigned`, `invokePartitionsRevoked` and `invokePartitionsLost` skips the dispatch and records no latency metric when no listener is registered. The subscription state is mocked rather than real on purpose: a real one drops the callbacks itself, which would hide whether the invoker checked at all. * `MockConsumerTest`: no callbacks after null, across `rebalance()` and `losePartitions()`, while the assignment keeps changing. * `KafkaConsumerTest` (CLASSIC): no callbacks on a subsequent rebalance, on `unsubscribe()`, or on `close()`. * `AsyncKafkaConsumerTest` (CONSUMER): a `PartitionsAssignedEvent` with no listener completes its future without emitting a `ConsumerRebalanceListenerCallbackCompletedEvent`, `PartitionsRemovedEvent` for revoked and lost reports back without an error and runs no callback, and `close()` runs no callback. * `PlaintextConsumerCallbackTest`: a classic/async pair asserting that no callbacks fire after null across rebalance, `unsubscribe()`, re-subscribe and `close()`. ### Testing strategy The "no listener, so skip the callback" decision is made at four places. The tests are placed per layer, and the layers are not equivalent: | Gate | Coverage | | --- | --- | | `ConsumerMembershipManager#invokeOnPartitionsRevokedCallback` / `invokeOnPartitionsLostCallback` | Already covered by `ConsumerMembershipManagerTest`; not touched by this PR. | | `AsyncKafkaConsumer#process(PartitionsAssignedEvent)` | New. Removing this gate leaves the suite green except for one of the tests added here, and the only behavioural difference is that the event's future completes one background round trip later, so this is about the event protocol rather than about correctness. | | `ConsumerRebalanceListenerInvoker#invokePartitionsAssigned` / `invokePartitionsRevoked` / `invokePartitionsLost` | New (`ConsumerRebalanceListenerInvokerTest`). In the end-to-end flow this gate is masked by the `ConsumerSubscriptionState#dispatch` early return, so removing it changes nothing observable through a consumer; it has to be tested at the invoker, against a mocked subscription state. | | `ConsumerSubscriptionState#dispatch` | New tests, plus five pre-existing `MockConsumerTest` tests that already fail if the early return is removed. The contribution here is that a failure points at the null-listener semantics rather than at an unrelated `IllegalStateException` from `poll()`. Note the order matters: `dispatch` checks the listener before the `RebalanceConsumer`, and `NULL_LISTENER` has both null, so removing the listener check surfaces as `IllegalStateException: Rebalance consumer has not been initialized`. | Following the convention in these test classes, behaviour common to both group protocols is asserted once per consumer implementation, since the rebalance-related calls differ (see the note on `KafkaConsumerTest#testPauseFlagPreservedForRetainedPartitionAcrossRebalance`). To check that the tests pin the behaviour rather than merely passing, each one was run against deliberately broken versions of the production code: * Making `setRebalanceListener(null)` keep the current listener instead of installing `NULL_LISTENER`: exactly the tests added here fail (9 of them), and nothing else in `:clients:test` does, which is the regression this PR makes visible. * Removing each of the three `ConsumerRebalanceListenerInvoker` gates individually: each one is caught by exactly one of the three new `ConsumerRebalanceListenerInvokerTest` cases, and by nothing else in the module. * Dropping the `dispatch` early return, and inverting the `ConsumerSubscriptionState` listener-removal and listener-restore paths: each break is caught, and the assertions in the new `ConsumerSubscriptionStateTest` cases are the ones that catch the last two. `:clients:test` passes (13845 tests), and the two new integration tests were run three times. 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
