Skip to content
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

[SR-15561] XCTestCase crashes due to use of expectations in async/await method on Linux #344

tonyarnold opened this issue Dec 6, 2021 · 5 comments


Copy link

@tonyarnold tonyarnold commented Dec 6, 2021

Previous ID SR-15561
Radar rdar://85344643
Original Reporter @tonyarnold
Type Bug
Status Resolved
Resolution Done

I was using the swiftlang/swift:nightly-5.5-focal Docker tag on the 1st December, 2021.

Additional Detail from JIRA
Votes 0
Component/s XCTest
Labels Bug
Assignee @stmontgomery
Priority Medium

md5: 219c66c7d78a9418491933d5732beff1

Issue Description:

It appears that XCTestCase.expectation(description: "…") calls through to XCTestCase.addExpectation(…) which asserts that it is being called from the main thread. See

precondition(Thread.isMainThread, "\(#function) must be called on the main thread")
for the implementation of this.

When running in an async test case, the test case is not always run on the main thread, so the test fails and crashes the running suite due to this assertion.

A simple test case is to call the following code within an async test, something like:

class MyTests: XCTestCase {
    func testAThing() async throws {
        let expectation = expectation(description: "Sending email")

And then run it on Linux using any recent Swift 5.5 snapshot.

The same code runs fine under macOS under Xcode 13.2 beta 2, so this appears to be a difference between Swift's implementation of XCTest and Xcode's.

Copy link

@stmontgomery stmontgomery commented Dec 6, 2021

This is because the XCTestCase methods which create and return XCTestExpectations currently require calling on the main thread:

precondition(Thread.isMainThread, "\(#function) must be called on the main thread")

But we should relax those constraints for the Corelibs implementation of XCTest since that's something we recently did in the Xcode implementation

Copy link

@stmontgomery stmontgomery commented Dec 6, 2021

Sorry, I overlooked Tony's comment which says the same thing! Sounds like we're on the same page 🙂

Copy link

@stmontgomery stmontgomery commented Dec 27, 2021

Copy link

@stmontgomery stmontgomery commented Jan 10, 2022

Copy link

@tonyarnold tonyarnold commented Jan 10, 2022

Fantastic, thanks @stmontgomery! 😃

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 9, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

2 participants