Explorar el Código

Fulfill as soon as possible

onevcat hace 8 años
padre
commit
ef566e2b87

+ 45 - 1
Tests/KingfisherTests/ImageDownloaderTests.swift

@@ -263,7 +263,8 @@ class ImageDownloaderTests: XCTestCase {
                 XCTAssertEqual(error!.code, NSURLErrorCancelled)
                 XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
                 
-                delay(0.1, block: expectation.fulfill)
+//                delay(0.1, block: expectation.fulfill)
+                expectation.fulfill()
         }
         
         XCTAssertNotNil(downloadTask)
@@ -274,6 +275,49 @@ class ImageDownloaderTests: XCTestCase {
         waitForExpectations(timeout: 5, handler: nil)
     }
     
+    // Issue 532 https://github.com/onevcat/Kingfisher/issues/532#issuecomment-305644311
+    func _testCancelThenRestartSameDownload() {
+        let expectation = self.expectation(description: "wait for downloading")
+        
+        let URLString = testKeys[0]
+        let stub = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)?.delay()
+        let url = URL(string: URLString)!
+        
+        var progressBlockIsCalled = false
+        
+        let group = DispatchGroup()
+        
+        group.enter()
+        let downloadTask = downloader.downloadImage(with: url, progressBlock: { (receivedSize, totalSize) -> () in
+            progressBlockIsCalled = true
+        }) { (image, error, imageURL, originalData) -> () in
+            XCTAssertNotNil(error)
+            XCTAssertEqual(error!.code, NSURLErrorCancelled)
+            XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
+            group.leave()
+        }
+        
+        XCTAssertNotNil(downloadTask)
+        
+        downloadTask!.cancel()
+        _ = stub!.go()
+        
+        group.enter()
+        downloader.downloadImage(with: url, progressBlock: { (receivedSize, totalSize) -> () in
+            progressBlockIsCalled = true
+        }) { (image, error, imageURL, originalData) -> () in
+            XCTAssertNotNil(image)
+            group.leave()
+        }
+        
+        group.notify(queue: .main) { 
+//            delay(0.1, block: expectation.fulfill)
+            expectation.fulfill()
+        }
+        
+        waitForExpectations(timeout: 5, handler: nil)
+    }
+    
     func testDownloadTaskNil() {
         modifier.url = nil
         let downloadTask = downloader.downloadImage(with: URL(string: "url")!, options: [.requestModifier(modifier)], progressBlock: nil, completionHandler: nil)

+ 1 - 1
Tests/KingfisherTests/ImagePrefetcherTests.swift

@@ -102,10 +102,10 @@ class ImagePrefetcherTests: XCTestCase {
                             progressBlock: { (skippedResources, failedResources, completedResources) -> () in
                             },
                             completionHandler: {(skippedResources, failedResources, completedResources) -> () in
-                                expectation.fulfill()
                                 XCTAssertEqual(skippedResources.count, 0, "There should be no items skipped.")
                                 XCTAssertEqual(failedResources.count, urls.count, "The failed count should be the same with started downloads due to cancellation.")
                                 XCTAssertEqual(completedResources.count, 0, "None resources prefetching should complete.")
+                                expectation.fulfill()
                             })
         
         prefetcher.maxConcurrentDownloads = maxConcurrentCount

+ 14 - 7
Tests/KingfisherTests/ImageViewExtensionTests.swift

@@ -148,7 +148,8 @@ class ImageViewExtensionTests: XCTestCase {
         }) { (image, error, cacheType, imageURL) -> () in
             XCTAssertEqual(error?.code, KingfisherError.downloadCancelledBeforeStarting.rawValue, "The error should be downloadCancelledBeforeStarting")
             XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
-            delay(0.1, block: expectation.fulfill)
+//            delay(0.1, block: expectation.fulfill)
+            expectation.fulfill()
         }
 
         task.cancel()
