[fix][test] Fix flaky PersistentTopicsTest setup caused by concurrent Mockito stubbing#26083
Open
void-ptr974 wants to merge 1 commit into
Open
[fix][test] Fix flaky PersistentTopicsTest setup caused by concurrent Mockito stubbing#26083void-ptr974 wants to merge 1 commit into
void-ptr974 wants to merge 1 commit into
Conversation
97ef426 to
5f39bea
Compare
merlimat
approved these changes
Jun 25, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
Fixes #26076.
PersistentTopicsTest.setupstarts a real test broker throughsuper.internalSetup(). After the broker is started, the test replaces the broker resources with Mockito stubbing:This is fragile because
pulsaris a spy of a livePulsarService. Once the broker has started, broker services and background tasks can call methods on the samePulsarServiceinstance concurrently. Mockito can usually handle concurrent method invocations on an already-configured mock, but it is not safe to create or modify stubs while another thread is invoking the same mock.The failure stack points at this setup-time stubbing:
When another broker thread calls the spy while the setup thread is in the middle of
doReturn(...).when(pulsar).getPulsarResources(), Mockito can observe an incomplete stubbing operation. That explains both failure modes from the issue:AssertionErrorfrom Mockito internals while recording the method for stubbing.UnfinishedStubbingExceptionduring cleanup because Mockito still sees the interrupted or incomplete stubbing state.There are also two test methods that later update resource-related stubs through chains such as:
Those have the same basic problem: they touch the live
PulsarServicespy after the broker has started, and they use chained Mockito stubbing around objects that can also be used by broker code.Modifications
pulsar.getPulsarResources()stub after broker startup.PulsarResourcesandTopicResourcesspies, configure their stubs first, and then install the replacement resources on the testPulsarService.namespaceResourcesOverrideselects a mockedNamespaceResourcesonly for the test that needs custom namespace policies.listPersistentTopicsAsyncHandlerreturns custom topic listings only for the test namespace that needs them, otherwise it falls back to the real method.cleanup()to avoid leaking test-specific behavior to the next test method.Result
The test still exercises the same admin paths, but Mockito stubbing is no longer created or modified on the live
PulsarServicespy while broker threads may be using it. Test-specific behavior is changed by updating volatile references, which is safe for concurrent readers and avoids Mockito unfinished stubbing state.Documentation
docdoc-requireddoc-not-neededdoc-completeVerifications
./gradlew :pulsar-broker:test --tests org.apache.pulsar.broker.admin.PersistentTopicsTest :pulsar-broker:checkstyleTest