Просмотр исходного кода

Add sync clear disk cache to improve testing

onevcat 10 лет назад
Родитель
Сommit
3b6e05f670

+ 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.
     */

+ 7 - 9
KingfisherTests/ImageCacheTests.swift

@@ -45,8 +45,8 @@ class ImageCacheTests: XCTestCase {
     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 +158,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)
     }
 }