Przeglądaj źródła

Merge pull request #177 from onevcat/fix/test-stability

Add sync clear disk cache to improve testing
Wei Wang 10 lat temu
rodzic
commit
5e13e86e4e

+ 20 - 13
Kingfisher/ImageCache.swift

@@ -357,12 +357,19 @@ extension ImageCache {
     @objc public func clearMemoryCache() {
         memoryCache.removeAllObjects()
     }
-    
+
     /**
-    Clear disk cache. This is an async operation.
+    Clear disk cache. This is could be an async or sync operation.
+    Specify the way you want it by passing the `sync` parameter.
+     
+    - parameter sync: If `true`, the clear process will be performed in a sync way. Otherwise, async. Default is `false`.
     */
-    public func clearDiskCache() {
-        clearDiskCacheWithCompletionHandler(nil)
+    public func clearDiskCache(sync: Bool = false) {
+        if sync {
+            clearDiskCacheSync()
+        } else {
+            clearDiskCacheWithCompletionHandler(nil)
+        }
     }
     
     /**
@@ -372,15 +379,7 @@ extension ImageCache {
     */
     public func clearDiskCacheWithCompletionHandler(completionHander: (()->())?) {
         dispatch_async(ioQueue, { () -> Void in
-            do {
-                try self.fileManager.removeItemAtPath(self.diskCachePath)
-            } catch _ {
-            }
-            do {
-                try self.fileManager.createDirectoryAtPath(self.diskCachePath, withIntermediateDirectories: true, attributes: nil)
-            } catch _ {
-            }
-            
+            self.clearDiskCacheSync()
             if let completionHander = completionHander {
                 dispatch_async(dispatch_get_main_queue(), { () -> Void in
                     completionHander()
@@ -389,6 +388,14 @@ extension ImageCache {
         })
     }
     
+    func clearDiskCacheSync() {
+        do {
+            try self.fileManager.removeItemAtPath(self.diskCachePath)
+            try self.fileManager.createDirectoryAtPath(self.diskCachePath, withIntermediateDirectories: true, attributes: nil)
+        } catch _ {
+        }
+    }
+    
     /**
     Clean expired disk cache. This is an async operation.
     */

+ 8 - 9
KingfisherTests/ImageCacheTests.swift

@@ -40,13 +40,14 @@ class ImageCacheTests: XCTestCase {
         super.setUp()
         // Put setup code here. This method is called before the invocation of each test method in the class.
         cache = ImageCache(name: "test")
+        clearCaches([cache])
     }
     
     override func tearDown() {
         // Put teardown code here. This method is called after the invocation of each test method in the class.
         super.tearDown()
-        cache.clearMemoryCache()
-        cache.clearDiskCache()
+        
+        clearCaches([cache])
         cache = nil
         observer = nil
     }
@@ -158,14 +159,12 @@ class ImageCacheTests: XCTestCase {
     func testIsImageCachedForKey() {
         let expectation = self.expectationWithDescription("wait for caching image")
         
-        let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.5 * Double(NSEC_PER_SEC)))
-        dispatch_after(delayTime, dispatch_get_main_queue()) {
-            XCTAssert(self.cache.isImageCachedForKey(testKeys[0]).cached == false, "This image should not be cached yet.")
-            self.cache.storeImage(testImage, originalData: testImageData, forKey: testKeys[0], toDisk: true) { () -> () in
-                XCTAssert(self.cache.isImageCachedForKey(testKeys[0]).cached == true, "This image should be already cached.")
-                expectation.fulfill()
-            }
+        XCTAssert(self.cache.isImageCachedForKey(testKeys[0]).cached == false, "This image should not be cached yet.")
+        self.cache.storeImage(testImage, originalData: testImageData, forKey: testKeys[0], toDisk: true) { () -> () in
+            XCTAssert(self.cache.isImageCachedForKey(testKeys[0]).cached == true, "This image should be already cached.")
+            expectation.fulfill()
         }
+
         self.waitForExpectationsWithTimeout(5, handler: nil)
     }
     

+ 1 - 1
KingfisherTests/KingfisherTestHelper.swift

@@ -49,7 +49,7 @@ func cleanDefaultCache() {
 
 func clearCaches(caches: [ImageCache]) {
     for c in caches {
-        c.clearDiskCache()
         c.clearMemoryCache()
+        c.clearDiskCache(true)
     }
 }

+ 1 - 0
KingfisherTests/UIButtonExtensionTests.swift

@@ -47,6 +47,7 @@ class UIButtonExtensionTests: XCTestCase {
         // Put setup code here. This method is called before the invocation of each test method in the class.
         button = UIButton()
         KingfisherManager.sharedManager.downloader = ImageDownloader(name: "testDownloader")
+        cleanDefaultCache()
     }
     
     override func tearDown() {

+ 1 - 0
KingfisherTests/UIImageViewExtensionTests.swift

@@ -47,6 +47,7 @@ class UIImageViewExtensionTests: XCTestCase {
         // Put setup code here. This method is called before the invocation of each test method in the class.
         imageView = UIImageView()
         KingfisherManager.sharedManager.downloader = ImageDownloader(name: "testDownloader")
+        cleanDefaultCache()
     }
     
     override func tearDown() {