Skip to content

fix: [SDK-5816] fix flaky unit tests and a login token race - #1816

Merged
fadi-george merged 14 commits into
mainfrom
fadi/sdk-5816
Nov 21, 2026
Merged

fadi-george merged 14 commits into
mainfrom
fadi/sdk-5816

Conversation

@fadi-george

@fadi-george fadi-george commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

One Line Summary

Fix flaky unit tests so CI passes reliably, and two SDK races found along the way.

Details

Motivation

These tests fail intermittently in CI on unrelated PRs (#1815 failed on the first two in separate attempts, and this PR's own CI hit the third).

Scope

  • OneSignalNotificationsTests.setBadgeCountAndWait waited for the system setBadgeCount completion, which can exceed 5s on a freshly booted simulator. The swizzled setter caches the count synchronously before calling the system, so the helper now waits on the cached count.
  • UserJwtLifecycleTests.testLoggingInWithATokenAnswersAPendingAskForThatUser asserted the token ask had already happened after a fixed 0.5s wait. It now polls pendingTokenAsks(), which reads the ask without consuming it (unlike askForToken).
  • While investigating IamFetchIdentityVerificationTests.testARejectedFetchParksWithoutReportingTheTokenItUsed, found a real SDK race: login published the new user and stored its token a moment later, so a reader on another queue (the IAM fetch runs on a global queue) could find the user without a token and ask the app for one it had just supplied. setNewInternalUser now sets the token on the identity model before publishing it. The existing storeJwtOrRearmAsk still runs to clear the ask and release held work. That IAM test's real cause turned out to be the cross-test leak described below.
  • OneSignalUserTests.testBasicCombiningUserUpdateDeltas_resultsInOneRequest: startNewSession() unpaused the operation repo before it enqueued its session_count Delta, so a poll in between sent the queue without it and split the session's update into two Requests. startNewSession() now queues the session's Deltas first, then retries pending user requests, unpauses, and flushes. The retry stays right before the unpause, as before, so a Create User that fails again still pauses the repo. In production the old order only cost an extra Request.
  • SubscriptionUpdateRaceTests waited on the persisted update queue, which every subscription executor writes, including ones earlier tests left running. The tests now read their own executor's in-memory queue (dispatchQueue and updateRequestQueue are now internal, no behavior change).
  • UserStateReportingTests.testAnIdentifyUserThatCompletesAfterAUserSwitchDoesNotReportThatUser and the IAM fetch test above never failed locally, so their failure messages now report the models, tokens, and requests involved.
  • The JWT pending-ask test still failed 2 of 14 CI runs. Its new failure message showed a Create User for user A signed with token-b, a token only an earlier test uses: that test's executors persisted user A's state after teardown cleared UserDefaults, and the next start uncached it, so the new Create User signed with the stale token and never asked. The test now logs in as a user no other test uses. The cross-test leak itself is test-infra only and not fixed here.
  • The badge test no longer verifies the system call completes, which it wasn't meant to test.
  • IamFetchIdentityVerificationTests.testARejectedFetchParksWithoutReportingTheTokenItUsed: its diagnostic showed the JWT listener hearing an ask for user A before this test had sent anything, raised by work an earlier test left running (the same leak). It and testARejectedFetchRefetchesWhenAReplacementTokenAlreadyLanded now count only the asks the fetch step itself reports. Both still fail if a rejected fetch reports its token (checked with a temporary mutation).
  • OneSignalCoreMocks.waitUntil defaults to 15s instead of 5s. A met condition returns at once, so this only matters when a CI runner stalls, which failed an unrelated custom events wait.
  • EarlyTriggerTrackingTests failed once with the first IAM fetch never completing, outside any runner stall. Its waits now report where the fetch stopped (IDs, deferral, RYW tokens, requests).

Testing

Unit testing

  • Badge and JWT suites: 20 iterations with -run-tests-until-failure on an iOS 26.5 simulator, 520 test runs, 0 failures.
  • Login race: testLoginPublishesTheNewUserWithTheTokenItLoggedInWith logs in and records the identity model's token at the moment the store adds it, so it fails if the token is set after the user is published. The gap is too narrow to hit in a test on its own, so I confirmed the race by widening it with a temporary 1ms sleep under a 200-login stress loop: without the fix a reader asked the app for a token, with the fix it did not.
  • CI: 10 consecutive passing runs of Build and Test on 4a043a7.
  • User and IAM test bundles: 3 iterations, all pass except UserConcurrencyTests.testIdentityExecutorConcurrency, which fails when repeated back to back with or without this change (19 of 20 without it).

Manual testing

Not applicable, covered by unit tests.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • User login / Identity Verification
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fadi-george
fadi-george requested a review from nan-li November 20, 2026 00:00
A reader on another queue, like the in-app message fetch, could catch the
new user before its token was stored and ask the app for one it had just
supplied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fadi-george fadi-george changed the title test: [SDK-5816] fix flaky badge and JWT pending-ask unit tests fix: [SDK-5816] fix flaky unit tests and a login token race Nov 20, 2026
fadi-george and others added 6 commits November 19, 2026 17:13
… limit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The stress test could not catch the race without an injected delay, and the
state its 200 logins left behind is the likely cause of a later test in the
class timing out in CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…failures

startNewSession unpauses the operation repo before it enqueues its Delta, so a
poll in between split the batch. The identify and IAM fetch tests now report
the models and requests involved, since they only fail in CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ests

The persisted queue is shared with every executor, including ones earlier
tests left running, so one could overwrite it while these tests waited.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…est left behind

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@abdulraqeeb33 abdulraqeeb33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed 4f8a1e99. The login race fix is right: the token lands on the identity model before publish, so a concurrent IAM fetch cannot see a user without a token and re-ask.

Flake fixes look solid. Two nits on the new regression test and the badge helper.

@nan-li nan-li left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nice, 10 runs in a row

fadi-george and others added 2 commits November 20, 2026 11:00
…he unawaited badge completion

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…il a wait

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@abdulraqeeb33 abdulraqeeb33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Both nits fixed in ddbc74a.

  1. Race test goes through login and records the token at store-add, so it fails if the assignment moves after publish.
  2. Badge helper no longer asserts in the unawaited system completion.

fadi-george and others added 4 commits November 20, 2026 12:40
… fails

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A poll landing between the unpause and the session_count enqueue sent the
queue without it, splitting the session's update into two requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Work an earlier test leaves running can ask for the same user's token before
this test logs in, and that ask reached the new listener.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fadi-george
fadi-george merged commit cadbd85 into main Nov 21, 2026
42 checks passed
@fadi-george
fadi-george deleted the fadi/sdk-5816 branch November 21, 2026 05:35
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.

3 participants