Skip to content

Make SelectionCopy's clear-then-write test deterministic - #242

Merged
alexkroman merged 1 commit into
mainfrom
test/selection-copy-flaky-timeout
Oct 9, 2026
Merged

alexkroman merged 1 commit into
mainfrom
test/selection-copy-flaky-timeout

Conversation

@alexkroman

Copy link
Copy Markdown
Collaborator

Problem

SelectionCopyTests › "a clear before the write isn't mistaken for an empty copy" failed on the v0.1.63 release PR's check run (#241). The same code passed on main.

✘ … SelectionCopyTests.swift:116: Expectation failed: await copy.copySelection(maxCharacters: 100) == "late copy"
✘ … SelectionCopyTests.swift:117: Expectation failed: (pasteboard.string → "late copy") == "user's clipboard"

The test modelled the app's late write with a detached Task that slept 30 ms, racing SelectionCopy's 500 ms wall-clock deadline. On a busy runner the task ran after the deadline (the test took 0.640 s). The copier gave up and restored the user's clipboard, and then the late write overwrote it.

Fix

Test-only. FakeCopyPasteboard gets a lateWrite that it applies the moment the cleared clipboard is first read. The copier therefore always sees the clear on one poll and the write on a later one, with no timer to race. The timeout is raised to 5 s. It only bounds the failure case, since a passing run returns after two polls (~20 ms).

Verification

  • swift test --filter SelectionCopy passes three runs in a row.
  • Mutation check: changing SelectionCopy to stop at the first change (while seen == before) instead of waiting for a string makes the test fail with 2 issues, so it still guards the behaviour.
  • swiftlint --strict and swift-format lint --strict pass.

🤖 Generated with Claude Code

The test modelled an app's late clipboard write with a detached Task that
slept 30 ms, racing SelectionCopy's 500 ms wall-clock deadline. On a loaded
CI runner the Task ran after the deadline: the copier gave up, restored the
user's clipboard, and the late write then clobbered it — both expectations
failed (seen on the v0.1.63 release PR's check run).

The fake pasteboard now applies the late write the moment the cleared
clipboard is first read, so the copier always sees the clear on one poll and
the write on a later one. Verified the test still fails when SelectionCopy
stops at the first change instead of waiting for a string.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alexkroman
alexkroman enabled auto-merge (squash) October 9, 2026 19:33
@alexkroman
alexkroman merged commit 2d1409b into main Oct 9, 2026
13 checks passed
@alexkroman
alexkroman deleted the test/selection-copy-flaky-timeout branch October 9, 2026 19:47
@alexkroman alexkroman mentioned this pull request Oct 9, 2026
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.

2 participants