From c658b5c23d6854898cf11207c636a92846b31e3f Mon Sep 17 00:00:00 2001 From: onevcat Date: Sat, 14 Dec 2024 22:38:24 +0900 Subject: [PATCH] A small test warning on Sendable --- Tests/KingfisherTests/ImageCacheTests.swift | 36 +++++++++++---------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/Tests/KingfisherTests/ImageCacheTests.swift b/Tests/KingfisherTests/ImageCacheTests.swift index 2963ef77b..0c5e81fb1 100644 --- a/Tests/KingfisherTests/ImageCacheTests.swift +++ b/Tests/KingfisherTests/ImageCacheTests.swift @@ -43,7 +43,10 @@ class ImageCacheTests: XCTestCase { override func tearDown() { clearCaches([cache]) cache = nil - observer = nil + if let o = observer { + NotificationCenter.default.removeObserver(o) + observer = nil + } super.tearDown() } @@ -351,28 +354,27 @@ class ImageCacheTests: XCTestCase { cache.diskStorage.config.expiration = .seconds(0.01) + let selfCache = self.cache cache.store(testImage, original: testImageData, forKey: key, toDisk: true) { _ in self.observer = NotificationCenter.default.addObserver( forName: .KingfisherDidCleanDiskCache, object: self.cache, - queue: .main) { - noti in - let receivedCache = noti.object as? ImageCache - XCTAssertNotNil(receivedCache) - XCTAssertTrue(receivedCache === self.cache) - - guard let hashes = noti.userInfo?[KingfisherDiskCacheCleanedHashKey] as? [String] else { - XCTFail("Notification should contains Strings in key 'KingfisherDiskCacheCleanedHashKey'") - exp.fulfill() - return - } - - XCTAssertEqual(hashes.count, 1) - XCTAssertEqual(hashes.first!, self.cache.hash(forKey: key)) - guard let o = self.observer else { return } - NotificationCenter.default.removeObserver(o) + queue: .main + ) { noti in + let receivedCache = noti.object as? ImageCache + XCTAssertNotNil(receivedCache) + XCTAssertTrue(receivedCache === selfCache) + + guard let hashes = noti.userInfo?[KingfisherDiskCacheCleanedHashKey] as? [String] else { + XCTFail("Notification should contains Strings in key 'KingfisherDiskCacheCleanedHashKey'") exp.fulfill() + return } + + XCTAssertEqual(hashes.count, 1) + XCTAssertEqual(hashes.first!, selfCache!.hash(forKey: key)) + exp.fulfill() + } delay(1) { self.cache.cleanExpiredDiskCache()