-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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][tests] Fix Mockito mocks memory leak #17851
[fix][tests] Fix Mockito mocks memory leak #17851
Conversation
dd5e115
to
9b70123
Compare
pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java
Outdated
Show resolved
Hide resolved
…MockedPulsarServiceBaseTest
pulsar-broker/src/test/java/org/apache/pulsar/broker/BrokerTestUtil.java
Show resolved
Hide resolved
buildtools/src/main/java/org/apache/pulsar/tests/MockitoThreadLocalStateCleaner.java
Show resolved
Hide resolved
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.
LGTM.
Since we already hard depend on org.mockito.internal.util.MockUtil
, the comment above is not a blocker.
Perhaps we can report this OOM to the mockito community for a solution in idiom. Since they mark these methods as not recommended they could have some insights here :) |
There's nothing here that is caused by a Mockito bug. |
I have some experience in asking upstream experts for best practices. But let's say I should volunteer to report the case instead of requesting others. |
In this case, the reason for the memory leaks is the misuse of Mockito in the Pulsar internal test framework. org.apache.pulsar.tests.MockitoThreadLocalStateCleaner will log warnings about "Invalid usage of Mockito detected on thread ..." to help find the locations of the misuse of Mockito in Pulsar tests. I guess it would be helpful to have some support for checks like this directly in Mockito so that it would be possible to add such validation without using reflection. @tisonkun Since you seem to care about this topic, it would be useful to fix issues in Pulsar test code one issue at a time. |
* Call cleanup method in finally block to ensure it's not skipped * Clear invocations for the mocks that are left around without cleanup * Cleanup PulsarService and PulsarAdmin mocks/spies in MockedPulsarServiceBaseTest * Don't record invocations at all for PulsarService and PulsarAdmin in MockedPulsarServiceBaseTest * Don't record invocations for spies by default * Simplify reseting mocks * Fix PersistentTopicTest * Fix TokenExpirationProducerConsumerTest * Fix SimpleLoadManagerImplTest * Fix FilterEntryTest (cherry picked from commit d4893d1)
Fixes #17714
Motivation
There's a memory leak when using Mockito mocks since all invocations get recorded.
Modifications
Add multiple solutions for making sure that mocks are cleaned up properly.
Modify MockedPulsarServiceBaseTest to clean up PulsarService and PulsarAdmin mocks since the detected memory leaks are caused by these mocks.
Documentation
doc-required
(Your PR needs to update docs and you will update later)
doc-not-needed
(Please explain why)
doc
(Your PR contains doc changes)
doc-complete
(Docs have been already added)
Matching PR in forked repository
PR in forked repository: lhotari#92