@@ -172,7 +173,8 @@ class ImageViewExtensionTests: XCTestCase {
                 XCTAssertNotNil(error)
                 XCTAssertEqual(error?.code, NSURLErrorCancelled)
                 XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
-                delay(0.1, block: expectation.fulfill)
+//                delay(0.1, block: expectation.fulfill)
+                expectation.fulfill()
         }
         
         delay(0.1) { 
@@ -221,7 +223,8 @@ class ImageViewExtensionTests: XCTestCase {
         delay(0.1) { _ = stub!.go() }
         
         group.notify(queue: .main) { 
-            delay(0.1, block: expectation.fulfill)
+//            delay(0.1, block: expectation.fulfill)
+            expectation.fulfill()
         }
         
         waitForExpectations(timeout: 5, handler: nil)
@@ -266,7 +269,8 @@ class ImageViewExtensionTests: XCTestCase {
         }
         
         group.notify(queue: .main) { 
-            delay(0.1, block: expectation.fulfill)
+//            delay(0.1, block: expectation.fulfill)
+            expectation.fulfill()
         }
         
         waitForExpectations(timeout: 5, handler: nil)
@@ -317,7 +321,8 @@ class ImageViewExtensionTests: XCTestCase {
         }
         
         group.notify(queue: .main) { 
-            delay(0.1, block: expectation.fulfill)
+//            delay(0.1, block: expectation.fulfill)
+            expectation.fulfill()
         }
         
         waitForExpectations(timeout: 5, handler: nil)
@@ -481,7 +486,8 @@ class ImageViewExtensionTests: XCTestCase {
         }
         
         group.notify(queue: .main) { 
-            delay(0.1, block: expectation.fulfill)
+//            delay(0.1, block: expectation.fulfill)
+            expectation.fulfill()
         }
         
         waitForExpectations(timeout: 5, handler: nil)
@@ -599,7 +605,8 @@ class ImageViewExtensionTests: XCTestCase {
         }
         
         group.notify(queue: .main) { 
-            delay(0.1, block: expectation.fulfill)
+//            delay(0.1, block: expectation.fulfill)
+            expectation.fulfill()
         }
         
         waitForExpectations(timeout: 1, handler: nil)

+ 4 - 2
Tests/KingfisherTests/NSButtonExtensionTests.swift

@@ -123,7 +123,8 @@ class NSButtonExtensionTests: XCTestCase {
             XCTAssertNotNil(error)
             XCTAssertEqual(error?.code, NSURLErrorCancelled)
 
-            delay(0.1, block: expectation.fulfill)
+//            delay(0.1, block: expectation.fulfill)
+            expectation.fulfill()
         }
         
         delay(0.1) { 
@@ -147,7 +148,8 @@ class NSButtonExtensionTests: XCTestCase {
             XCTAssertNotNil(error)
             XCTAssertEqual(error?.code, NSURLErrorCancelled)
 
-            delay(0.1, block: expectation.fulfill)
+//            delay(0.1, block: expectation.fulfill)
+            expectation.fulfill()
         }
 
         delay(0.1) { 

+ 4 - 2
Tests/KingfisherTests/UIButtonExtensionTests.swift

@@ -123,7 +123,8 @@ class UIButtonExtensionTests: XCTestCase {
                 XCTAssertNotNil(error)
                 XCTAssertEqual(error?.code, NSURLErrorCancelled)
 
-                delay(0.1, block: expectation.fulfill)
+//                delay(0.1, block: expectation.fulfill)
+                expectation.fulfill()
         }
         delay(0.1) { 
             self.button.kf.cancelImageDownloadTask()
@@ -146,7 +147,8 @@ class UIButtonExtensionTests: XCTestCase {
                 XCTAssertNotNil(error)
                 XCTAssertEqual(error?.code, NSURLErrorCancelled)
                 
-                delay(0.1, block: expectation.fulfill)
+//                delay(0.1, block: expectation.fulfill)
+                expectation.fulfill()
         }
         delay(0.1) { 
             self.button.kf.cancelBackgroundImageDownloadTask()