@W-22952929: [Android] Stabilize flaky test testNullsInSelectedLoginServer#2926
Merged
Conversation
…erver (Fix postValue race in getSelectedLoginServer)
brandonpage
approved these changes
Jun 11, 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.
Summary
Fixes a LiveData
postValue/getValuerace condition inLoginServerManager.getSelectedLoginServer()that causedtestNullsInSelectedLoginServerto fail intermittently on Firebase Test Lab under device load.Related: PR #2922 (stabilized
LoginViewModelTestcoroutine races — same flaky-test stabilization effort, different root cause)Root Cause
getSelectedLoginServer()usedselectedServer.postValue(...)to update LiveData, then immediately read fromselectedServer.getValue()for both its return value and a subsequentsetSelectedLoginServer()call. SincepostValueis asynchronous (scheduled on the main looper),getValue()returns stale data until the post is dispatched — a window that widens under CI device load.Three instances of this race existed in the method:
Fallback path (lines 228–233): When the selected server is null/unavailable and the method falls back to the first server in the list,
postValue(loginServers.get(0))is followed bysetSelectedLoginServer(selectedServer.getValue()). ThegetValue()returnsnull(nothing was ever posted before), sosetSelectedLoginServerno-ops (null guard) and nothing gets persisted.Return value (line 236):
return selectedServer.getValue()reads from LiveData instead of returning the locally-computed result. In the fallback path this returnsnull; in the "valid and available" path it returns the previous server value beforepostValuelands.Valid-and-available path (lines 214–219): The method correctly computes
selectedLoginServerlocally but discards it at the return statement in favor ofselectedServer.getValue(), which may still hold the prior value ifpostValuehasn't dispatched.Fix
The method now uses the local
selectedLoginServervariable as the single source of truth and returns it directly. LiveData is used exclusively as a notification channel for observers — never read back for synchronous logic:Test Change
testNullSelectedLoginServerpreviously mocked the server list SharedPreferences with null name/URL entries (producing an emptygetLoginServers()result), yet asserted thatgetSelectedLoginServer()returns "Production". This only passed because the constructor'sinitSharedPrefFile()calledsetSelectedLoginServer(...)which synchronously setselectedServer's LiveData value, and the racyreturn selectedServer.getValue()happened to read it back.The mock now stubs the server list with "Production" / "https://login.salesforce.com" — reflecting the post-initialization state where
initSharedPrefFile()has populated servers fromservers.xml. This matches what real SharedPreferences would contain after the constructor completes and tests the intended scenario: "selected server prefs are null, but the server list is populated, so fall back to the first available server."Backward Compatibility
getSelectedLoginServer()is a public API, but its contract ("returns the selected login server, defaulting to the first available") is unchangednullor a stale valueselectedServer) continue to receive the samepostValuenotifications as beforeTest Plan
testNullsInSelectedLoginServerpasses 15+ consecutive runs locally (API 35 emulator)LoginServerManagerMockTestclass (21 tests) passes 10 consecutive runs with no failuresLoginServerManagerTestclass (22 tests) passes with no regressionsThis response was generated by an AI agent on behalf of @JohnsonEricAtSalesforce.