Added test case `test31ThatMultipleRequestForSameURLNeverSkipCallback` to ensure all download request for same url always callback

This commit is contained in:
DreamPiggy 2023-02-06 17:29:08 +08:00
parent 0c94e540bc
commit b6910be155
3 changed files with 37 additions and 10 deletions

View File

@ -63,7 +63,7 @@ static NSString * _defaultDiskCacheDirectory;
@property (nonatomic, strong, readwrite, nonnull) id<SDDiskCache> diskCache;
@property (nonatomic, copy, readwrite, nonnull) SDImageCacheConfig *config;
@property (nonatomic, copy, readwrite, nonnull) NSString *diskCachePath;
@property (nonatomic, strong, nullable) dispatch_queue_t ioQueue;
@property (nonatomic, strong, nonnull) dispatch_queue_t ioQueue;
@end
@ -508,10 +508,6 @@ static NSString * _defaultDiskCacheDirectory;
- (nullable UIImage *)diskImageForKey:(nullable NSString *)key {
NSData *data = [self diskImageDataForKey:key];
return [self diskImageForKey:key data:data];
}
- (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data {
return [self diskImageForKey:key data:data options:0 context:nil];
}

View File

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

View File

@ -680,7 +680,7 @@
expect(metric.fetchStartDate).notTo.beNil();
expect(metric.connectStartDate).notTo.beNil();
expect(metric.connectEndDate).notTo.beNil();
expect(metric.networkProtocolName).equal(@"http/1.1");
expect(metric.networkProtocolName).equal(@"h2");
expect(metric.resourceFetchType).equal(NSURLSessionTaskMetricsResourceFetchTypeNetworkLoad);
expect(metric.isProxyConnection).beFalsy();
expect(metric.isReusedConnection).beFalsy();
@ -799,8 +799,39 @@
[self waitForExpectationsWithTimeout:kAsyncTestTimeout * 5 handler:nil];
}
- (void)test31ThatMultipleRequestForSameURLNeverSkipCallback {
// See #3475
// When multiple download request for same URL, the SDWebImageDownloader will try to `Re-use` URLSessionTask to avoid duplicate actual network request
// However, if caller submit too frequently in another queue, we should stop attaching more callback once the URLSessionTask `didCompleteWithError:` is called
NSURL *url = [NSURL fileURLWithPath:[self testPNGPath]];
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray arrayWithCapacity:100];
__block void (^recursiveBlock)(int);
void (^mainBlock)(int) = ^(int i) {
if (i > 200) return;
NSString *desc = [NSString stringWithFormat:@"Local url with index %d not callback!", i];
XCTestExpectation *expectation = [self expectationWithDescription:desc];
[expectations addObject:expectation];
// Delay 0.01s ~ 0.99s for each download request, simulate the real-world call site
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * 10000000ull)), dispatch_get_main_queue(), ^{
[SDWebImageDownloader.sharedDownloader downloadImageWithURL:url completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
if (image) {
NSLog(@"Local url callback with index: %d", i);
[expectation fulfill];
} else {
XCTFail(@"Something went wrong: %@", error.description);
}
}];
});
recursiveBlock(i+1);
};
recursiveBlock = mainBlock;
recursiveBlock(0);
[self waitForExpectations:expectations timeout:kAsyncTestTimeout * 2];
}
#pragma mark - SDWebImageLoader
- (void)test30CustomImageLoaderWorks {
- (void)testCustomImageLoaderWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"];
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
@ -820,7 +851,7 @@
[self waitForExpectationsWithCommonTimeout];
}
- (void)test31ThatLoadersManagerWorks {
- (void)testThatLoadersManagerWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"Loaders manager not works"];
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
SDImageLoadersManager *manager = [[SDImageLoadersManager alloc] init];