MemoryStorageTests.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // MemoryStorageTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2018/11/12.
  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. extension Int {
  29. public var cacheCost: Int {
  30. return 1
  31. }
  32. }
  33. #if compiler(>=6)
  34. extension Int: @retroactive CacheCostCalculable { }
  35. #else
  36. extension Int: CacheCostCalculable { }
  37. #endif
  38. class MemoryStorageTests: XCTestCase {
  39. var storage: MemoryStorage.Backend<Int>!
  40. override func setUp() {
  41. super.setUp()
  42. let config = MemoryStorage.Config(totalCostLimit: 3)
  43. storage = MemoryStorage.Backend(config: config)
  44. }
  45. override func tearDown() {
  46. storage = nil
  47. super.tearDown()
  48. }
  49. func testConfigSettingStorage() {
  50. XCTAssertEqual(storage.config.totalCostLimit, 3)
  51. XCTAssertEqual(storage.storage.totalCostLimit, 3)
  52. storage.config = MemoryStorage.Config(totalCostLimit: 10)
  53. XCTAssertEqual(storage.config.totalCostLimit, 10)
  54. XCTAssertEqual(storage.storage.totalCostLimit, 10)
  55. storage.config.countLimit = 100
  56. XCTAssertEqual(storage.config.countLimit, 100)
  57. XCTAssertEqual(storage.storage.countLimit, 100)
  58. }
  59. func testStoreAndGetValue() {
  60. XCTAssertFalse(storage.isCached(forKey: "1"))
  61. storage.store(value: 1, forKey: "1")
  62. XCTAssertTrue(storage.isCached(forKey: "1"))
  63. XCTAssertEqual(storage.value(forKey: "1"), 1)
  64. }
  65. func testStoreValueOverwriting() {
  66. storage.store(value: 1, forKey: "1")
  67. XCTAssertEqual(storage.value(forKey: "1"), 1)
  68. storage.store(value: 100, forKey: "1")
  69. XCTAssertEqual(storage.value(forKey: "1"), 100)
  70. }
  71. func testRemoveValue() {
  72. XCTAssertFalse(storage.isCached(forKey: "1"))
  73. storage.store(value: 1, forKey: "1")
  74. XCTAssertTrue(storage.isCached(forKey: "1"))
  75. storage.remove(forKey: "1")
  76. XCTAssertFalse(storage.isCached(forKey: "1"))
  77. }
  78. func testRemoveAllValues() {
  79. storage.store(value: 1, forKey: "1")
  80. storage.store(value: 2, forKey: "2")
  81. XCTAssertTrue(storage.isCached(forKey: "1"))
  82. XCTAssertTrue(storage.isCached(forKey: "2"))
  83. storage.removeAll()
  84. XCTAssertFalse(storage.isCached(forKey: "1"))
  85. XCTAssertFalse(storage.isCached(forKey: "2"))
  86. }
  87. func testStoreWithExpiration() {
  88. let exp = expectation(description: #function)
  89. XCTAssertFalse(storage.isCached(forKey: "1"))
  90. storage.store(value: 1, forKey: "1", expiration: .seconds(0.1))
  91. XCTAssertTrue(storage.isCached(forKey: "1"))
  92. XCTAssertFalse(storage.isCached(forKey: "2"))
  93. storage.store(value: 2, forKey: "2")
  94. XCTAssertTrue(storage.isCached(forKey: "2"))
  95. delay(0.2) {
  96. XCTAssertFalse(self.storage.isCached(forKey: "1"))
  97. XCTAssertTrue(self.storage.isCached(forKey: "2"))
  98. // But the object is still in underlying cache.
  99. let obj = self.storage.storage.object(forKey: "1")
  100. XCTAssertNotNil(obj)
  101. exp.fulfill()
  102. }
  103. waitForExpectations(timeout: 3, handler: nil)
  104. }
  105. func testStoreWithConfigExpiration() {
  106. let exp = expectation(description: #function)
  107. storage.config.expiration = .seconds(0.1)
  108. XCTAssertFalse(storage.isCached(forKey: "1"))
  109. storage.store(value: 1, forKey: "1")
  110. XCTAssertTrue(storage.isCached(forKey: "1"))
  111. delay(0.2) {
  112. XCTAssertFalse(self.storage.isCached(forKey: "1"))
  113. // But the object is still in underlying cache.
  114. let obj = self.storage.storage.object(forKey: "1")
  115. XCTAssertNotNil(obj)
  116. exp.fulfill()
  117. }
  118. waitForExpectations(timeout: 3, handler: nil)
  119. }
  120. func testStoreWithExpirationExtending() {
  121. let exp = expectation(description: #function)
  122. XCTAssertFalse(storage.isCached(forKey: "1"))
  123. storage.store(value: 1, forKey: "1", expiration: .seconds(1))
  124. XCTAssertTrue(storage.isCached(forKey: "1"))
  125. delay(0.1) {
  126. let expirationDate1 = self.storage.storage.object(forKey: "1")?.estimatedExpiration
  127. XCTAssertNotNil(expirationDate1)
  128. // Request for the object to extend it's expiration date
  129. let obj = self.storage.value(forKey: "1", extendingExpiration: .expirationTime(.seconds(5)))
  130. XCTAssertNotNil(obj)
  131. let expirationDate2 = self.storage.storage.object(forKey: "1")?.estimatedExpiration
  132. XCTAssertNotNil(expirationDate2)
  133. XCTAssertNotEqual(expirationDate1!, expirationDate2!)
  134. XCTAssert(expirationDate1!.isPast(referenceDate: expirationDate2!))
  135. exp.fulfill()
  136. }
  137. waitForExpectations(timeout: 3, handler: nil)
  138. }
  139. func testStoreWithExpirationNotExtending() {
  140. let exp = expectation(description: #function)
  141. XCTAssertFalse(storage.isCached(forKey: "1"))
  142. storage.store(value: 1, forKey: "1", expiration: .seconds(1))
  143. XCTAssertTrue(storage.isCached(forKey: "1"))
  144. delay(0.1) {
  145. let expirationDate1 = self.storage.storage.object(forKey: "1")?.estimatedExpiration
  146. XCTAssertNotNil(expirationDate1)
  147. // Request for the object to extend it's expiration date
  148. let obj = self.storage.value(forKey: "1", extendingExpiration: .none)
  149. XCTAssertNotNil(obj)
  150. let expirationDate2 = self.storage.storage.object(forKey: "1")?.estimatedExpiration
  151. XCTAssertNotNil(expirationDate2)
  152. XCTAssertEqual(expirationDate1, expirationDate2)
  153. exp.fulfill()
  154. }
  155. waitForExpectations(timeout: 3, handler: nil)
  156. }
  157. func testRemoveExpired() {
  158. let exp = expectation(description: #function)
  159. XCTAssertFalse(storage.isCached(forKey: "1"))
  160. storage.store(value: 1, forKey: "1", expiration: .seconds(0.1))
  161. XCTAssertTrue(storage.isCached(forKey: "1"))
  162. delay(0.2) {
  163. XCTAssertFalse(self.storage.isCached(forKey: "1"))
  164. // But the object is still in underlying cache.
  165. XCTAssertNotNil(self.storage.storage.object(forKey: "1"))
  166. self.storage.removeExpired()
  167. // It should be removed now.
  168. XCTAssertNil(self.storage.storage.object(forKey: "1"))
  169. exp.fulfill()
  170. }
  171. waitForExpectations(timeout: 3, handler: nil)
  172. }
  173. func testExtendExpirationByAccessing() {
  174. let exp = expectation(description: #function)
  175. let expiration = StorageExpiration.seconds(0.5)
  176. storage.store(value: 1, forKey: "1", expiration: expiration)
  177. delay(0.3) {
  178. // This should extend the expiration to (0.3 + 0.5) from initially created.
  179. let v = self.storage.value(forKey: "1")
  180. XCTAssertEqual(v, 1)
  181. }
  182. delay(0.6) {
  183. // Accessing `isCached` does not extend expiration
  184. XCTAssertTrue(self.storage.isCached(forKey: "1"))
  185. }
  186. delay(1) {
  187. XCTAssertFalse(self.storage.isCached(forKey: "1"))
  188. exp.fulfill()
  189. }
  190. waitForExpectations(timeout: 3, handler: nil)
  191. }
  192. func testAutoCleanExpiredMemory() {
  193. let exp = expectation(description: #function)
  194. let config = MemoryStorage.Config(totalCostLimit: 3, cleanInterval: 0.1)
  195. storage = MemoryStorage.Backend(config: config)
  196. storage.store(value: 1, forKey: "1", expiration: .seconds(0.1))
  197. XCTAssertTrue(storage.isCached(forKey: "1"))
  198. XCTAssertEqual(self.storage.keys.count, 1)
  199. delay(0.2) {
  200. XCTAssertFalse(self.storage.isCached(forKey: "1"))
  201. XCTAssertNil(self.storage.storage.object(forKey: "1"))
  202. XCTAssertEqual(self.storage.keys.count, 0)
  203. exp.fulfill()
  204. }
  205. waitForExpectations(timeout: 3, handler: nil)
  206. }
  207. func testStorageObject() {
  208. let now = Date()
  209. let obj = MemoryStorage.StorageObject(1, expiration: .seconds(1))
  210. XCTAssertEqual(obj.value, 1)
  211. XCTAssertEqual(
  212. obj.estimatedExpiration.timeIntervalSince1970,
  213. now.addingTimeInterval(1).timeIntervalSince1970,
  214. accuracy: 0.3)
  215. let exp = expectation(description: #function)
  216. delay(0.5) {
  217. obj.extendExpiration()
  218. XCTAssertEqual(
  219. obj.estimatedExpiration.timeIntervalSince1970,
  220. now.addingTimeInterval(1.5).timeIntervalSince1970,
  221. accuracy: 0.3)
  222. exp.fulfill()
  223. }
  224. waitForExpectations(timeout: 3, handler: nil)
  225. }
  226. }