Fix: Stabilize ExtensionLoaderTest.testInjectExtension
What is the purpose of the change?
This PR fixes order-dependent flakiness (OD-Vic) in ExtensionLoaderTest.testInjectExtension
This test fails under randomized test execution orders due to multiple DemoImpl instances being registered in the shared ScopeBeanFactory of ApplicationModel.
Root Cause
testInjectExtension and related tests (testGetOrDefaultExtension) both interacted with the same shared ScopeBeanFactory.
Because bean registration was not idempotent, repeated or parallel test execution sometimes resulted in duplicate DemoImpl entries, leading to inconsistent injection results or assertion failures.
What's Changed
- Added defensive logic to check existing DemoImpl beans before registering a new one.
- Made registration idempotent by registering only one instance (demo-test-singleton) if none exists.
- Ensured subsequent tests reuse the same canonical bean reference.
- Updated testGetOrDefaultExtension to follow the same consistent setup.
These changes eliminate shared-state interference and make the test deterministic under any order or random seed.
Verification
You can try running the loop below on pre-fix and post-fix code:
fail=0; total=0
for s in 7138 7351 7892 11170 15857 15869 17721 20421 21699 22631 24059 29905; do
echo "SEED=$s"
total=$((total+1))
SEED=$s ./mvnw -pl dubbo-common -DfailIfNoTests=true -DforkCount=1 -DreuseForks=false \
-Dsurefire.runOrder=random \
-Djunit.jupiter.testclass.order.default=org.junit.jupiter.api.ClassOrderer\$Random \
-Djunit.jupiter.testmethod.order.default=org.junit.jupiter.api.MethodOrderer\$Random \
-Djunit.jupiter.testclass.order.random.seed="$s" \
-Djunit.jupiter.testmethod.order.random.seed="$s" \
-Dtest=org.apache.dubbo.common.extension.ExtensionLoaderTest test \
>/dev/null || { echo "** FAILED on SEED=$s **"; fail=$((fail+1)); }
done
echo
echo "===== SUMMARY ====="
echo "Total seeds: $total"
echo "Failed: $fail"
echo "Passed: $((total - fail))"
echo "==================="
The seeds were found by using a script I developed locally. Sometimes not all seeds fail on execution, but some will always fail on pre-fix code. However, on post-fix code none of the seeds fail.
Checklist
- [x] Make sure there is a GitHub_issue field for the change.
- [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
- [x] Write necessary unit-test to verify your logic correction. If the new feature or significant change is committed, please remember to add sample in dubbo samples project.
- [x] Make sure gitHub actions can pass. Why the workflow is failing and how to fix it?
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 60.76%. Comparing base (9b32dc1) to head (f3cbe95).
Additional details and impacted files
@@ Coverage Diff @@
## 3.3 #15744 +/- ##
============================================
- Coverage 60.76% 60.76% -0.01%
Complexity 11698 11698
============================================
Files 1938 1938
Lines 88646 88646
Branches 13379 13379
============================================
- Hits 53868 53865 -3
- Misses 29250 29258 +8
+ Partials 5528 5523 -5
| Flag | Coverage Δ | |
|---|---|---|
| integration-tests-java21 | 32.37% <ø> (+0.01%) |
:arrow_up: |
| integration-tests-java8 | 32.44% <ø> (+<0.01%) |
:arrow_up: |
| samples-tests-java21 | 32.05% <ø> (-0.01%) |
:arrow_down: |
| samples-tests-java8 | 29.71% <ø> (+0.01%) |
:arrow_up: |
| unit-tests-java11 | 59.06% <ø> (-0.02%) |
:arrow_down: |
| unit-tests-java17 | 58.53% <ø> (-0.02%) |
:arrow_down: |
| unit-tests-java21 | 58.57% <ø> (-0.01%) |
:arrow_down: |
| unit-tests-java25 | 58.52% <ø> (-0.02%) |
:arrow_down: |
| unit-tests-java8 | 59.07% <ø> (-0.01%) |
:arrow_down: |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
fail=0; total=0
for s in 7138 7351 7892 11170 15857 15869 17721 20421 21699 22631 24059 29905; do
echo "SEED=$s"
total=$((total+1))
SEED=$s ./mvnw -pl dubbo-common -DfailIfNoTests=true -DforkCount=1 -DreuseForks=false \
-Dsurefire.runOrder=random \
-Djunit.jupiter.testclass.order.default=org.junit.jupiter.api.ClassOrderer\$Random \
-Djunit.jupiter.testmethod.order.default=org.junit.jupiter.api.MethodOrderer\$Random \
-Djunit.jupiter.testclass.order.random.seed="$s" \
-Djunit.jupiter.testmethod.order.random.seed="$s" \
-Dtest=org.apache.dubbo.common.extension.ExtensionLoaderTest test \
>/dev/null || { echo "** FAILED on SEED=$s **"; fail=$((fail+1)); }
done
echo
echo "===== SUMMARY ====="
echo "Total seeds: $total"
echo "Failed: $fail"
echo "Passed: $((total - fail))"
echo "==================="
It might be better that adding an unit test to prove this PR.
Thanks for the review!
I believe there might be a small misunderstanding...this PR doesn’t introduce new behavior that needs its own unit test. It fixes order-dependent flakiness in the existing test ExtensionLoaderTest.testInjectExtension
Before this change, the ExtensionLoader classes' tests, particularly testGetOrDefaultExtension (polluter) and testInjectExtension (victim) interfered with each other via shared ScopeBeanFactory state. Running the shell command below exposes this flakiness:
fail=0; total=0
for s in 7138 7351 7892 11170 15857 15869 17721 20421 21699 22631 24059 29905; do
echo "SEED=$s"
total=$((total+1))
SEED=$s ./mvnw -pl dubbo-common -DfailIfNoTests=true -DforkCount=1 -DreuseForks=false \
-Dsurefire.runOrder=random \
-Djunit.jupiter.testclass.order.default=org.junit.jupiter.api.ClassOrderer\$Random \
-Djunit.jupiter.testmethod.order.default=org.junit.jupiter.api.MethodOrderer\$Random \
-Djunit.jupiter.testclass.order.random.seed="$s" \
-Djunit.jupiter.testmethod.order.random.seed="$s" \
-Dtest=org.apache.dubbo.common.extension.ExtensionLoaderTest test \
>/dev/null || { echo "** FAILED on SEED=$s **"; fail=$((fail+1)); }
done
echo
echo "===== SUMMARY ====="
echo "Total seeds: $total"
echo "Failed: $fail"
echo "Passed: $((total - fail))"
echo "==================="
These seeds reliably reproduce the flakiness on the pre-fix code.
I attempted to simplify this to a single command (e.g., running the polluter → victim pair deterministically), but Surefire didn’t consistently preserve the inter-test interference needed to trigger the bug. The above loop was therefore the most reliable way to demonstrate the issue.
After this fix, none of these seeds fail. The test is now stable under any random execution order.
i mean you could write an junit test by calling java Process to run your script and assert its result.
I will look into this and add a comment when I made changes
I will look into this and add a comment when I made changes
Any progress?