Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

serialize.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. var test = require('tap').test
  2. var LRU = require('../')
  3. test('dump', function (t) {
  4. var cache = new LRU()
  5. t.equal(cache.dump().length, 0, "nothing in dump for empty cache")
  6. cache.set("a", "A")
  7. cache.set("b", "B")
  8. t.deepEqual(cache.dump(), [
  9. { k: "b", v: "B", e: 0 },
  10. { k: "a", v: "A", e: 0 }
  11. ])
  12. cache.set("a", "A");
  13. t.deepEqual(cache.dump(), [
  14. { k: "a", v: "A", e: 0 },
  15. { k: "b", v: "B", e: 0 }
  16. ])
  17. cache.get("b");
  18. t.deepEqual(cache.dump(), [
  19. { k: "b", v: "B", e: 0 },
  20. { k: "a", v: "A", e: 0 }
  21. ])
  22. cache.del("a");
  23. t.deepEqual(cache.dump(), [
  24. { k: "b", v: "B", e: 0 }
  25. ])
  26. t.end()
  27. })
  28. test("do not dump stale items", function(t) {
  29. var cache = new LRU({
  30. max: 5,
  31. maxAge: 50
  32. })
  33. //expires at 50
  34. cache.set("a", "A")
  35. setTimeout(function () {
  36. //expires at 75
  37. cache.set("b", "B")
  38. var s = cache.dump()
  39. t.equal(s.length, 2)
  40. t.equal(s[0].k, "b")
  41. t.equal(s[1].k, "a")
  42. }, 25)
  43. setTimeout(function () {
  44. //expires at 110
  45. cache.set("c", "C")
  46. var s = cache.dump()
  47. t.equal(s.length, 2)
  48. t.equal(s[0].k, "c")
  49. t.equal(s[1].k, "b")
  50. }, 60)
  51. setTimeout(function () {
  52. //expires at 130
  53. cache.set("d", "D", 40)
  54. var s = cache.dump()
  55. t.equal(s.length, 2)
  56. t.equal(s[0].k, "d")
  57. t.equal(s[1].k, "c")
  58. }, 90)
  59. setTimeout(function () {
  60. var s = cache.dump()
  61. t.equal(s.length, 1)
  62. t.equal(s[0].k, "d")
  63. }, 120)
  64. setTimeout(function () {
  65. var s = cache.dump()
  66. t.deepEqual(s, [])
  67. t.end()
  68. }, 155)
  69. })
  70. test("load basic cache", function(t) {
  71. var cache = new LRU(),
  72. copy = new LRU()
  73. cache.set("a", "A")
  74. cache.set("b", "B")
  75. copy.load(cache.dump())
  76. t.deepEquals(cache.dump(), copy.dump())
  77. t.end()
  78. })
  79. test("load staled cache", function(t) {
  80. var cache = new LRU({maxAge: 50}),
  81. copy = new LRU({maxAge: 50}),
  82. arr
  83. //expires at 50
  84. cache.set("a", "A")
  85. setTimeout(function () {
  86. //expires at 80
  87. cache.set("b", "B")
  88. arr = cache.dump()
  89. t.equal(arr.length, 2)
  90. }, 30)
  91. setTimeout(function () {
  92. copy.load(arr)
  93. t.equal(copy.get("a"), undefined)
  94. t.equal(copy.get("b"), "B")
  95. }, 60)
  96. setTimeout(function () {
  97. t.equal(copy.get("b"), undefined)
  98. t.end()
  99. }, 90)
  100. })
  101. test("load to other size cache", function(t) {
  102. var cache = new LRU({max: 2}),
  103. copy = new LRU({max: 1})
  104. cache.set("a", "A")
  105. cache.set("b", "B")
  106. copy.load(cache.dump())
  107. t.equal(copy.get("a"), undefined)
  108. t.equal(copy.get("b"), "B")
  109. //update the last read from original cache
  110. cache.get("a")
  111. copy.load(cache.dump())
  112. t.equal(copy.get("a"), "A")
  113. t.equal(copy.get("b"), undefined)
  114. t.end()
  115. })
  116. test("load to other age cache", function(t) {
  117. var cache = new LRU({maxAge: 50}),
  118. aged = new LRU({maxAge: 100}),
  119. simple = new LRU(),
  120. arr,
  121. expired
  122. //created at 0
  123. //a would be valid till 0 + 50
  124. cache.set("a", "A")
  125. setTimeout(function () {
  126. //created at 20
  127. //b would be valid till 20 + 50
  128. cache.set("b", "B")
  129. //b would be valid till 20 + 70
  130. cache.set("c", "C", 70)
  131. arr = cache.dump()
  132. t.equal(arr.length, 3)
  133. }, 20)
  134. setTimeout(function () {
  135. t.equal(cache.get("a"), undefined)
  136. t.equal(cache.get("b"), "B")
  137. t.equal(cache.get("c"), "C")
  138. aged.load(arr)
  139. t.equal(aged.get("a"), undefined)
  140. t.equal(aged.get("b"), "B")
  141. t.equal(aged.get("c"), "C")
  142. simple.load(arr)
  143. t.equal(simple.get("a"), undefined)
  144. t.equal(simple.get("b"), "B")
  145. t.equal(simple.get("c"), "C")
  146. }, 60)
  147. setTimeout(function () {
  148. t.equal(cache.get("a"), undefined)
  149. t.equal(cache.get("b"), undefined)
  150. t.equal(cache.get("c"), "C")
  151. aged.load(arr)
  152. t.equal(aged.get("a"), undefined)
  153. t.equal(aged.get("b"), undefined)
  154. t.equal(aged.get("c"), "C")
  155. simple.load(arr)
  156. t.equal(simple.get("a"), undefined)
  157. t.equal(simple.get("b"), undefined)
  158. t.equal(simple.get("c"), "C")
  159. }, 80)
  160. setTimeout(function () {
  161. t.equal(cache.get("a"), undefined)
  162. t.equal(cache.get("b"), undefined)
  163. t.equal(cache.get("c"), undefined)
  164. aged.load(arr)
  165. t.equal(aged.get("a"), undefined)
  166. t.equal(aged.get("b"), undefined)
  167. t.equal(aged.get("c"), undefined)
  168. simple.load(arr)
  169. t.equal(simple.get("a"), undefined)
  170. t.equal(simple.get("b"), undefined)
  171. t.equal(simple.get("c"), undefined)
  172. t.end()
  173. }, 100)
  174. })