Skip to content

Use XCTest's async waiting functionality instead of a hand-rolled method #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 7 additions & 26 deletions TestFoundation/TestNSNotificationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,41 +213,22 @@ class TestNSNotificationQueue : XCTestCase {
// MARK: Private

private func scheduleTimer(withInterval interval: NSTimeInterval) {
var isInvoked = false
let e = expectation(withDescription: "Timer")
let dummyTimer = NSTimer.scheduledTimer(interval, repeats: false) { _ in
isInvoked = true
e.fulfill()
}
NSRunLoop.currentRunLoop().addTimer(dummyTimer, forMode: NSDefaultRunLoopMode)
self.waitForExpectation({
return isInvoked
}, withTimeout: 0.1)
waitForExpectations(withTimeout: 0.1)
}

private func executeInBackgroundThread(_ operation: () -> ()) -> Bool {
var isFinished = false
let lock = NSLock()
private func executeInBackgroundThread(_ operation: () -> Void) {
let e = expectation(withDescription: "Background Execution")
let bgThread = NSThread() {
operation()
lock.lock()
isFinished = true
lock.unlock()
e.fulfill()
}
bgThread.start()

return self.waitForExpectation({
lock.lock()
let finished = isFinished
lock.unlock()
return finished
}, withTimeout: 0.2)
waitForExpectations(withTimeout: 0.2)
}

private func waitForExpectation(_ expectation: () -> Bool, withTimeout timeout: NSTimeInterval) -> Bool {
let timeoutDate = NSDate(timeIntervalSinceNow: timeout)
while !expectation() && timeoutDate.timeIntervalSinceNow > 0.0 {
NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 0.01))
}
return expectation()
}

}