Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import Foundation
6
import XCTest
7
8
extension XCTestCase {
9
func wait(_ time: TimeInterval) {
10
let expectation = self.expectation(description: "Wait")
11
let delayTime = DispatchTime.now() + Double(Int64(time * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
12
DispatchQueue.main.asyncAfter(deadline: delayTime) {
13
expectation.fulfill()
14
}
15
waitForExpectations(timeout: time + 1, handler: nil)
16
}
17
18
func waitForCondition(timeout: TimeInterval = 10, condition: () -> Bool) {
19
let timeoutTime = Date.timeIntervalSinceReferenceDate + timeout
20
21
while !condition() {
22
if Date.timeIntervalSinceReferenceDate > timeoutTime {
23
XCTFail("Condition timed out")
24
return
25
}
26
27
wait(0.1)
28
}
29
}
30
}