Skip to content

Commit 810024a

Browse files
committed
Merge pull request #90 from briancroom/fix-expired-notification-expectation
Don't call a notification expectation handler after the expectation has expired
2 parents e74dea1 + 4960261 commit 810024a

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

Sources/XCTest/XCTestCase.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,27 +328,35 @@ extension XCTestCase {
328328
let expectation = self.expectationWithDescription("Expect notification '\(notificationName)' from " + objectDescription)
329329
// Start observing the notification with specified name and object.
330330
var observer: NSObjectProtocol? = nil
331+
func removeObserver() {
332+
if let observer = observer as? AnyObject {
333+
NSNotificationCenter.defaultCenter().removeObserver(observer)
334+
}
335+
}
336+
337+
weak var weakExpectation = expectation
331338
observer = NSNotificationCenter
332339
.defaultCenter()
333340
.addObserverForName(notificationName,
334341
object: objectToObserve,
335342
queue: nil,
336343
usingBlock: {
337344
notification in
345+
guard let expectation = weakExpectation else {
346+
removeObserver()
347+
return
348+
}
349+
338350
// If the handler is invoked, the test will
339351
// only pass if true is returned.
340352
if let handler = handler {
341353
if handler(notification) {
342354
expectation.fulfill()
343-
if let observer = observer as? AnyObject {
344-
NSNotificationCenter.defaultCenter().removeObserver(observer)
345-
}
355+
removeObserver()
346356
}
347357
} else {
348358
expectation.fulfill()
349-
if let observer = observer as? AnyObject {
350-
NSNotificationCenter.defaultCenter().removeObserver(observer)
351-
}
359+
removeObserver()
352360
}
353361
})
354362

Tests/Functional/Asynchronous/Notifications/Handler/main.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,29 @@ class NotificationHandlerTestCase: XCTestCase {
3333
NSNotificationCenter.defaultCenter().postNotificationName("returnTrue", object: nil)
3434
waitForExpectationsWithTimeout(0.1, handler: nil)
3535
}
36+
37+
// CHECK: Test Case 'NotificationHandlerTestCase.test_notificationNameIsObservedAfterTimeout_handlerIsNotCalled' started.
38+
// CHECK: .*/Tests/Functional/Asynchronous/Notifications/Handler/main.swift:\d+: error: NotificationHandlerTestCase.test_notificationNameIsObservedAfterTimeout_handlerIsNotCalled : Asynchronous wait failed - Exceeded timeout of 0.1 seconds, with unfulfilled expectations: Expect notification 'note' from any object
39+
// CHECK: Test Case 'NotificationHandlerTestCase.test_notificationNameIsObservedAfterTimeout_handlerIsNotCalled' failed \(\d+\.\d+ seconds\).
40+
func test_notificationNameIsObservedAfterTimeout_handlerIsNotCalled() {
41+
expectationForNotification("note", object: nil, handler: { _ in
42+
XCTFail("Should not call the notification expectation handler")
43+
return true
44+
})
45+
waitForExpectationsWithTimeout(0.1, handler: nil)
46+
NSNotificationCenter.defaultCenter().postNotificationName("note", object: nil)
47+
}
3648

3749
static var allTests: [(String, NotificationHandlerTestCase -> () throws -> Void)] {
3850
return [
3951
("test_notificationNameIsObserved_handlerReturnsFalse_andFails", test_notificationNameIsObserved_handlerReturnsFalse_andFails),
4052
("test_notificationNameIsObserved_handlerReturnsTrue_andPasses", test_notificationNameIsObserved_handlerReturnsTrue_andPasses),
53+
("test_notificationNameIsObservedAfterTimeout_handlerIsNotCalled", test_notificationNameIsObservedAfterTimeout_handlerIsNotCalled),
4154
]
4255
}
4356
}
4457

4558
XCTMain([testCase(NotificationHandlerTestCase.allTests)])
4659

47-
// CHECK: Executed 2 tests, with 1 failure \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
48-
// CHECK: Total executed 2 tests, with 1 failure \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
60+
// CHECK: Executed 3 tests, with 2 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
61+
// CHECK: Total executed 3 tests, with 2 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

0 commit comments

Comments
 (0)