Created
May 1, 2017 14:48
-
-
Save SlaunchaMan/44f41df8eb7511f53d1ce2b0a65e30b5 to your computer and use it in GitHub Desktop.
Helper methods for asynchronous tests
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
import Foundation | |
import Dispatch | |
import XCTest | |
extension DispatchQueue { | |
func asyncAfter(_ seconds: TimeInterval, | |
execute work: @escaping @convention(block) () -> Void) { | |
asyncAfter(deadline: DispatchTime.now() + seconds, execute: work) | |
} | |
} | |
extension XCTestCase { | |
func waitForExecution(on queue: DispatchQueue = .main, | |
timeout: TimeInterval = 2, | |
delay: TimeInterval = 0, | |
executing block: @escaping () -> Void) { | |
let expect = expectation(description: "executing on queue") | |
queue.asyncAfter(delay) { | |
block() | |
expect.fulfill() | |
} | |
wait(for: [expect], timeout: timeout + delay) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment