Skip to content

Commit 93cd136

Browse files
briancroomparkera
authored andcommitted
Use XCTest's async waiting functionality instead of a hand-rolled method (#309)
1 parent 48dc6db commit 93cd136

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed

TestFoundation/TestNSNotificationQueue.swift

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -213,41 +213,22 @@ class TestNSNotificationQueue : XCTestCase {
213213
// MARK: Private
214214

215215
private func scheduleTimer(withInterval interval: NSTimeInterval) {
216-
var isInvoked = false
216+
let e = expectation(withDescription: "Timer")
217217
let dummyTimer = NSTimer.scheduledTimer(interval, repeats: false) { _ in
218-
isInvoked = true
218+
e.fulfill()
219219
}
220220
NSRunLoop.currentRunLoop().addTimer(dummyTimer, forMode: NSDefaultRunLoopMode)
221-
self.waitForExpectation({
222-
return isInvoked
223-
}, withTimeout: 0.1)
221+
waitForExpectations(withTimeout: 0.1)
224222
}
225223

226-
private func executeInBackgroundThread(_ operation: () -> ()) -> Bool {
227-
var isFinished = false
228-
let lock = NSLock()
224+
private func executeInBackgroundThread(_ operation: () -> Void) {
225+
let e = expectation(withDescription: "Background Execution")
229226
let bgThread = NSThread() {
230227
operation()
231-
lock.lock()
232-
isFinished = true
233-
lock.unlock()
228+
e.fulfill()
234229
}
235230
bgThread.start()
236231

237-
return self.waitForExpectation({
238-
lock.lock()
239-
let finished = isFinished
240-
lock.unlock()
241-
return finished
242-
}, withTimeout: 0.2)
232+
waitForExpectations(withTimeout: 0.2)
243233
}
244-
245-
private func waitForExpectation(_ expectation: () -> Bool, withTimeout timeout: NSTimeInterval) -> Bool {
246-
let timeoutDate = NSDate(timeIntervalSinceNow: timeout)
247-
while !expectation() && timeoutDate.timeIntervalSinceNow > 0.0 {
248-
NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 0.01))
249-
}
250-
return expectation()
251-
}
252-
253234
}

0 commit comments

Comments
 (0)