ImagePrefetcherTests.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // ImagePrefetcherTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Claire Knight <claire.knight@moggytech.co.uk> on 24/02/2016
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import XCTest
  27. @testable import Kingfisher
  28. #if os(macOS)
  29. import AppKit
  30. #else
  31. import UIKit
  32. #endif
  33. class ImagePrefetcherTests: XCTestCase {
  34. override class func setUp() {
  35. super.setUp()
  36. LSNocilla.sharedInstance().start()
  37. }
  38. override class func tearDown() {
  39. LSNocilla.sharedInstance().stop()
  40. super.tearDown()
  41. }
  42. override func setUp() {
  43. super.setUp()
  44. cleanDefaultCache()
  45. }
  46. override func tearDown() {
  47. cleanDefaultCache()
  48. super.tearDown()
  49. }
  50. func testPrefetchingImages() {
  51. let exp = expectation(description: #function)
  52. testURLs.forEach { stub($0, data: testImageData) }
  53. var progressCalledCount = 0
  54. let prefetcher = ImagePrefetcher(
  55. urls: testURLs,
  56. options: [.waitForCache],
  57. progressBlock: { _, _, _ in progressCalledCount += 1 }) {
  58. skippedResources, failedResources, completedResources in
  59. XCTAssertEqual(skippedResources.count, 0)
  60. XCTAssertEqual(failedResources.count, 0)
  61. XCTAssertEqual(completedResources.count, testURLs.count)
  62. XCTAssertEqual(progressCalledCount, testURLs.count)
  63. for url in testURLs {
  64. XCTAssertTrue(KingfisherManager.shared.cache.imageCachedType(forKey: url.absoluteString).cached)
  65. }
  66. exp.fulfill()
  67. }
  68. prefetcher.start()
  69. waitForExpectations(timeout: 3, handler: nil)
  70. }
  71. func testCancelPrefetching() {
  72. let exp = expectation(description: #function)
  73. let stubs = testURLs.map { delayedStub($0, data: testImageData) }
  74. let maxConcurrentCount = 2
  75. let prefetcher = ImagePrefetcher(
  76. urls: testURLs,
  77. options: [.waitForCache],
  78. completionHandler: { skippedResources, failedResources, completedResources in
  79. XCTAssertEqual(skippedResources.count, 0)
  80. XCTAssertEqual(failedResources.count, testURLs.count)
  81. XCTAssertEqual(completedResources.count, 0)
  82. delay(0.1) { exp.fulfill() }
  83. }
  84. )
  85. prefetcher.maxConcurrentDownloads = maxConcurrentCount
  86. prefetcher.start()
  87. DispatchQueue.main.async {
  88. prefetcher.stop()
  89. stubs.forEach { _ = $0.go() }
  90. }
  91. waitForExpectations(timeout: 3, handler: nil)
  92. }
  93. func testPrefetcherCouldSkipCachedImages() {
  94. let exp = expectation(description: #function)
  95. KingfisherManager.shared.cache.store(KFCrossPlatformImage(), forKey: testKeys[0])
  96. testURLs.forEach { stub($0, data: testImageData) }
  97. let prefetcher = ImagePrefetcher(
  98. urls: testURLs,
  99. options: [.waitForCache],
  100. completionHandler: { skippedResources, failedResources, completedResources in
  101. XCTAssertEqual(skippedResources.count, 1)
  102. XCTAssertEqual(skippedResources[0].downloadURL, testURLs[0])
  103. XCTAssertEqual(failedResources.count, 0)
  104. XCTAssertEqual(completedResources.count, testURLs.count - 1)
  105. exp.fulfill()
  106. }
  107. )
  108. prefetcher.start()
  109. waitForExpectations(timeout: 3, handler: nil)
  110. }
  111. func testPrefetcherForceRefreshDownloadImages() {
  112. let exp = expectation(description: #function)
  113. KingfisherManager.shared.cache.store(KFCrossPlatformImage(), forKey: testKeys[0])
  114. testURLs.forEach { stub($0, data: testImageData) }
  115. let prefetcher = ImagePrefetcher(urls: testURLs, options: [.forceRefresh, .waitForCache], completionHandler: {
  116. skippedResources, failedResources, completedResources in
  117. XCTAssertEqual(skippedResources.count, 0)
  118. XCTAssertEqual(failedResources.count, 0)
  119. XCTAssertEqual(completedResources.count, testURLs.count)
  120. exp.fulfill()
  121. })
  122. prefetcher.start()
  123. waitForExpectations(timeout: 3, handler: nil)
  124. }
  125. func testPrefetchWithWrongInitParameters() {
  126. let exp = expectation(description: #function)
  127. let prefetcher = ImagePrefetcher(urls: [], options: [.waitForCache], completionHandler: {
  128. skippedResources, failedResources, completedResources in
  129. XCTAssertEqual(skippedResources.count, 0)
  130. XCTAssertEqual(failedResources.count, 0)
  131. XCTAssertEqual(completedResources.count, 0)
  132. exp.fulfill()
  133. })
  134. prefetcher.start()
  135. waitForExpectations(timeout: 3, handler: nil)
  136. }
  137. func testFetchWithProcessor() {
  138. let exp = expectation(description: #function)
  139. testURLs.forEach { stub($0, data: testImageData, length: 123) }
  140. let p = RoundCornerImageProcessor(cornerRadius: 20)
  141. func prefetchAgain() {
  142. var progressCalledCount = 0
  143. let prefetcher = ImagePrefetcher(
  144. urls: testURLs,
  145. options: [.processor(p), .waitForCache],
  146. progressBlock: { _, _, _ in progressCalledCount += 1 })
  147. {
  148. skippedResources, failedResources, completedResources in
  149. XCTAssertEqual(skippedResources.count, testURLs.count)
  150. XCTAssertEqual(failedResources.count, 0)
  151. XCTAssertEqual(completedResources.count, 0)
  152. XCTAssertEqual(progressCalledCount, testURLs.count)
  153. for url in testURLs {
  154. let cached = KingfisherManager.shared.cache.imageCachedType(
  155. forKey: url.absoluteString, processorIdentifier: p.identifier).cached
  156. XCTAssertTrue(cached)
  157. }
  158. exp.fulfill()
  159. }
  160. prefetcher.start()
  161. }
  162. var progressCalledCount = 0
  163. let prefetcher = ImagePrefetcher(
  164. urls: testURLs,
  165. options: [.processor(p), .waitForCache],
  166. progressBlock: { _, _, _ in progressCalledCount += 1 })
  167. {
  168. skippedResources, failedResources, completedResources in
  169. XCTAssertEqual(skippedResources.count, 0)
  170. XCTAssertEqual(failedResources.count, 0)
  171. XCTAssertEqual(completedResources.count, testURLs.count)
  172. XCTAssertEqual(progressCalledCount, testURLs.count)
  173. for url in testURLs {
  174. let cached = KingfisherManager.shared.cache.imageCachedType(
  175. forKey: url.absoluteString, processorIdentifier: p.identifier).cached
  176. XCTAssertTrue(cached)
  177. }
  178. prefetchAgain()
  179. }
  180. prefetcher.start()
  181. waitForExpectations(timeout: 3, handler: nil)
  182. }
  183. func testAlsoPrefetchToMemory() {
  184. let exp = expectation(description: #function)
  185. let cache = KingfisherManager.shared.cache
  186. let key = testKeys[0]
  187. cache.store(KFCrossPlatformImage(), forKey: key)
  188. cache.store(testImage, forKey: key) { result in
  189. cache.memoryStorage.remove(forKey: key)
  190. XCTAssertEqual(cache.imageCachedType(forKey: key), .disk)
  191. testURLs.forEach { stub($0, data: testImageData) }
  192. let prefetcher = ImagePrefetcher(
  193. urls: testURLs,
  194. options: [.waitForCache, .alsoPrefetchToMemory],
  195. completionHandler: { skippedResources, failedResources, completedResources in
  196. XCTAssertEqual(cache.imageCachedType(forKey: key), .memory)
  197. XCTAssertEqual(skippedResources.count, 1)
  198. XCTAssertEqual(skippedResources[0].downloadURL, testURLs[0])
  199. XCTAssertEqual(failedResources.count, 0)
  200. XCTAssertEqual(completedResources.count, testURLs.count - 1)
  201. exp.fulfill()
  202. }
  203. )
  204. prefetcher.start()
  205. }
  206. waitForExpectations(timeout: 3, handler: nil)
  207. }
  208. func testNotPrefetchToMemory() {
  209. let exp = expectation(description: #function)
  210. let cache = KingfisherManager.shared.cache
  211. let key = testKeys[0]
  212. cache.store(testImage, forKey: key) { result in
  213. cache.memoryStorage.remove(forKey: key)
  214. XCTAssertEqual(cache.imageCachedType(forKey: key), .disk)
  215. testURLs.forEach { stub($0, data: testImageData) }
  216. let prefetcher = ImagePrefetcher(
  217. urls: testURLs,
  218. options: [.waitForCache],
  219. completionHandler: { skippedResources, failedResources, completedResources in
  220. XCTAssertEqual(cache.imageCachedType(forKey: key), .disk)
  221. XCTAssertEqual(skippedResources.count, 1)
  222. XCTAssertEqual(skippedResources[0].downloadURL, testURLs[0])
  223. XCTAssertEqual(failedResources.count, 0)
  224. XCTAssertEqual(completedResources.count, testURLs.count - 1)
  225. exp.fulfill()
  226. }
  227. )
  228. prefetcher.start()
  229. }
  230. waitForExpectations(timeout: 3, handler: nil)
  231. }
  232. func testPrefetchMoreTaskThanMaxConcurrency() {
  233. let exp = expectation(description: #function)
  234. testURLs.forEach { stub($0, data: testImageData) }
  235. let prefetcher = ImagePrefetcher(
  236. urls: testURLs,
  237. options: [.waitForCache],
  238. completionHandler: { skippedResources, failedResources, completedResources in
  239. XCTAssertEqual(skippedResources.count, 0)
  240. XCTAssertEqual(failedResources.count, 0)
  241. XCTAssertEqual(completedResources.count, testURLs.count)
  242. exp.fulfill()
  243. }
  244. )
  245. prefetcher.maxConcurrentDownloads = 1
  246. prefetcher.start()
  247. waitForExpectations(timeout: 3, handler: nil)
  248. }
  249. func testPrefetchMultiTimes() {
  250. let exp = expectation(description: #function)
  251. let group = DispatchGroup()
  252. testURLs.forEach { stub($0, data: testImageData) }
  253. for _ in 0..<10000 {
  254. group.enter()
  255. let prefetcher = ImagePrefetcher(
  256. resources: testURLs,
  257. options: [.cacheMemoryOnly],
  258. completionHandler: {
  259. _, _, _ in group.leave()
  260. }
  261. )
  262. prefetcher.start()
  263. }
  264. group.notify(queue: .main) { exp.fulfill() }
  265. waitForExpectations(timeout: 15, handler: nil)
  266. }
  267. func testPrefetchSources() {
  268. let exp = expectation(description: #function)
  269. let url = testURLs[0]
  270. stub(url, data: testImageData)
  271. let sources: [Source] = [
  272. .provider(SimpleImageDataProvider(cacheKey: "1") { .success(testImageData) }),
  273. .provider(SimpleImageDataProvider(cacheKey: "2") { .success(testImageData) }),
  274. .network(url)
  275. ]
  276. var counter = 0
  277. let prefetcher = ImagePrefetcher(
  278. sources: sources,
  279. options: [.waitForCache],
  280. progressBlock: {
  281. skipped, failed, completed in
  282. counter += 1
  283. XCTAssertEqual(skipped.count, 0)
  284. XCTAssertEqual(failed.count, 0)
  285. XCTAssertEqual(completed.count, counter)
  286. },
  287. completionHandler: {
  288. skipped, failed, completed in
  289. XCTAssertEqual(skipped.count, 0)
  290. XCTAssertEqual(failed.count, 0)
  291. XCTAssertEqual(completed.count, sources.count)
  292. XCTAssertEqual(counter, sources.count)
  293. let allCached = [ImageCache.default.isCached(forKey: "1"),
  294. ImageCache.default.isCached(forKey: "2"),
  295. ImageCache.default.isCached(forKey: url.absoluteString)
  296. ].allSatisfy { $0 == true }
  297. XCTAssertTrue(allCached)
  298. exp.fulfill()
  299. })
  300. prefetcher.start()
  301. waitForExpectations(timeout: 3, handler: nil)
  302. }
  303. }