Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dpfannenstiel/b3ab5467c576cdf533288c19c2f0d47d to your computer and use it in GitHub Desktop.
Save dpfannenstiel/b3ab5467c576cdf533288c19c2f0d47d to your computer and use it in GitHub Desktop.
Synchronous testing of async tasks.
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