From 550d8c3dd249a0a71e290379d1d0a2cc08b62846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric=20Luthi?= Date: Thu, 22 Sep 2016 23:34:33 +0200 Subject: [PATCH 1/5] Remove unnecessary assert This pattern does not make sense at all because the assert can never fail. if (condition) { XCTAssert(condition, @"comment"); } --- Tests/Tests/AFNetworkReachabilityManagerTests.m | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Tests/Tests/AFNetworkReachabilityManagerTests.m b/Tests/Tests/AFNetworkReachabilityManagerTests.m index 20b7d8bf5f..7d3bc3b846 100644 --- a/Tests/Tests/AFNetworkReachabilityManagerTests.m +++ b/Tests/Tests/AFNetworkReachabilityManagerTests.m @@ -67,12 +67,7 @@ - (void)verifyReachabilityNotificationGetsPostedWithManager:(AFNetworkReachabili BOOL reachable = (status == AFNetworkReachabilityStatusReachableViaWiFi || status == AFNetworkReachabilityStatusReachableViaWWAN); - if (reachable) { - XCTAssert(reachable, - @"Expected network to be reachable but got '%@'", - AFStringFromNetworkReachabilityStatus(status)); - XCTAssertEqual(reachable, manager.isReachable, @"Expected status to match 'isReachable'"); - } + XCTAssertEqual(reachable, manager.isReachable, @"Expected status to match 'isReachable'"); return reachable; }]; From 111931b32cc94c2d4538abc0a8d572a289e9530b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric=20Luthi?= Date: Thu, 22 Sep 2016 23:54:49 +0200 Subject: [PATCH 2/5] Fulfill the expectation once the network status becomes reachable Do not use XCTAssertEqual(reachable, ...) because it might transition to AFNetworkReachabilityStatusNotReachable before becoming reachable. --- Tests/Tests/AFNetworkReachabilityManagerTests.m | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tests/Tests/AFNetworkReachabilityManagerTests.m b/Tests/Tests/AFNetworkReachabilityManagerTests.m index 7d3bc3b846..1c3b383820 100644 --- a/Tests/Tests/AFNetworkReachabilityManagerTests.m +++ b/Tests/Tests/AFNetworkReachabilityManagerTests.m @@ -95,10 +95,11 @@ - (void)verifyReachabilityStatusBlockGetsCalledWithManager:(AFNetworkReachabilit BOOL reachable = (status == AFNetworkReachabilityStatusReachableViaWiFi || status == AFNetworkReachabilityStatusReachableViaWWAN); - XCTAssert(reachable, @"Expected network to be reachable but got '%@'", AFStringFromNetworkReachabilityStatus(status)); XCTAssertEqual(reachable, weakManager.isReachable, @"Expected status to match 'isReachable'"); - [expectation fulfill]; + if (reachable) { + [expectation fulfill]; + } }]; [manager startMonitoring]; From 83c33fb9a73d0be91aa484201205e36ca2857a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric=20Luthi?= Date: Thu, 22 Sep 2016 23:55:11 +0200 Subject: [PATCH 3/5] Enable domain reachability tests --- Tests/Tests/AFNetworkReachabilityManagerTests.m | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Tests/Tests/AFNetworkReachabilityManagerTests.m b/Tests/Tests/AFNetworkReachabilityManagerTests.m index 1c3b383820..d07f9f4e20 100644 --- a/Tests/Tests/AFNetworkReachabilityManagerTests.m +++ b/Tests/Tests/AFNetworkReachabilityManagerTests.m @@ -81,10 +81,9 @@ - (void)testAddressReachabilityNotification { [self verifyReachabilityNotificationGetsPostedWithManager:self.addressReachability]; } -//Commenting out for Travis Stability -//- (void)testDomainReachabilityNotification { -// [self verifyReachabilityNotificationGetsPostedWithManager:self.domainReachability]; -//} +- (void)testDomainReachabilityNotification { + [self verifyReachabilityNotificationGetsPostedWithManager:self.domainReachability]; +} - (void)verifyReachabilityStatusBlockGetsCalledWithManager:(AFNetworkReachabilityManager *)manager { @@ -113,9 +112,8 @@ - (void)testAddressReachabilityBlock { [self verifyReachabilityStatusBlockGetsCalledWithManager:self.addressReachability]; } -//Commenting out for Travis Stability -//- (void)testDomainReachabilityBlock { -// [self verifyReachabilityStatusBlockGetsCalledWithManager:self.domainReachability]; -//} +- (void)testDomainReachabilityBlock { + [self verifyReachabilityStatusBlockGetsCalledWithManager:self.domainReachability]; +} @end From 025c82fa7b7d583ecb8ba34ec04e227084fa354d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric=20Luthi?= Date: Wed, 12 Oct 2016 07:37:42 +0200 Subject: [PATCH 4/5] Explicitly set the expectation to nil to avoid calling fulfill twice --- Tests/Tests/AFNetworkReachabilityManagerTests.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/Tests/AFNetworkReachabilityManagerTests.m b/Tests/Tests/AFNetworkReachabilityManagerTests.m index d07f9f4e20..ce2b2a797e 100644 --- a/Tests/Tests/AFNetworkReachabilityManagerTests.m +++ b/Tests/Tests/AFNetworkReachabilityManagerTests.m @@ -87,7 +87,7 @@ - (void)testDomainReachabilityNotification { - (void)verifyReachabilityStatusBlockGetsCalledWithManager:(AFNetworkReachabilityManager *)manager { - __weak XCTestExpectation *expectation = [self expectationWithDescription:@"reachability status change block gets called"]; + __weak __block XCTestExpectation *expectation = [self expectationWithDescription:@"reachability status change block gets called"]; typeof(manager) __weak weakManager = manager; [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { @@ -98,6 +98,7 @@ - (void)verifyReachabilityStatusBlockGetsCalledWithManager:(AFNetworkReachabilit if (reachable) { [expectation fulfill]; + expectation = nil; } }]; From fb5022c0732e9be6981cf23bf04b9ef7ebd3e527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric=20Luthi?= Date: Wed, 12 Oct 2016 07:54:10 +0200 Subject: [PATCH 5/5] Remove assertion that status in callback == reachabilityManager status The status passed in callbacks (both block and notification) is captured before the callbacks are dispatched on the main thread. It is thus possible that the reachabilityManager status change in the meantime. Users MUST use the status parameter for the block callback or the `AFNetworkingReachabilityNotificationStatusItem` user info key for the notification. --- .../Tests/AFNetworkReachabilityManagerTests.m | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/Tests/Tests/AFNetworkReachabilityManagerTests.m b/Tests/Tests/AFNetworkReachabilityManagerTests.m index ce2b2a797e..a79530dde4 100644 --- a/Tests/Tests/AFNetworkReachabilityManagerTests.m +++ b/Tests/Tests/AFNetworkReachabilityManagerTests.m @@ -64,12 +64,9 @@ - (void)verifyReachabilityNotificationGetsPostedWithManager:(AFNetworkReachabili handler:^BOOL(NSNotification *note) { AFNetworkReachabilityStatus status; status = [note.userInfo[AFNetworkingReachabilityNotificationStatusItem] integerValue]; - BOOL reachable = (status == AFNetworkReachabilityStatusReachableViaWiFi - || status == AFNetworkReachabilityStatusReachableViaWWAN); - - XCTAssertEqual(reachable, manager.isReachable, @"Expected status to match 'isReachable'"); - - return reachable; + BOOL isReachable = (status == AFNetworkReachabilityStatusReachableViaWiFi + || status == AFNetworkReachabilityStatusReachableViaWWAN); + return isReachable; }]; [manager startMonitoring]; @@ -89,14 +86,10 @@ - (void)verifyReachabilityStatusBlockGetsCalledWithManager:(AFNetworkReachabilit { __weak __block XCTestExpectation *expectation = [self expectationWithDescription:@"reachability status change block gets called"]; - typeof(manager) __weak weakManager = manager; [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { - BOOL reachable = (status == AFNetworkReachabilityStatusReachableViaWiFi - || status == AFNetworkReachabilityStatusReachableViaWWAN); - - XCTAssertEqual(reachable, weakManager.isReachable, @"Expected status to match 'isReachable'"); - - if (reachable) { + BOOL isReachable = (status == AFNetworkReachabilityStatusReachableViaWiFi + || status == AFNetworkReachabilityStatusReachableViaWWAN); + if (isReachable) { [expectation fulfill]; expectation = nil; }