Skip to content

Fix the flaky daysElapsedCountsSpansAcrossBoundaries test - #26459

Merged
jkmassel merged 1 commit into
trunkfrom
bugfix/days-elapsed-test-time-zone-race
Nov 26, 2026
Merged

jkmassel merged 1 commit into
trunkfrom
bugfix/days-elapsed-test-time-zone-race

Conversation

@crazytonyli

Copy link
Copy Markdown
Contributor

Description

CalendarHelpersTests.daysElapsedCountsSpansAcrossBoundaries, added in #25826, has been failing for daysAgo → 30 on CI since November. It only fails between 23:00 and 23:59 Pacific, and only when NSDateHelperTest runs before it.

NSDateHelperTest.init() sets NSTimeZone.default to GMT for the whole test process. Calendar.current follows that default, but TimeZone.autoupdatingCurrent does not. daysElapsedSinceDate found midnight with normalizedDate(), which uses autoupdatingCurrent (Pacific on CI), and then counted days with the receiving calendar (GMT). The 1 November clock change puts the two zones an hour out of step, so 30 days counts as 29.

This PR makes daysElapsedSinceDate use the receiving calendar for both steps. The app is not affected: it never sets NSTimeZone.default, so both zones are the same for the one caller in NotificationsViewController.

The new now parameter is there for the new test. Without a fixed clock, the old failure depends on the wall clock and the process time zone, so no test could catch it reliably.

This PR leaves NSDateHelperTest changing the process time zone, and leaves the other normalizedDate() callers that mix zones the same way. Those can be follow-ups.

What I ran

Claude Code ran -only-testing:WordPressSharedTests/CalendarHelpersTests -only-testing:WordPressSharedTests/NSDateHelperTest on an iOS 26.2 simulator:

Test run with 16 tests in 2 suites passed after 0.009 seconds.

It also reproduced the failure with TZ=America/Anchorage at 23:xx local time. A copy of the test that first sets NSTimeZone.default to GMT, as NSDateHelperTest does, failed 20 of 20 runs before this change and 0 of 20 after.

  • I built this change and ran the affected tests locally

Testing instructions

To reproduce the failure on trunk before 1 December: between 07:00 and 07:59 UTC, run TZ=America/Los_Angeles swift test --filter WordPressSharedTests a few times. It fails when NSDateHelperTest runs first, which happened in about 2 of 3 local runs. At other hours, use a US time zone where the local time is 23:xx, such as America/Chicago from 05:00 UTC.

daysElapsedSinceDate found midnight in the process time zone but counted days in the receiving calendar. When a test changed NSTimeZone.default, Calendar.current followed it and the two zones disagreed across a clock change, making the 30-day case return 29 at 23:xx Pacific. Use the receiving calendar for both steps, and accept an optional now so the behaviour can be tested deterministically.
@dangermattic

Copy link
Copy Markdown
Collaborator
1 Message
📖 This PR is still a Draft: some checks will be skipped.

Generated by 🚫 Danger

@crazytonyli
crazytonyli requested a review from jkmassel November 26, 2026 08:52
@crazytonyli crazytonyli added this to the 27.6 milestone Nov 26, 2026
@crazytonyli
crazytonyli marked this pull request as ready for review November 26, 2026 08:52
@jkmassel
jkmassel enabled auto-merge November 26, 2026 18:27
@jkmassel
jkmassel added this pull request to the merge queue Nov 26, 2026
Merged via the queue into trunk with commit 62a71c5 Nov 26, 2026
23 checks passed
@jkmassel
jkmassel deleted the bugfix/days-elapsed-test-time-zone-race branch November 26, 2026 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants