Skip to content

Commit b6910be

Browse files
committed
Added test case test31ThatMultipleRequestForSameURLNeverSkipCallback to ensure all download request for same url always callback
1 parent 0c94e54 commit b6910be

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

SDWebImage/Core/SDImageCache.m

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ @interface SDImageCache ()
6363
@property (nonatomic, strong, readwrite, nonnull) id<SDDiskCache> diskCache;
6464
@property (nonatomic, copy, readwrite, nonnull) SDImageCacheConfig *config;
6565
@property (nonatomic, copy, readwrite, nonnull) NSString *diskCachePath;
66-
@property (nonatomic, strong, nullable) dispatch_queue_t ioQueue;
66+
@property (nonatomic, strong, nonnull) dispatch_queue_t ioQueue;
6767

6868
@end
6969

@@ -508,10 +508,6 @@ - (nullable NSData *)diskImageDataBySearchingAllPathsForKey:(nullable NSString *
508508

509509
- (nullable UIImage *)diskImageForKey:(nullable NSString *)key {
510510
NSData *data = [self diskImageDataForKey:key];
511-
return [self diskImageForKey:key data:data];
512-
}
513-
514-
- (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data {
515511
return [self diskImageForKey:key data:data options:0 context:nil];
516512
}
517513

Tests/Tests/SDTestCase.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
const int64_t kAsyncTestTimeout = 5;
1313
const int64_t kMinDelayNanosecond = NSEC_PER_MSEC * 100; // 0.1s
14-
NSString *const kTestJPEGURL = @"http://via.placeholder.com/50x50.jpg";
14+
NSString *const kTestJPEGURL = @"https://via.placeholder.com/50x50.jpg";
1515
NSString *const kTestProgressiveJPEGURL = @"https://raw.githubusercontent.com/ibireme/YYImage/master/Demo/YYImageDemo/mew_progressive.jpg";
16-
NSString *const kTestPNGURL = @"http://via.placeholder.com/50x50.png";
16+
NSString *const kTestPNGURL = @"https://via.placeholder.com/50x50.png";
1717
NSString *const kTestGIFURL = @"https://media.giphy.com/media/UEsrLdv7ugRTq/giphy.gif";
1818
NSString *const kTestAPNGPURL = @"https://upload.wikimedia.org/wikipedia/commons/1/14/Animated_PNG_example_bouncing_beach_ball.png";
1919

Tests/Tests/SDWebImageDownloaderTests.m

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ - (void)test26DownloadURLSessionMetrics {
680680
expect(metric.fetchStartDate).notTo.beNil();
681681
expect(metric.connectStartDate).notTo.beNil();
682682
expect(metric.connectEndDate).notTo.beNil();
683-
expect(metric.networkProtocolName).equal(@"http/1.1");
683+
expect(metric.networkProtocolName).equal(@"h2");
684684
expect(metric.resourceFetchType).equal(NSURLSessionTaskMetricsResourceFetchTypeNetworkLoad);
685685
expect(metric.isProxyConnection).beFalsy();
686686
expect(metric.isReusedConnection).beFalsy();
@@ -799,8 +799,39 @@ - (void)test30ThatDifferentThumbnailLoadShouldCallbackDifferentSize {
799799
[self waitForExpectationsWithTimeout:kAsyncTestTimeout * 5 handler:nil];
800800
}
801801

802+
- (void)test31ThatMultipleRequestForSameURLNeverSkipCallback {
803+
// See #3475
804+
// When multiple download request for same URL, the SDWebImageDownloader will try to `Re-use` URLSessionTask to avoid duplicate actual network request
805+
// However, if caller submit too frequently in another queue, we should stop attaching more callback once the URLSessionTask `didCompleteWithError:` is called
806+
NSURL *url = [NSURL fileURLWithPath:[self testPNGPath]];
807+
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray arrayWithCapacity:100];
808+
__block void (^recursiveBlock)(int);
809+
void (^mainBlock)(int) = ^(int i) {
810+
if (i > 200) return;
811+
NSString *desc = [NSString stringWithFormat:@"Local url with index %d not callback!", i];
812+
XCTestExpectation *expectation = [self expectationWithDescription:desc];
813+
[expectations addObject:expectation];
814+
// Delay 0.01s ~ 0.99s for each download request, simulate the real-world call site
815+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * 10000000ull)), dispatch_get_main_queue(), ^{
816+
[SDWebImageDownloader.sharedDownloader downloadImageWithURL:url completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
817+
if (image) {
818+
NSLog(@"Local url callback with index: %d", i);
819+
[expectation fulfill];
820+
} else {
821+
XCTFail(@"Something went wrong: %@", error.description);
822+
}
823+
}];
824+
});
825+
recursiveBlock(i+1);
826+
};
827+
recursiveBlock = mainBlock;
828+
recursiveBlock(0);
829+
830+
[self waitForExpectations:expectations timeout:kAsyncTestTimeout * 2];
831+
}
832+
802833
#pragma mark - SDWebImageLoader
803-
- (void)test30CustomImageLoaderWorks {
834+
- (void)testCustomImageLoaderWorks {
804835
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"];
805836
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
806837
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
@@ -820,7 +851,7 @@ - (void)test30CustomImageLoaderWorks {
820851
[self waitForExpectationsWithCommonTimeout];
821852
}
822853

823-
- (void)test31ThatLoadersManagerWorks {
854+
- (void)testThatLoadersManagerWorks {
824855
XCTestExpectation *expectation = [self expectationWithDescription:@"Loaders manager not works"];
825856
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
826857
SDImageLoadersManager *manager = [[SDImageLoadersManager alloc] init];

0 commit comments

Comments
 (0)