Skip to content

Fix flaky testCardArtImage_returnsNilWhenImageNotCached test 🪿✨ - #6872

Open
joyceqin-stripe wants to merge 1 commit into
masterfrom
joyceqin/cardartimage-nil-cached-stppaymentmethod
Open

Fix flaky testCardArtImage_returnsNilWhenImageNotCached test 🪿✨#6872
joyceqin-stripe wants to merge 1 commit into
masterfrom
joyceqin/cardartimage-nil-cached-stppaymentmethod

Conversation

@joyceqin-stripe

Copy link
Copy Markdown
Collaborator

Minion run

Summary

Pass isTesting: true when constructing DownloadManager in STPPaymentMethodCardArtImageTest.setUp() so the manager uses the test's explicit memory-only URLCache instead of a disk-backed one.

Motivation

testCardArtImage_returnsNilWhenImageNotCached was flaky because DownloadManager.init (with isTesting: false, the default) unconditionally replaces the provided URLSessionConfiguration's urlCache with a disk-backed URLCache at a fixed STPCache path — discarding the memory-only cache the test configured. When testCardArtImage_returnsImageWhenCached ran first, it seeded image data to that shared disk cache. A subsequent run of testCardArtImage_returnsNilWhenImageNotCached would create a new URLCache pointing to the same path, and URLCache.removeAllCachedResponses() (called in resetCache()) does not guarantee synchronous disk removal, so promoteFromDiskCache could still find and return the stale image — causing the XCTAssertNil to fail.

Passing isTesting: true skips the disk cache setup entirely. The DownloadManager then uses the URLCache(memoryCapacity: 5_000_000, diskCapacity: 0) already set on the config, eliminating the disk I/O race.

Testing

Existing tests in STPPaymentMethodCardArtImageTest cover the fix — testCardArtImage_returnsNilWhenImageNotCached (previously flaky) and testCardArtImage_returnsImageWhenCached (verifies the positive case still works with the memory cache seeded via seedURLCache).

No tests were newly ignored.

Changelog

No user-facing change.

When DownloadManager is initialized with isTesting: false (the default),
it overrides the provided URLSessionConfiguration's urlCache with a
disk-backed URLCache at a fixed path. This caused test ordering flakiness:
testCardArtImage_returnsImageWhenCached seeds the disk cache, and a
subsequent testCardArtImage_returnsNilWhenImageNotCached may read that
stale disk data before removeAllCachedResponses() fully completes.

Pass isTesting: true so the DownloadManager uses the explicit memory-only
URLCache (diskCapacity: 0) the test sets up, eliminating the disk I/O
race condition.

Committed-By-Agent: goose
@joyceqin-stripe
joyceqin-stripe marked this pull request as ready for review August 7, 2026 19:57
@joyceqin-stripe
joyceqin-stripe requested review from a team as code owners August 7, 2026 19:57
@joyceqin-stripe
joyceqin-stripe enabled auto-merge (squash) August 7, 2026 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant