Skip to content

Conversation

@muzahidul-opti
Copy link
Contributor

@muzahidul-opti muzahidul-opti commented Jan 21, 2026

Summary

This PR fixes failing test cases in EventDispatcherTests_Batch and adds comprehensive project documentation to improve developer onboarding.

Bug Fixes

  • Fixed testFlushEventsWhenBatchFails: Test was timing out due to actual HTTP requests in TestEventDispatcher.sendEvent()

    • Modified TestEventDispatcher to simulate network completion immediately instead of making real network calls
    • Added notification posting to maintain compatibility with notification listener tests
  • Fixed testInvalidEventInSecond: Test was failing due to non-deterministic JSON output

    • Added .sortedKeys to JSONEncoder in ArrayEventForDispatch+Extension.makeBatchEvent()
    • Ensures consistent JSON key ordering for test comparisons

Root Cause

The recent async retry strategy refactoring (commit 50c66b1) changed DefaultEventDispatcher from synchronous to asynchronous retry logic. The TestEventDispatcher.sendEvent() was calling super.sendEvent() which made actual HTTP requests to dummy URLs, causing timeouts and leaving events in the queue.

Changes Made

Sources/Extensions/ArrayEventForDispatch+Extension.swift

  • Added .sortedKeys formatting to JSONEncoder in makeBatchEvent() for deterministic JSON output

Tests/OptimizelyTests-Batch-iOS/EventDispatcherTests_Batch.swift

  • Modified TestEventDispatcher.sendEvent() to avoid real network calls
  • Added notification posting to maintain test compatibility
  • Removed print statement for cleaner test output

CLAUDE.md (New)

Added comprehensive project documentation including:

  • Project overview and structure (source code + tests)
  • Getting started guide (platform support, installation, setup)
  • Coding standards and common patterns
  • Complete development workflow (branch strategy, making changes, testing, PR process)
  • Testing guide with xcodebuild commands and best practices
  • Key API usage examples
  • Helpful commands reference

Test Plan

  • testFlushEventsWhenBatchFails now passes in 0.018 seconds (previously timed out)
  • testInvalidEventInSecond now passes with deterministic JSON comparison
  • ✅ All other EventDispatcherTests_Batch tests remain passing

Issues

muzahidul-opti and others added 2 commits January 21, 2026 20:40
Added CLAUDE.md file to provide context and guidelines for development:

- Project overview and structure
- Getting started guide with platform support and installation
- Coding standards and common patterns (protocol-oriented design, thread safety)
- Complete development workflow from branch creation to PR
- Testing guide with xcodebuild commands and best practices
- Key API usage examples
- Helpful commands for file searching, testing, and git operations

The documentation is organized logically from introduction to reference material
for better developer onboarding and contribution experience.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@muzahidul-opti muzahidul-opti changed the title fix: resolve test failures in EventDispatcherTests_Batch and add project documentation [FSSDK-12234 ] fix: resolve test failures in EventDispatcherTests_Batch and add project documentation Jan 21, 2026
muzahidul-opti and others added 2 commits January 21, 2026 21:36
Fixed test failures caused by non-deterministic JSON key ordering in Xcode 16.x
by adding .sortedKeys to all JSONEncoder instances.

Changes:
- Sources/Utils/Utils.swift: Added .sortedKeys to static jsonEncoder used by
  UserAttribute.stringRepresentation generation
- Tests/TestUtils/OTUtils.swift: Added .sortedKeys to isEqualWithEncodeThenDecode()
  and makeEventForDispatch() test utilities
- Tests/OptimizelyTests-Common/DecisionServiceTests_Experiments.swift: Updated
  expected JSON error messages to use alphabetically sorted keys
- Tests/OptimizelyTests-Common/OptimizelyUserContextTests_Decide_Reasons.swift:
  Updated expected JSON condition strings to use alphabetically sorted keys

Root Cause:
JSONEncoder produces non-deterministic key ordering without .sortedKeys, causing
tests to fail on Xcode 16.x even though they passed on earlier versions. The
UserAttribute model generates a stringRepresentation field using Utils.jsonEncoder
during decode, which is then compared in equality checks. Without sorted keys,
encode/decode cycles produce different stringRepresentation values.

Fixes testEncodeJSON, DecisionServiceTests_Experiments, and
OptimizelyUserContextTests_Decide_Reasons test failures.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@muzahidul-opti muzahidul-opti changed the title [FSSDK-12234 ] fix: resolve test failures in EventDispatcherTests_Batch and add project documentation [FSSDK-12240] fix: resolve test failures in EventDispatcherTests_Batch and add project documentation Jan 21, 2026
muzahidul-opti and others added 4 commits January 22, 2026 00:15
Fixed three intermittently failing tests by replacing unreliable timing-based
synchronization with proper DispatchGroup waits and thread-safe option tracking.

Changes:
1. EventDispatcherRetryTests.swift:
   - testRetry_AllAttemptsExhausted: Replaced asyncAfter with notify.wait()
   - testRetry_NetworkReachabilityDown: Added notify.wait() for proper completion

2. OptimizelyUserContextTests_Decide_CMAB.swift:
   - testDecideAsync_cmabCacheOptions: Fixed race condition by capturing options
     in an array and validating after all async calls complete
   - MockCmabService: Overrides async getDecision() to respect parent's locking,
     uses separate optionsLock only for mock-specific tracking

The fixes eliminate race conditions by using the existing notify DispatchGroup
in DefaultEventDispatcher and ensuring thread-safe access to mock state.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Added OTUtils.clearAllEventQueues() to setUp() to ensure each test
starts with a clean queue. Previously only clearing in tearDown() meant
that if a test failed or was interrupted, the next test would inherit
leftover events from the queue.

This fixes testRetry_AllAttemptsExhausted which was failing with 2
events in queue instead of 1 due to stale events from previous tests.

Also fixed MockCmabService to override both sync and async getDecision()
methods since DecisionService calls the sync version.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Added optimizely.decisionQueue.sync {} after wait(for:) to ensure all
queued async decision tasks finish executing before checking the mock's
capturedOptions array.

decideAsync dispatches work to a background decisionQueue. On CI, the
wait(for:) might complete (expectations fulfilled in callbacks) before
the actual getDecision() calls are tracked in the mock. The sync barrier
ensures all queued work completes.

Fixes intermittent CI failure: "0 is not equal to 3" in
testDecideAsync_cmabCacheOptions

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Consolidated duplicate sections to improve readability:
- Moved all testing commands to dedicated 'Testing' section
- Kept linting info only in 'Coding Standards'
- Consolidated git commands in 'Helpful Commands'
- Added cross-references in 'Making Changes' workflow
- Removed repeated commands from multiple locations

This reduces redundancy and makes the guide easier to maintain.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@muzahidul-opti muzahidul-opti deleted the fix-unit-test-event-dispatcher branch January 22, 2026 06:36
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