fix: return EmptyResponse instead of null on exception in NacosLoadBalancer#4288
Merged
uuuyuqi merged 2 commits intoalibaba:2025.1.xfrom Mar 31, 2026
Merged
Conversation
Collaborator
|
Thanks for the fix, the behavior change is correct. One suggestion on tests: current tests verify the private helper via reflection, but the bug is observed on the public reactive path (choose()). Could we add one test that goes through choose() end-to-end (mock supplier -> non-empty instance list -> algorithm throws) and assert we get an EmptyResponse instead of an error/NPE? This would better lock the API-level contract and prevent regressions on the actual call path. |
Add three tests that exercise the public reactive choose() API end-to-end using StepVerifier: - chooseShouldReturnEmptyResponseWhenAlgorithmThrows - chooseShouldReturnValidResponseOnNormalPath - chooseShouldReturnEmptyResponseWhenNoInstances This locks the API-level contract and prevents regressions on the actual call path, as suggested in review.
Contributor
Author
|
@uuuyuqi Thanks for the suggestion! I've added three end-to-end tests that go through the public
The original reflection-based tests are kept alongside as a direct unit-level complement. All 5 tests pass locally. |
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.
Describe what this PR does / why we need it
In
NacosLoadBalancer.getInstanceResponse(), when an exception occurs during instance selection, the method returnsnullinstead of a properEmptyResponse. This violates theResponse<ServiceInstance>API contract and can causeNullPointerExceptionin downstream code that callsresponse.hasServer()or similar methods on the returned value.Does this pull request fix one issue?
No specific issue filed, found during code review.
Describe how you did it
Changed
return nulltoreturn new EmptyResponse()in the catch block ofgetInstanceResponse().Before:
After:
This is consistent with the method's own behavior at line 138 where it already returns
new EmptyResponse()when no instances are available.Describe how to verify it
Run
NacosLoadBalancerErrorHandlingTests— both tests pass.Tests Added
exceptionShouldReturnEmptyResponseNotNull()normalRequestShouldReturnValidResponse()Special notes for reviews
One-line change with minimal risk. The
EmptyResponseis already used in the same method for the empty-instances case.