Created
May 26, 2022 15:48
-
-
Save mattrob33/2ab983e0f06a808becbbfadc1c326b9d to your computer and use it in GitHub Desktop.
This file contains 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
/// | |
/// A subclass of ``XCTestCase`` that supports async setup and teardown. | |
/// | |
class XCAsyncTestCase: XCTestCase { | |
func asyncSetUpWithError() async throws { | |
fatalError("Must override") | |
} | |
func asyncTearDownWithError() async throws { | |
fatalError("Must override") | |
} | |
override func setUpWithError() async throws { | |
wait { | |
try await self.asyncSetUpWithError() | |
} | |
} | |
override func tearDownWithError() throws { | |
wait { | |
try await self.asyncTearDownWithError() | |
} | |
} | |
func wait(asyncBlock: @escaping (() async throws -> Void)) { | |
let semaphore = DispatchSemaphore(value: 0) | |
Task.init { | |
try await asyncBlock() | |
semaphore.signal() | |
} | |
semaphore.wait() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment