Skip to content

Conversation

@ajpallares
Copy link
Member

The test EventsManagerIntegrationTests.testPostingCustomerCenterDoesNotFail() was sometimes failing. E.g. here (even though it passed after 3 retries).

This PR makes the test more deterministic by waiting for the execution of Purchases.shared.track(customerCenterEvent: ) to complete before checking the logs.
In addition, the logs check is also more strict now, checking for the right event name + event identifier.

More details

I suspect the reason of the random failures is that Task.yield() would not always wait for the track event Tasks to complete, resulting in the test checks happening before the expected logs actually happened.

@ajpallares
Copy link
Member Author

@RCGitBot please test

Comment on lines -79 to 81
await Task.yield()
_ = await (task0.value, task1.value)

Copy link
Contributor

@JZDesign JZDesign Dec 18, 2025

Choose a reason for hiding this comment

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

There is a very reliable way to make these kinds of tests more stable without needing to wait for the tasks.

The trick is to use a lower priority on a different task needing to yield.

I made a helper for that a while ago XCTestCase+Yield.swift

Copy link
Contributor

@JZDesign JZDesign Dec 18, 2025

Choose a reason for hiding this comment

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

You can see usages of it in the. FileRepository tests

func yield() async {
    await Task(priority: .low) {
        await Task.yield()
    }.value
}
 
// 👇 this Task ensures the file repository has a user initiated priority 
// when it dispatches its own tasks
await Task(priority: .userInitiated) {
   // 👇 this dispatches its own task
   sut.fileRepository.prefetch(urls: [someURL])
}.value

// 👇 this yields on a low priority thread forcing the structured concurrency 
// system to pick up a higher priority task
await yield()

// because Tasks inherit the current scope's priority, we have a stable test without needing to explicitly wait for the dispatch nested task

Copy link
Member Author

Choose a reason for hiding this comment

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

That makes a lot of sense. Thanks for the tip! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants