Browse Source

Fix Reachability behavior regression from 4.8.1. (#2931)

* Fix regression in reachability behavior.

* Update Example app to recommended localization.

* Update Travis to 10.2.

* Try to enable Travis for hotfix branch.

* Cleanup whitespace.

* Update settings for Example app scheme.

* Fix memory leak in validate().

* Fix memory leak for downloads too.

* Try to fix sim issues by updating to Xcode 10.3 Travis image.

* Another Travis fix.
Jon Shier 6 years ago
parent
commit
736fe64bce

+ 1 - 0
.travis.yml

@@ -3,6 +3,7 @@ osx_image: xcode10.1
 branches:
 branches:
   only:
   only:
     - master
     - master
+    - hotfix
 cache: bundler
 cache: bundler
 env:
 env:
   global:
   global:

+ 2 - 1
Example/iOS Example.xcodeproj/project.pbxproj

@@ -216,10 +216,11 @@
 			};
 			};
 			buildConfigurationList = F8111E0019A951050040E7D1 /* Build configuration list for PBXProject "iOS Example" */;
 			buildConfigurationList = F8111E0019A951050040E7D1 /* Build configuration list for PBXProject "iOS Example" */;
 			compatibilityVersion = "Xcode 3.2";
 			compatibilityVersion = "Xcode 3.2";
-			developmentRegion = English;
+			developmentRegion = en;
 			hasScannedForEncodings = 0;
 			hasScannedForEncodings = 0;
 			knownRegions = (
 			knownRegions = (
 				Base,
 				Base,
+				en,
 			);
 			);
 			mainGroup = F8111DFC19A951050040E7D1;
 			mainGroup = F8111DFC19A951050040E7D1;
 			productRefGroup = F8111E0619A951050040E7D1 /* Products */;
 			productRefGroup = F8111E0619A951050040E7D1 /* Products */;

+ 10 - 7
Example/iOS Example.xcodeproj/xcshareddata/xcschemes/iOS Example.xcscheme

@@ -61,14 +61,17 @@
             ReferencedContainer = "container:iOS Example.xcodeproj">
             ReferencedContainer = "container:iOS Example.xcodeproj">
          </BuildableReference>
          </BuildableReference>
       </BuildableProductRunnable>
       </BuildableProductRunnable>
-      <EnvironmentVariables>
-         <EnvironmentVariable
-            key = "OS_ACTIVITY_MODE"
-            value = "disable"
-            isEnabled = "YES">
-         </EnvironmentVariable>
-      </EnvironmentVariables>
       <AdditionalOptions>
       <AdditionalOptions>
+         <AdditionalOption
+            key = "MallocStackLogging"
+            value = ""
+            isEnabled = "YES">
+         </AdditionalOption>
+         <AdditionalOption
+            key = "PrefersMallocStackLoggingLite"
+            value = ""
+            isEnabled = "YES">
+         </AdditionalOption>
       </AdditionalOptions>
       </AdditionalOptions>
    </LaunchAction>
    </LaunchAction>
    <ProfileAction
    <ProfileAction

+ 3 - 0
Source/NetworkReachabilityManager.swift

@@ -159,7 +159,10 @@ open class NetworkReachabilityManager {
         let queueEnabled = SCNetworkReachabilitySetDispatchQueue(reachability, listenerQueue)
         let queueEnabled = SCNetworkReachabilitySetDispatchQueue(reachability, listenerQueue)
 
 
         listenerQueue.async {
         listenerQueue.async {
+            self.previousFlags = SCNetworkReachabilityFlags(rawValue: 1 << 30)
+
             guard let flags = self.flags else { return }
             guard let flags = self.flags else { return }
+
             self.notifyListener(flags)
             self.notifyListener(flags)
         }
         }
 
 

+ 1 - 1
Source/Request.swift

@@ -500,7 +500,7 @@ open class DownloadRequest: Request {
     override open func cancel() {
     override open func cancel() {
         cancel(createResumeData: true)
         cancel(createResumeData: true)
     }
     }
-    
+
     /// Cancels the request.
     /// Cancels the request.
     ///
     ///
     /// - parameter createResumeData: Determines whether resume data is created via the underlying download task or not.
     /// - parameter createResumeData: Determines whether resume data is created via the underlying download task or not.

+ 8 - 2
Source/Validation.swift

@@ -219,7 +219,10 @@ extension DataRequest {
     /// - returns: The request.
     /// - returns: The request.
     @discardableResult
     @discardableResult
     public func validate() -> Self {
     public func validate() -> Self {
-        return validate(statusCode: self.acceptableStatusCodes).validate(contentType: self.acceptableContentTypes)
+        let contentTypes = { [unowned self] in
+            self.acceptableContentTypes
+        }
+        return validate(statusCode: acceptableStatusCodes).validate(contentType: contentTypes())
     }
     }
 }
 }
 
 
@@ -310,6 +313,9 @@ extension DownloadRequest {
     /// - returns: The request.
     /// - returns: The request.
     @discardableResult
     @discardableResult
     public func validate() -> Self {
     public func validate() -> Self {
-        return validate(statusCode: self.acceptableStatusCodes).validate(contentType: self.acceptableContentTypes)
+        let contentTypes = { [unowned self] in
+            self.acceptableContentTypes
+        }
+        return validate(statusCode: acceptableStatusCodes).validate(contentType: contentTypes())
     }
     }
 }
 }

+ 1 - 1
Tests/BaseTestCase.swift

@@ -27,7 +27,7 @@ import Foundation
 import XCTest
 import XCTest
 
 
 class BaseTestCase: XCTestCase {
 class BaseTestCase: XCTestCase {
-    let timeout: TimeInterval = 30.0
+    let timeout: TimeInterval = 5.0
 
 
     static var testDirectoryURL: URL { return FileManager.temporaryDirectoryURL.appendingPathComponent("org.alamofire.tests") }
     static var testDirectoryURL: URL { return FileManager.temporaryDirectoryURL.appendingPathComponent("org.alamofire.tests") }
     var testDirectoryURL: URL { return BaseTestCase.testDirectoryURL }
     var testDirectoryURL: URL { return BaseTestCase.testDirectoryURL }

+ 7 - 7
Tests/DownloadTests.swift

@@ -515,19 +515,19 @@ class DownloadResumeDataTestCase: BaseTestCase {
 
 
         progressValues.forEach { XCTAssertGreaterThanOrEqual($0, 0.4) }
         progressValues.forEach { XCTAssertGreaterThanOrEqual($0, 0.4) }
     }
     }
-    
+
     func testThatDownloadCanBeCancelledWithoutResumeData() {
     func testThatDownloadCanBeCancelledWithoutResumeData() {
         // Given
         // Given
         let expectation = self.expectation(description: "Download should be cancelled")
         let expectation = self.expectation(description: "Download should be cancelled")
         var cancelled = false
         var cancelled = false
-        
+
         var response: DefaultDownloadResponse?
         var response: DefaultDownloadResponse?
-        
+
         // When
         // When
         let download = Alamofire.download(urlString)
         let download = Alamofire.download(urlString)
         download.downloadProgress { progress in
         download.downloadProgress { progress in
             guard !cancelled else { return }
             guard !cancelled else { return }
-            
+
             if progress.fractionCompleted > 0.1 {
             if progress.fractionCompleted > 0.1 {
                 download.cancel(createResumeData: false)
                 download.cancel(createResumeData: false)
                 cancelled = true
                 cancelled = true
@@ -537,15 +537,15 @@ class DownloadResumeDataTestCase: BaseTestCase {
             response = resp
             response = resp
             expectation.fulfill()
             expectation.fulfill()
         }
         }
-        
+
         waitForExpectations(timeout: timeout, handler: nil)
         waitForExpectations(timeout: timeout, handler: nil)
-        
+
         // Then
         // Then
         XCTAssertNotNil(response?.request)
         XCTAssertNotNil(response?.request)
         XCTAssertNotNil(response?.response)
         XCTAssertNotNil(response?.response)
         XCTAssertNil(response?.destinationURL)
         XCTAssertNil(response?.destinationURL)
         XCTAssertNotNil(response?.error)
         XCTAssertNotNil(response?.error)
-        
+
         XCTAssertNil(response?.resumeData)
         XCTAssertNil(response?.resumeData)
         XCTAssertNil(download.resumeData)
         XCTAssertNil(download.resumeData)
     }
     }

+ 26 - 0
Tests/NetworkReachabilityManagerTests.swift

@@ -148,6 +148,32 @@ class NetworkReachabilityManagerTestCase: BaseTestCase {
         XCTAssertEqual(networkReachabilityStatus, .reachable(.ethernetOrWiFi))
         XCTAssertEqual(networkReachabilityStatus, .reachable(.ethernetOrWiFi))
     }
     }
 
 
+    func testThatZeroManagerIsNotifiedWhenStoppedAndStarted() {
+        // Given
+        let manager = NetworkReachabilityManager()
+        let first = expectation(description: "first listener notified")
+        let second = expectation(description: "second listener notified")
+
+        // When
+        manager?.listener = { _ in
+            first.fulfill()
+        }
+        manager?.startListening()
+
+        wait(for: [first], timeout: timeout)
+
+        manager?.stopListening()
+        manager?.listener = { _ in
+            second.fulfill()
+        }
+        manager?.startListening()
+
+        wait(for: [second], timeout: timeout)
+
+        // Then
+        XCTAssertEqual(manager?.networkReachabilityStatus, .reachable(.ethernetOrWiFi))
+    }
+
     // MARK: - Tests - Network Reachability Status
     // MARK: - Tests - Network Reachability Status
 
 
     func testThatManagerReturnsNotReachableStatusWhenReachableFlagIsAbsent() {
     func testThatManagerReturnsNotReachableStatusWhenReachableFlagIsAbsent() {

+ 1 - 1
Tests/SessionManagerTests.swift

@@ -251,7 +251,7 @@ class SessionManagerTestCase: BaseTestCase {
             return "Alamofire/\(build)"
             return "Alamofire/\(build)"
         }()
         }()
 
 
-        
+
         XCTAssertTrue(userAgent?.contains(alamofireVersion) == true)
         XCTAssertTrue(userAgent?.contains(alamofireVersion) == true)
         XCTAssertTrue(userAgent?.contains(osNameVersion) == true)
         XCTAssertTrue(userAgent?.contains(osNameVersion) == true)
         XCTAssertTrue(userAgent?.contains("Unknown/Unknown") == true)
         XCTAssertTrue(userAgent?.contains("Unknown/Unknown") == true)