Skip to content

test: ignore other SDK instances' batches in session event tests - #885

Merged
turnipdabeets merged 1 commit into
mainfrom
test/filter-mock-batches-by-token
Oct 1, 2026
Merged

turnipdabeets merged 1 commit into
mainfrom
test/filter-mock-batches-by-token

Conversation

@turnipdabeets

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

"Clears $session_id after 30 mins of background inactivity" still fails intermittently on test-ios-simulator, for example on #881 (run), after #878:

Expectation failed: (events.count → 6) == 2
Expectation failed: (events[0].event → "$screen") == "event captured"

#878 already sets captureScreenViews = false for this suite, so those $screen events can't come from the test's own SDK. They come from another test's SDK instance that is still running. Every SDK in the test process posts to localhost:9001, and OHHTTPStubs routes every /batch to whichever MockPostHogServer is current. A foreign batch can then satisfy the test's batch expectation before its own events are sent. The test's existing server.reset(batchCount: 1) ("Clear any stale batch requests from previous tests' PostHogSDK instances") only clears batches that have already arrived. It doesn't help when one arrives during the test.

  • MockPostHogServer gets an optional batchProjectToken. When it's set, /batch requests whose api_key differs aren't recorded. The body is buffered before parsing, because a stream-backed body can only be read once.
  • The session event suite sets it to the SDK's own token in getSut(). Each test already uses a fresh UUID() token.
  • Other suites are unchanged. They can opt in the same way if they show the same symptom.

This is test-only; SDK behavior and public API are unchanged.

💚 How did you test it?

Reproduced the failure deterministically by adding a second SDK with a different token that sends a $screen while the test runs:

let foreignConfig = PostHogConfig(projectToken: "foreign_\(UUID().uuidString)", host: "http://localhost:9001")
foreignConfig.flushAt = 1
let foreign = PostHogSDK.with(foreignConfig)
foreign.capture("$screen")
Setup -only-testing:PostHogTests/PostHogSessionManagerTest/PostHogSDKEvents (iOS 26 simulator)
main + snippet ❌ events[0].event → "$screen", the same failure as CI
this branch + snippet ✅ 5/5 pass
  • make lint: no new violations.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Investigated and drafted with Claude Code (Claude Opus 5.5) while diagnosing the test-ios-simulator failures blocking #872 and #881.

  • Filtering by api_key at the mock fixes this whole class of failure without having to track down every leaked SDK. Closing every SDK in every suite would be a much larger change.
  • The filter is opt-in, so suites that intentionally use several SDKs or tokens keep working as before.

🤖 Generated with Claude Code

MockPostHogServer can now record only batches carrying the test's project token.
@turnipdabeets
turnipdabeets requested a review from a team as a code owner October 1, 2026 18:12
@turnipdabeets turnipdabeets self-assigned this Oct 1, 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