Fix the wrong test because now ioQueue and encodeQueue is different thing, we should not assume calling storeImage and queryDiskCache get the data (they are not guarded by the same serial queue)

This commit is contained in:
DreamPiggy 2023-01-12 15:20:51 +08:00
parent e083ab293f
commit 911a9a86a0
2 changed files with 88 additions and 67 deletions

View File

@ -36,20 +36,21 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
- (void)test04ClearDiskCache{
XCTestExpectation *expectation = [self expectationWithDescription:@"Clear disk cache"];
[[SDImageCache sharedImageCache] storeImage:[self testJPEGImage] forKey:kTestImageKeyJPEG completion:nil];
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.equal([self testJPEGImage]);
[[SDImageCache sharedImageCache] diskImageExistsWithKey:kTestImageKeyJPEG completion:^(BOOL isInCache) {
if (!isInCache) {
[[SDImageCache sharedImageCache] calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
expect(fileCount).to.equal(0);
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG withCompletion:^{
[expectation fulfill];
[[SDImageCache sharedImageCache] storeImage:[self testJPEGImage] forKey:kTestImageKeyJPEG completion:^{
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.equal([self testJPEGImage]);
[[SDImageCache sharedImageCache] diskImageExistsWithKey:kTestImageKeyJPEG completion:^(BOOL isInCache) {
if (!isInCache) {
[[SDImageCache sharedImageCache] calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
expect(fileCount).to.equal(0);
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG withCompletion:^{
[expectation fulfill];
}];
}];
}];
} else {
XCTFail(@"Image should not be in cache");
}
} else {
XCTFail(@"Image should not be in cache");
}
}];
}];
}];
[self waitForExpectationsWithCommonTimeout];
@ -79,17 +80,22 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
XCTestExpectation *expectation = [self expectationWithDescription:@"storeImage forKey"];
UIImage *image = [self testJPEGImage];
[[SDImageCache sharedImageCache] storeImage:image forKey:kTestImageKeyJPEG completion:nil];
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.equal(image);
[[SDImageCache sharedImageCache] diskImageExistsWithKey:kTestImageKeyJPEG completion:^(BOOL isInCache) {
if (isInCache) {
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG withCompletion:^{
[expectation fulfill];
}];
} else {
XCTFail(@"Image should be in cache");
}
[[SDImageCache sharedImageCache] removeImageFromDiskForKey:kTestImageKeyJPEG];
[[SDImageCache sharedImageCache] removeImageFromMemoryForKey:kTestImageKeyJPEG];
[[SDImageCache sharedImageCache] storeImage:image forKey:kTestImageKeyJPEG completion:^{
// Disk cache store in async
[[SDImageCache sharedImageCache] diskImageExistsWithKey:kTestImageKeyJPEG completion:^(BOOL isInCache) {
if (isInCache) {
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG withCompletion:^{
[expectation fulfill];
}];
} else {
XCTFail(@"Image should be in cache");
}
}];
}];
// Memory cache store in sync
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.equal(image);
[self waitForExpectationsWithCommonTimeout];
}
@ -98,17 +104,20 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
XCTestExpectation *expectation = [self expectationWithDescription:@"storeImage forKey toDisk=YES"];
UIImage *image = [self testJPEGImage];
[[SDImageCache sharedImageCache] storeImage:image forKey:kTestImageKeyJPEG toDisk:YES completion:nil];
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.equal(image);
[[SDImageCache sharedImageCache] diskImageExistsWithKey:kTestImageKeyJPEG completion:^(BOOL isInCache) {
if (isInCache) {
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG withCompletion:^{
[expectation fulfill];
}];
} else {
XCTFail(@"Image should be in cache");
}
[[SDImageCache sharedImageCache] storeImage:image forKey:kTestImageKeyJPEG toDisk:YES completion:^{
// Disk cache store in async
[[SDImageCache sharedImageCache] diskImageExistsWithKey:kTestImageKeyJPEG completion:^(BOOL isInCache) {
if (isInCache) {
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG withCompletion:^{
[expectation fulfill];
}];
} else {
XCTFail(@"Image should be in cache");
}
}];
}];
// Memory cache store in sync
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.equal(image);
[self waitForExpectationsWithCommonTimeout];
}
@ -116,16 +125,20 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
- (void)test08InsertionOfImageOnlyInMemory {
XCTestExpectation *expectation = [self expectationWithDescription:@"storeImage forKey toDisk=NO"];
UIImage *image = [self testJPEGImage];
[[SDImageCache sharedImageCache] storeImage:image forKey:kTestImageKeyJPEG toDisk:NO completion:nil];
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.equal([self testJPEGImage]);
[[SDImageCache sharedImageCache] diskImageExistsWithKey:kTestImageKeyJPEG completion:^(BOOL isInCache) {
if (!isInCache) {
[expectation fulfill];
} else {
XCTFail(@"Image should not be in cache");
}
[[SDImageCache sharedImageCache] removeImageFromDiskForKey:kTestImageKeyJPEG];
[[SDImageCache sharedImageCache] removeImageFromMemoryForKey:kTestImageKeyJPEG];
[[SDImageCache sharedImageCache] storeImage:image forKey:kTestImageKeyJPEG toDisk:NO completion:^{
// Disk cache store in async
[[SDImageCache sharedImageCache] diskImageExistsWithKey:kTestImageKeyJPEG completion:^(BOOL isInCache) {
if (!isInCache) {
[expectation fulfill];
} else {
XCTFail(@"Image should not be in cache");
}
}];
}];
// Memory cache store in sync
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.equal([self testJPEGImage]);
[[SDImageCache sharedImageCache] storeImageToMemory:image forKey:kTestImageKeyJPEG];
[[SDImageCache sharedImageCache] clearMemory];
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.beNil();
@ -135,46 +148,50 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
- (void)test09RetrieveImageThroughNSOperation {
XCTestExpectation *expectation = [self expectationWithDescription:@"queryCacheOperationForKey"];
UIImage *imageForTesting = [self testJPEGImage];
[[SDImageCache sharedImageCache] storeImage:imageForTesting forKey:kTestImageKeyJPEG completion:nil];
id<SDWebImageOperation> operation = [[SDImageCache sharedImageCache] queryCacheOperationForKey:kTestImageKeyJPEG done:^(UIImage *image, NSData *data, SDImageCacheType cacheType) {
expect(image).to.equal(imageForTesting);
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG withCompletion:^{
[expectation fulfill];
[[SDImageCache sharedImageCache] storeImage:imageForTesting forKey:kTestImageKeyJPEG completion:^{
id<SDWebImageOperation> operation = [[SDImageCache sharedImageCache] queryCacheOperationForKey:kTestImageKeyJPEG done:^(UIImage *image, NSData *data, SDImageCacheType cacheType) {
expect(image).to.equal(imageForTesting);
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG withCompletion:^{
[expectation fulfill];
}];
}];
expect(operation).toNot.beNil;
}];
expect(operation).toNot.beNil;
[self waitForExpectationsWithCommonTimeout];
}
- (void)test10RemoveImageForKeyWithCompletion {
XCTestExpectation *expectation = [self expectationWithDescription:@"removeImageForKey"];
[[SDImageCache sharedImageCache] storeImage:[self testJPEGImage] forKey:kTestImageKeyJPEG completion:nil];
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG withCompletion:^{
expect([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:kTestImageKeyJPEG]).to.beNil;
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.beNil;
[expectation fulfill];
[[SDImageCache sharedImageCache] storeImage:[self testJPEGImage] forKey:kTestImageKeyJPEG completion:^{
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG withCompletion:^{
expect([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:kTestImageKeyJPEG]).to.beNil;
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.beNil;
[expectation fulfill];
}];
}];
[self waitForExpectationsWithCommonTimeout];
}
- (void)test11RemoveImageforKeyNotFromDiskWithCompletion{
XCTestExpectation *expectation = [self expectationWithDescription:@"removeImageForKey fromDisk:NO"];
[[SDImageCache sharedImageCache] storeImage:[self testJPEGImage] forKey:kTestImageKeyJPEG completion:nil];
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG fromDisk:NO withCompletion:^{
expect([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:kTestImageKeyJPEG]).toNot.beNil;
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.beNil;
[expectation fulfill];
[[SDImageCache sharedImageCache] storeImage:[self testJPEGImage] forKey:kTestImageKeyJPEG completion:^{
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG fromDisk:NO withCompletion:^{
expect([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:kTestImageKeyJPEG]).toNot.beNil;
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.beNil;
[expectation fulfill];
}];
}];
[self waitForExpectationsWithCommonTimeout];
}
- (void)test12RemoveImageforKeyFromDiskWithCompletion{
XCTestExpectation *expectation = [self expectationWithDescription:@"removeImageForKey fromDisk:YES"];
[[SDImageCache sharedImageCache] storeImage:[self testJPEGImage] forKey:kTestImageKeyJPEG completion:nil];
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG fromDisk:YES withCompletion:^{
expect([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:kTestImageKeyJPEG]).to.beNil;
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.beNil;
[expectation fulfill];
[[SDImageCache sharedImageCache] storeImage:[self testJPEGImage] forKey:kTestImageKeyJPEG completion:^{
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG fromDisk:YES withCompletion:^{
expect([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:kTestImageKeyJPEG]).to.beNil;
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.beNil;
[expectation fulfill];
}];
}];
[self waitForExpectationsWithCommonTimeout];
}

View File

@ -389,7 +389,11 @@
NSString *originalKey = [SDWebImageManager.sharedManager cacheKeyForURL:url];
NSString *transformedKey = [SDWebImageManager.sharedManager cacheKeyForURL:url context:@{SDWebImageContextImageTransformer : transformer}];
[[SDWebImageManager sharedManager] loadImageWithURL:url options:0 context:@{SDWebImageContextImageTransformer : transformer, SDWebImageContextOriginalStoreCacheType : @(SDImageCacheTypeAll)} progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
[[SDImageCache sharedImageCache] removeImageFromDiskForKey:originalKey];
[[SDImageCache sharedImageCache] removeImageFromMemoryForKey:originalKey];
[[SDImageCache sharedImageCache] removeImageFromDiskForKey:transformedKey];
[[SDImageCache sharedImageCache] removeImageFromMemoryForKey:transformedKey];
[[SDWebImageManager sharedManager] loadImageWithURL:url options:SDWebImageWaitStoreCache context:@{SDWebImageContextImageTransformer : transformer, SDWebImageContextOriginalStoreCacheType : @(SDImageCacheTypeAll)} progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
// Get the transformed image
expect(image).equal(transformer.testImage);
// Now, the original image is stored into memory/disk cache
@ -400,7 +404,7 @@
expect(image).equal(transformedImage);
[SDImageCache.sharedImageCache removeImageFromDiskForKey:transformedKey];
[SDImageCache.sharedImageCache removeImageFromMemoryForKey:transformedKey];
[SDWebImageManager.sharedManager loadImageWithURL:url options:SDWebImageFromCacheOnly context:@{SDWebImageContextImageTransformer : transformer, SDWebImageContextOriginalQueryCacheType : @(SDImageCacheTypeAll)} progress:nil completed:^(UIImage * _Nullable image2, NSData * _Nullable data2, NSError * _Nullable error2, SDImageCacheType cacheType2, BOOL finished2, NSURL * _Nullable imageURL2) {
[SDWebImageManager.sharedManager loadImageWithURL:url options:SDWebImageWaitStoreCache | SDWebImageFromCacheOnly context:@{SDWebImageContextImageTransformer : transformer, SDWebImageContextOriginalQueryCacheType : @(SDImageCacheTypeAll)} progress:nil completed:^(UIImage * _Nullable image2, NSData * _Nullable data2, NSError * _Nullable error2, SDImageCacheType cacheType2, BOOL finished2, NSURL * _Nullable imageURL2) {
// Get the transformed image
expect(image2).equal(transformer.testImage);
expect(data).beNil(); // Currently, the thumbnailed and transformed image always data is nil, to avoid confuse user (the image and data represent no longer match)
@ -510,7 +514,7 @@
NSString *thumbnailKey = SDThumbnailedKeyForKey(fullSizeKey, thumbnailSize, YES);
[SDImageCache.sharedImageCache removeImageFromDiskForKey:thumbnailKey];
// Load with thumbnail, should use full size cache instead to decode and scale down
[SDWebImageManager.sharedManager loadImageWithURL:url options:0 context:@{SDWebImageContextImageThumbnailPixelSize : @(thumbnailSize)} progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
[SDWebImageManager.sharedManager loadImageWithURL:url options:SDWebImageWaitStoreCache context:@{SDWebImageContextImageThumbnailPixelSize : @(thumbnailSize)} progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
expect(image.size).equal(thumbnailSize);
expect(data).beNil(); // Currently, the thumbnailed and transformed image always data is nil, to avoid confuse user (the image and data represent no longer match)
expect(cacheType).equal(SDImageCacheTypeDisk);
@ -598,7 +602,7 @@
};
NSString *key = [SDWebImageManager.sharedManager cacheKeyForURL:url context:context]; // Thumbnail key
[SDImageCache.sharedImageCache removeImageFromDiskForKey:key];
[SDWebImageManager.sharedManager loadImageWithURL:url options:0 context:context progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
[SDWebImageManager.sharedManager loadImageWithURL:url options:SDWebImageWaitStoreCache context:context progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
expect(cacheType).equal(SDImageCacheTypeDisk);
expect(data).beNil(); // Currently, the thumbnailed and transformed image always data is nil, to avoid confuse user (the image and data represent no longer match)
expect(image.size.width).equal(100);