Browse Source

Fix CI test stability issues

- Increase extremely short time intervals in ImageCacheTests:
  * Disk cache expiration from 0.01s to 0.1s
  * Memory cache expiration from 0.2s to 0.5s
- Fix missing expectation in RetryStrategyTests.testDelayRetryStrategyDidRetried
- Increase fixed delays in KingfisherManagerTests from 0.1s to 0.3s and 0.2s to 0.5s
- Increase all timeout values from 1s to 3s across multiple test files

These changes improve test reliability in CI environments and slow hardware
by providing more reasonable timing margins for async operations.
onevcat 7 months ago
parent
commit
88a807f84b

+ 3 - 3
Tests/KingfisherTests/ImageCacheTests.swift

@@ -352,7 +352,7 @@ class ImageCacheTests: XCTestCase {
         let exp = expectation(description: #function)
         let key = testKeys[0]
 
-        cache.diskStorage.config.expiration = .seconds(0.01)
+        cache.diskStorage.config.expiration = .seconds(0.1)
 
         let selfCache = self.cache
         cache.store(testImage, original: testImageData, forKey: key, toDisk: true) { _ in
@@ -607,7 +607,7 @@ class ImageCacheTests: XCTestCase {
             testImage,
             original: testImageData,
             forKey: key,
-            options: KingfisherParsedOptionsInfo([.memoryCacheExpiration(.seconds(0.2))]),
+            options: KingfisherParsedOptionsInfo([.memoryCacheExpiration(.seconds(0.5))]),
             toDisk: true)
         {
             _ in
@@ -626,7 +626,7 @@ class ImageCacheTests: XCTestCase {
             testImage,
             original: testImageData,
             forKey: key,
-            options: KingfisherParsedOptionsInfo([.memoryCacheExpiration(.seconds(0.2))]),
+            options: KingfisherParsedOptionsInfo([.memoryCacheExpiration(.seconds(0.5))]),
             toDisk: true
         )
         XCTAssertEqual(self.cache.imageCachedType(forKey: key), .memory)

+ 1 - 1
Tests/KingfisherTests/ImageDataProviderTests.swift

@@ -47,7 +47,7 @@ class ImageDataProviderTests: XCTestCase {
             exp.fulfill()
         }
 
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
     
     func testLocalFileImageDataProviderAsync() async {

+ 3 - 3
Tests/KingfisherTests/ImageViewExtensionTests.swift

@@ -855,7 +855,7 @@ class ImageViewExtensionTests: XCTestCase {
             XCTAssertEqual(result.value!.originalSource.url, brokenURL)
             exp.fulfill()
         }
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
     @MainActor func testImageSettingCanCancelAlternativeSource() {
@@ -893,7 +893,7 @@ class ImageViewExtensionTests: XCTestCase {
             XCTAssertEqual(task.task.originalRequest?.url, url, "Should be the alternative url cancelled.")
         }
 
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
     @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
@@ -917,7 +917,7 @@ class ImageViewExtensionTests: XCTestCase {
             XCTAssertEqual(result.value?.originalSource.url, brokenURL)
             exp.fulfill()
         }
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
 }

+ 13 - 13
Tests/KingfisherTests/KingfisherManagerTests.swift

@@ -444,7 +444,7 @@ class KingfisherManagerTests: XCTestCase {
 
             XCTAssertEqual(imageCached, .memory)
 
-            delay(0.1) {
+            delay(0.3) {
                 manager.cache.clearMemoryCache()
                 
                 imageCached = manager.cache.imageCachedType(forKey: url.cacheKey, processorIdentifier: p.identifier)
@@ -735,7 +735,7 @@ class KingfisherManagerTests: XCTestCase {
             // Clear the memory cache.
             self.manager.cache.clearMemoryCache()
             // After some time, the disk cache should be done.
-            delay(0.2) {
+            delay(0.5) {
                 XCTAssertEqual(self.manager.cache.imageCachedType(forKey: url.cacheKey), .disk)
                 exp.fulfill()
             }
@@ -1036,7 +1036,7 @@ class KingfisherManagerTests: XCTestCase {
             exp.fulfill()
         }
 
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
     func testRetrievingErrorsWithAlternativeSource() {
@@ -1077,7 +1077,7 @@ class KingfisherManagerTests: XCTestCase {
             XCTAssertEqual(errorInfo[2].source.url, url)
         }
 
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
     func testRetrievingAlternativeSourceTaskUpdateBlockCalled() {
@@ -1110,7 +1110,7 @@ class KingfisherManagerTests: XCTestCase {
 
         XCTAssertEqual(task?.sessionTask?.task.currentRequest?.url, brokenURL)
 
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
     func testRetrievingAlternativeSourceCancelled() {
@@ -1133,7 +1133,7 @@ class KingfisherManagerTests: XCTestCase {
         }
         task?.cancel()
 
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
     func testRetrievingAlternativeSourceCanCancelUpdatedTask() {
@@ -1162,7 +1162,7 @@ class KingfisherManagerTests: XCTestCase {
             XCTAssertNotNil(result.error)
             XCTAssertTrue(result.error?.isTaskCancelled ?? false)
 
-            delay(0.1) {
+            delay(0.3) {
                 _ = dataStub.go()
                 Task {
                     let result = await called.value
@@ -1175,7 +1175,7 @@ class KingfisherManagerTests: XCTestCase {
         XCTAssertNotNil(task)
         XCTAssertTrue(task!.isInitialized)
 
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
     
     func testDownsamplingHandleScale2x() {
@@ -1203,7 +1203,7 @@ class KingfisherManagerTests: XCTestCase {
             exp.fulfill()
         }
 
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
     
     func testDownsamplingHandleScale3x() {
@@ -1230,7 +1230,7 @@ class KingfisherManagerTests: XCTestCase {
             exp.fulfill()
         }
 
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
     func testCacheCallbackCoordinatorStateChanging() {
@@ -1315,7 +1315,7 @@ class KingfisherManagerTests: XCTestCase {
         Task {
             await task.setValue(t)
         }
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
     func testCanUseCustomizeDefaultCacheSerializer() {
@@ -1340,7 +1340,7 @@ class KingfisherManagerTests: XCTestCase {
 
                 exp.fulfill()
             }
-        waitForExpectations(timeout: 1.0)
+        waitForExpectations(timeout: 3.0)
     }
 
     func testCanUseCustomizeDefaultCacheSerializerStoreEncoded() {
@@ -1366,7 +1366,7 @@ class KingfisherManagerTests: XCTestCase {
 
                 exp.fulfill()
             }
-        waitForExpectations(timeout: 1.0)
+        waitForExpectations(timeout: 3.0)
     }
     
     func testImageResultContainsDataWhenDownloaded() {

+ 8 - 8
Tests/KingfisherTests/RetryStrategyTests.swift

@@ -99,7 +99,7 @@ class RetryStrategyTests: XCTestCase {
                 exp.fulfill()
             }
         )
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
     func testDelayRetryStrategyExceededCount() {
@@ -155,7 +155,7 @@ class RetryStrategyTests: XCTestCase {
                 exp.fulfill()
             }
         }
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
     func testDelayRetryStrategyNotRetryForErrorReason() {
@@ -208,10 +208,11 @@ class RetryStrategyTests: XCTestCase {
                 exp.fulfill()
             }
         }
-        waitForExpectations(timeout: 1, handler: nil)
+        waitForExpectations(timeout: 3, handler: nil)
     }
 
     func testDelayRetryStrategyDidRetried() {
+        let exp = expectation(description: #function)
         let called = ActorBox(false)
         let source = Source.network(URL(string: "url")!)
         let retry = DelayRetryStrategy(maxRetryCount: 3, retryInterval: .seconds(0))
@@ -226,14 +227,13 @@ class RetryStrategyTests: XCTestCase {
             }
             Task {
                 await called.setValue(true)
+                let result = await called.value
+                XCTAssertTrue(result)
+                exp.fulfill()
             }
         }
-
-        Task {
-            let result = await called.value
-            XCTAssertTrue(result)
-        }
         
+        waitForExpectations(timeout: 3, handler: nil)
     }
 }