Skip to content

Instantly share code, notes, and snippets.

@SlaunchaMan
Created May 1, 2017 14:48
Show Gist options
  • Save SlaunchaMan/44f41df8eb7511f53d1ce2b0a65e30b5 to your computer and use it in GitHub Desktop.
Save SlaunchaMan/44f41df8eb7511f53d1ce2b0a65e30b5 to your computer and use it in GitHub Desktop.
Helper methods for asynchronous tests
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