Skip to content

Conversation

@jaclync
Copy link
Contributor

@jaclync jaclync commented Sep 17, 2025

For WOOMOB-1326

Just one review is required.

Description

This PR addresses a flaky test in the Alamofire networking layer that were causing intermittent test failures due to race conditions. It also updates a similar test case to not make an external network request. The changes address two main issues:

  1. Removed flaky concurrent request test: The test AlamofireNetworkTests.test_concurrent_requests_fail_with_sessionDeinitialized_error_when_ensuresSessionManagerIsInitialized_is_false was removed because it relied on a race condition between Alamofire.AFError.sessionDeinitialized and network errors that could be thrown simultaneously, making the test unreliable.
  • I tried a few approaches like updating the concurrent requests to a URL that never returns, or a localhost address with a quick timeout, but the life cycle of the Alamofire.Session is unpredictable and Alamofire.AFError.sessionDeinitialized isn't consistently thrown right away.
  • I also tried replacing the alamofireSession: Alamofire.Session from a lazy var to a let, but it results in a crash when running test_authenticationMode_changes_to_appPasswordsWithJetpack_when_app_password_switching_enabled on this line that updates the fixed RequestProcessor:
NetworkingCore`async function pointer to NetworkingCore.AlamofireNetwork.responseDataAndHeaders(for: Alamofire.URLRequestConvertible) async throws -> (Foundation.Data, Swift.Optional<Swift.Dictionary<Swift.String, Swift.String>>):
->  0x10e55f410 <+0>: .long  0xfff70450                ; unknown opcode
    0x10e55f414 <+4>: udf    #0x160

But I ran out of time to look further, and didn't have enough context of the recent authentication changes from Kiwi to determine the cause. It felt a bit concerning that the test case is relying on the session being a lazy variable, even though that test case actually DI'ed a mock session. It might just be the test setup, though.

  1. Updated concurrent request test to use internal network call: The remaining test AlamofireNetworkTests.test_concurrent_requests_do_not_fail_with_sessionDeinitialized_error_when_ensuresSessionManagerIsInitialized_is_true now uses a direct URLRequest to localhost with a very short timeout instead of a JetpackRequest, making it more predictable and eliminating the race condition.

Steps to reproduce

CI passing is sufficient, but feel free to run AlamofireNetworkTests repeatedly locally.

Testing information

I ran AlamofireNetworkTests repeatedly (100 times) multiple times locally and esnured that they passed. I was able to reproduce the flakiness by running AlamofireNetworkTests repeatedly (100 times) once before the PR changes.


  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

… condition `Alamofire.AFError.sessionDeinitialized` and network error could be thrown at the same time.
…l_with_sessionDeinitialized_error_when_ensuresSessionManagerIsInitialized_is_true`.
@jaclync jaclync added type: technical debt Represents or solves tech debt of the project. category: unit tests Related to unit testing. labels Sep 17, 2025
@jaclync jaclync added this to the 23.3 milestone Sep 17, 2025
@jaclync jaclync changed the title Fix flaky Alamofire networking tests Address flaky Alamofire network tests Sep 17, 2025
@jaclync jaclync changed the title Address flaky Alamofire network tests Address flaky Alamofire network tests on Alamofire.AFError.sessionDeinitialized error from concurrent requests Sep 17, 2025
@jaclync jaclync changed the title Address flaky Alamofire network tests on Alamofire.AFError.sessionDeinitialized error from concurrent requests Address flaky Alamofire network test on Alamofire.AFError.sessionDeinitialized error from concurrent requests Sep 17, 2025
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Sep 17, 2025

App Icon📲 You can test the changes from this Pull Request in WooCommerce iOS Prototype by scanning the QR code below to install the corresponding build.

App NameWooCommerce iOS Prototype
Build Numberpr16137-05c3061
Version23.3
Bundle IDcom.automattic.alpha.woocommerce
Commit05c3061
Installation URL3jdh9qqbkfnmo
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@iangmaia iangmaia modified the milestones: 23.3, 23.4 Sep 19, 2025
@iangmaia
Copy link
Contributor

Version 23.3 has now entered code-freeze, so the milestone of this PR has been updated to 23.4.

…mofireNetworkTests

# Conflicts:
#	Modules/Tests/NetworkingTests/Network/AlamofireNetworkTests.swift
@jaclync jaclync requested a review from itsmeichigo September 23, 2025 05:07
@itsmeichigo itsmeichigo self-assigned this Sep 24, 2025
@itsmeichigo
Copy link
Contributor

Regarding this part:

I also tried replacing the alamofireSession: Alamofire.Session from a lazy var to a let, but it results in a crash when running test_authenticationMode_changes_to_appPasswordsWithJetpack_when_app_password_switching_enabled on this line that updates the fixed RequestProcessor

This line is no longer in trunk after the changes from last week. Do you think it's worth trying your approach again to update the test_concurrent_requests_fail_with_sessionDeinitialized_error_when_ensuresSessionManagerIsInitialized_is_false test?

@jaclync
Copy link
Contributor Author

jaclync commented Oct 1, 2025

Regarding this part:

I also tried replacing the alamofireSession: Alamofire.Session from a lazy var to a let, but it results in a crash when running test_authenticationMode_changes_to_appPasswordsWithJetpack_when_app_password_switching_enabled on this line that updates the fixed RequestProcessor

This line is no longer in trunk after the changes from last week. Do you think it's worth trying your approach again to update the test_concurrent_requests_fail_with_sessionDeinitialized_error_when_ensuresSessionManagerIsInitialized_is_false test?

Nice, thanks for sharing the update. I replaced the lazy var Alamofire.Session with a let in e1c5a7d. Unit tests passing locally and on CI, the PR is ready for another look.

Copy link
Contributor

@itsmeichigo itsmeichigo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! The tests pass locally for me too. I also did some regression tests and confirmed that network switching still works correctly after the change to alamofireSession. :shipit:

@jaclync
Copy link
Contributor Author

jaclync commented Oct 1, 2025

Thanks for the review & additional testing @itsmeichigo, I also tested again for concurrent requests in POS catalog sync for stores with big & small catalog size. Merging it now.

@jaclync jaclync merged commit 0a622d9 into trunk Oct 1, 2025
13 checks passed
@jaclync jaclync deleted the feat/WOOMOB-1326-flaky-concurrent-tests-AlamofireNetworkTests branch October 1, 2025 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: unit tests Related to unit testing. type: technical debt Represents or solves tech debt of the project.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants