Skip to content

[Suggestion] Add a timeout option to confirmation #1068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
BrentMifsud opened this issue Apr 9, 2025 · 1 comment
Closed

[Suggestion] Add a timeout option to confirmation #1068

BrentMifsud opened this issue Apr 9, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@BrentMifsud
Copy link

BrentMifsud commented Apr 9, 2025

Motivation

Confirmation is the closest thing Swift Testing has to the old Expectation API that XCTest had.

The only issue with it, is that it does not wait for its confirmation requirements to be finished. It just executes to the end of its block immediately.

here is an example of this limitation:

// This will fail the test case
await confirmation { confirmation in
    myClass.synchronousFuncWithCompletionHandler { 
          confirmation()
    }
}

If you want to make that test pass, you have to add your own artificial delay:

// This will succeed assuming the closure completed within 100ms
try await confirmation { confirmation in
    myClass.synchronousFuncWithCompletionHandler { 
          confirmation()
    }

    try await Task.sleep(for: .milliseconds(100))
}

This isn't ideal, as it not only delays the execution of your test, but it also adds room for flakiness.

Proposed solution

add a timeout param to the confirmation function.

The await on the confirmation should end once the expectedCount is met, or the timeout is reached. Whichever comes first.

example usage:

// this will succeed if the completion handler finishes within 100ms
await confirmation(timeout: .milliseconds(100)) { confirmation in
    myClass.synchronousFuncWithCompletionHandler { 
          confirmation()
    }
}
// this will fail due to timeout
await confirmation(timeout: .milliseconds(100)) { confirmation in
    try await Task.sleep(for: .seconds(1))
    myClass.synchronousFuncWithCompletionHandler { 
          confirmation()
    }
}

Alternatives considered

Alternatively, to not break the behaviour of confirmation, there could be an expectation function that behaves very similarly, but with the additional features mentioned above.

Additional information

No response

@BrentMifsud BrentMifsud added enhancement New feature or request triage-needed labels Apr 9, 2025
@grynspan
Copy link
Contributor

grynspan commented Apr 9, 2025

Duplicate of #978

@grynspan grynspan marked this as a duplicate of #978 Apr 9, 2025
@grynspan grynspan closed this as not planned Won't fix, can't repro, duplicate, stale Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants