What is the proper way to use XCTestExpectation on Linux? I'm working on a project that supports both macOS and Linux and I have an asynchronous test that compiles and runs on Mac, but fails to compile on Linux.
class BindingTests: XCTestCase {
func testAccepting() {
let socket = SocketPath("/tmp/com.trailblazer.sock")!
let binding: Binding
do {
binding = try socket.bind()
} catch {
XCTFail("Failed to bind to socket with error \(error)")
return
}
XCTAssertNoThrow(try binding.listen(maxQueued: 1))
let acceptConnection = XCTestExpectation(description: "Ensure connection is properly accepted")
DispatchQueue.global(qos: .background).async {
do {
try binding.accept() { _ in
acceptConnection.fulfill()
}
} catch {}
}
XCTAssertNoThrow(try socket.connect(type: TCPSocket.self))
XCTAssertEqual(XCTWaiter.wait(for: [acceptConnection], timeout: 5.0), .completed)
}
}
I get the following compilation errors on Linux:
error: cannot invoke initializer for type 'XCTestExpectation' with an argument list of type '(description: String)'
expected an argument list of type '(description: String, file: StaticString, line: Int, testCase: XCTestCase)'
and
error: use of unresolved identifier 'XCTWaiter'
If I change XCTestExpectation to initialize like this:
let acceptConnection = XCTestExpectation(description: "Ensure connection is properly accepted", file: #file, line: #line, testCase: self)
It fails to compile with this error instead:
error: 'XCTestExpectation' initializer is inaccessible due to 'internal' protection level
I apparently can't trust the online documentation on XCTestExpectation since it must only be valid for Apple platforms?
I dug through a bit of Swift/SwiftPM source code but I'm not really sure where is the "correct" place to look at and all the places where I did find XCTestExpectation had the initializer declared public (with default values) so I'm really confused as to why it works on my Mac but not Linux. There were also some additional helper methods for adding expectation to an XCTestCase, but none of them compiled on my mac.