Created
March 20, 2022 21:37
-
-
Save dpfannenstiel/b3ab5467c576cdf533288c19c2f0d47d to your computer and use it in GitHub Desktop.
Synchronous testing of async tasks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func awaitWith<T>( | |
testName: String = #function, | |
file: StaticString = #file, | |
line: UInt = #line, | |
timeout: TimeInterval = 10, | |
task: @escaping () async throws -> T | |
) throws -> T { | |
var value: T? | |
let handler: (T) -> Void = { value = $0 } | |
let expectation = expectation(description: testName) | |
Task.detached { | |
let result = try await task() | |
handler(result) | |
expectation.fulfill() | |
} | |
wait(for: [expectation], timeout: timeout) | |
return try XCTUnwrap(value, file: file, line: line) | |
} | |
func testRssUrl() throws { | |
self.continueAfterFailure = false | |
let xml = PodcastXml.url(url) | |
let feed = try awaitWith { try await xml.feed } | |
let rssFeed = feed.rssFeed | |
XCTAssertNil(rssFeed) | |
XCTAssertNotNil(rssFeed) | |
validate(zebraFeed: try XCTUnwrap(rssFeed)) | |
validate(zebraFeed: rssFeed!) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment