Merge pull request #3497 from dreampiggy/bugfix/same_url_failed_callback_times

Fix the issue when multiple request for same url failed, the completedBlock will callback more times
This commit is contained in:
DreamPiggy 2023-02-18 17:57:14 +08:00 committed by GitHub
commit 940f991ca1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -601,7 +601,8 @@ didReceiveResponse:(NSURLResponse *)response
CGSize imageSize = image.size;
if (imageSize.width == 0 || imageSize.height == 0) {
NSString *description = image == nil ? @"Downloaded image decode failed" : @"Downloaded image has 0 pixels";
[self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorBadImageData userInfo:@{NSLocalizedDescriptionKey : description}]];
NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorBadImageData userInfo:@{NSLocalizedDescriptionKey : description}];
[self callCompletionBlockWithToken:token image:nil imageData:nil error:error finished:YES];
} else {
[self callCompletionBlockWithToken:token image:image imageData:imageData error:nil finished:YES];
}

View File

@ -830,6 +830,35 @@
[self waitForExpectations:expectations timeout:kAsyncTestTimeout * 2];
}
- (void)test31ThatMultipleRequestForSameURLFailedCallback {
// See #3493, silly bug
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"]; // Always fail url
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray arrayWithCapacity:100];
__block void (^recursiveBlock)(int);
void (^mainBlock)(int) = ^(int i) {
if (i > 200) return;
NSString *desc = [NSString stringWithFormat:@"Failed url with index %d should callback error", 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 (error) {
expect(error.code).equal(SDWebImageErrorBadImageData);
[expectation fulfill];
}
}];
});
recursiveBlock(i+1);
};
recursiveBlock = mainBlock;
recursiveBlock(0);
[self waitForExpectations:expectations timeout:kAsyncTestTimeout * 2];
}
#pragma mark - SDWebImageLoader
- (void)testCustomImageLoaderWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"];