From 0c8d01cbacfa602dac5800c0533f8a2cf6801d46 Mon Sep 17 00:00:00 2001 From: Andrew Breckenridge Date: Wed, 21 Feb 2024 12:20:29 -0800 Subject: [PATCH] ThreadSafetyTests: add failing test where we load and store an image --- .../ThreadSafetyTests.swift | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Tests/NukeThreadSafetyTests/ThreadSafetyTests.swift b/Tests/NukeThreadSafetyTests/ThreadSafetyTests.swift index 99d5cf7ed..d8062fe32 100644 --- a/Tests/NukeThreadSafetyTests/ThreadSafetyTests.swift +++ b/Tests/NukeThreadSafetyTests/ThreadSafetyTests.swift @@ -164,6 +164,31 @@ class ThreadSafetyTests: XCTestCase { } queue.waitUntilAllOperationsAreFinished() } + + func testDataCacheMultipleThreadAccess() { + let aURL = URL(string: "https://example.com/image-01-small.jpeg")! + let imageData = Data(repeating: 1, count: 256 * 1024) + + let expectSuccessFromCache = self.expectation(description: "one successful load, from cache") + expectSuccessFromCache.expectedFulfillmentCount = 1 + expectSuccessFromCache.assertForOverFulfill = true + + ImagePipeline.shared.cache.storeCachedData(imageData, for: ImageRequest.init(url: aURL)) + ImagePipeline.shared.loadImage(with: aURL) { result in + switch result { + case .success(let response): + if response.cacheType == .memory || response.cacheType == .disk { + expectSuccessFromCache.fulfill() + } else { + XCTFail("didn't load that just cached image data: \(response)") + } + case .failure: + XCTFail("didn't load that just cached image data") + } + } + + wait(for: [expectSuccessFromCache], timeout: 2) + } } final class OperationThreadSafetyTests: XCTestCase {