Skip to content

Commit

Permalink
Fulfill as soon as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Jun 2, 2017
1 parent 1ab5758 commit 27c3018
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
46 changes: 45 additions & 1 deletion Tests/KingfisherTests/ImageDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Tests/KingfisherTests/ImagePrefetcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions Tests/KingfisherTests/ImageViewExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions Tests/KingfisherTests/NSButtonExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions Tests/KingfisherTests/UIButtonExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 27c3018

Please sign in to comment.