Authored by DreamPiggy

Fix prefetcher test to first clear the disk cache, and manager test to only fulfill the finished one

@@ -110,21 +110,29 @@ @@ -110,21 +110,29 @@
110 110
111 - (void)test07ThatLoadImageWithSDWebImageRefreshCachedWorks { 111 - (void)test07ThatLoadImageWithSDWebImageRefreshCachedWorks {
112 XCTestExpectation *expectation = [self expectationWithDescription:@"Image download twice with SDWebImageRefresh failed"]; 112 XCTestExpectation *expectation = [self expectationWithDescription:@"Image download twice with SDWebImageRefresh failed"];
113 - NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];  
114 - [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];  
115 - 113 + NSURL *originalImageURL = [NSURL URLWithString:@"http://via.placeholder.com/10x10.png"];
  114 + __block BOOL firstCompletion = NO;
116 [[SDWebImageManager sharedManager] loadImageWithURL:originalImageURL options:SDWebImageRefreshCached progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) { 115 [[SDWebImageManager sharedManager] loadImageWithURL:originalImageURL options:SDWebImageRefreshCached progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
117 expect(image).toNot.beNil(); 116 expect(image).toNot.beNil();
118 expect(error).to.beNil(); 117 expect(error).to.beNil();
119 // #1993, load image with SDWebImageRefreshCached twice should not fail if the first time success. 118 // #1993, load image with SDWebImageRefreshCached twice should not fail if the first time success.
120 119
121 - [[SDWebImageManager sharedManager] loadImageWithURL:originalImageURL options:SDWebImageRefreshCached progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {  
122 - expect(image).toNot.beNil();  
123 - expect(error).to.beNil();  
124 - [expectation fulfill];  
125 - }]; 120 + // Because we call completion before remove the operation from queue, so need a dispatch to avoid get the same operation again. Attention this trap.
  121 + // One way to solve this is use another `NSURL instance` because we use `NSURL` as key but not `NSString`. However, this is implementation detail and no guarantee in the future.
  122 + dispatch_async(dispatch_get_main_queue(), ^{
  123 + NSURL *newImageURL = [NSURL URLWithString:@"http://via.placeholder.com/10x10.png"];
  124 + [[SDWebImageManager sharedManager] loadImageWithURL:newImageURL options:SDWebImageRefreshCached progress:nil completed:^(UIImage * _Nullable image2, NSData * _Nullable data2, NSError * _Nullable error2, SDImageCacheType cacheType2, BOOL finished2, NSURL * _Nullable imageURL2) {
  125 + expect(image2).toNot.beNil();
  126 + expect(error2).to.beNil();
  127 + if (!firstCompletion) {
  128 + firstCompletion = YES;
  129 + [expectation fulfill];
  130 + }
  131 + }];
  132 + });
126 }]; 133 }];
127 - [self waitForExpectationsWithCommonTimeout]; 134 +
  135 + [self waitForExpectationsWithTimeout:kAsyncTestTimeout * 2 handler:nil];
128 } 136 }
129 137
130 @end 138 @end
@@ -28,21 +28,23 @@ @@ -28,21 +28,23 @@
28 @"http://via.placeholder.com/30x30.jpg", 28 @"http://via.placeholder.com/30x30.jpg",
29 @"http://via.placeholder.com/40x40.jpg"]; 29 @"http://via.placeholder.com/40x40.jpg"];
30 30
31 - __block int numberOfPrefetched = 0; 31 + __block NSUInteger numberOfPrefetched = 0;
32 32
33 - [[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:imageURLs progress:^(NSUInteger noOfFinishedUrls, NSUInteger noOfTotalUrls) {  
34 - numberOfPrefetched += 1;  
35 - expect(numberOfPrefetched).to.equal(noOfFinishedUrls);  
36 - expect(noOfFinishedUrls).to.beLessThanOrEqualTo(noOfTotalUrls);  
37 - expect(noOfTotalUrls).to.equal(imageURLs.count);  
38 - } completed:^(NSUInteger noOfFinishedUrls, NSUInteger noOfSkippedUrls) {  
39 - expect(numberOfPrefetched).to.equal(noOfFinishedUrls);  
40 - expect(noOfFinishedUrls).to.equal(imageURLs.count);  
41 - expect(noOfSkippedUrls).to.equal(0);  
42 - [expectation fulfill]; 33 + [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
  34 + [[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:imageURLs progress:^(NSUInteger noOfFinishedUrls, NSUInteger noOfTotalUrls) {
  35 + numberOfPrefetched += 1;
  36 + expect(numberOfPrefetched).to.equal(noOfFinishedUrls);
  37 + expect(noOfFinishedUrls).to.beLessThanOrEqualTo(noOfTotalUrls);
  38 + expect(noOfTotalUrls).to.equal(imageURLs.count);
  39 + } completed:^(NSUInteger noOfFinishedUrls, NSUInteger noOfSkippedUrls) {
  40 + expect(numberOfPrefetched).to.equal(noOfFinishedUrls);
  41 + expect(noOfFinishedUrls).to.equal(imageURLs.count);
  42 + expect(noOfSkippedUrls).to.equal(0);
  43 + [expectation fulfill];
  44 + }];
43 }]; 45 }];
44 46
45 - [self waitForExpectationsWithCommonTimeout]; 47 + [self waitForExpectationsWithTimeout:kAsyncTestTimeout * 3 handler:nil];
46 } 48 }
47 49
48 - (void)test03PrefetchWithEmptyArrayWillCallTheCompletionWithAllZeros { 50 - (void)test03PrefetchWithEmptyArrayWillCallTheCompletionWithAllZeros {