lru-memoizer.itemmaxage.test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. var memoizer = require('./..');
  2. var assert = require('chai').assert;
  3. describe('lru-memoizer (itemMaxAge)', function () {
  4. var loadTimes = 0, memoized;
  5. beforeEach(function () {
  6. loadTimes = 0;
  7. });
  8. it('should use default behavior if not configured', function (done) {
  9. memoized = memoizer({
  10. load: function (a, b, callback) {
  11. loadTimes++;
  12. setTimeout(function () {
  13. callback(null, a + b);
  14. }, 100);
  15. },
  16. hash: function (a, b) {
  17. return a + '-' + b;
  18. },
  19. max: 10,
  20. maxAge: 500
  21. });
  22. memoized(1,2, function (err, result) {
  23. assert.isNull(err);
  24. assert.strictEqual(result, 3);
  25. assert.strictEqual(loadTimes, 1);
  26. // Not expired yet.
  27. setTimeout(function() {
  28. memoized(1,2, function (err, result) {
  29. assert.isNull(err);
  30. assert.strictEqual(result, 3);
  31. assert.strictEqual(loadTimes, 1);
  32. // Expired, load times will increase.
  33. setTimeout(function() {
  34. memoized(1,2, function (err, result) {
  35. assert.isNull(err);
  36. assert.strictEqual(result, 3);
  37. assert.strictEqual(loadTimes, 2);
  38. done();
  39. });
  40. }, 200);
  41. });
  42. }, 400);
  43. });
  44. });
  45. it('should return all args and the result in the itemMaxAge function', function (done) {
  46. var args;
  47. memoized = memoizer({
  48. load: function (a, b, callback) {
  49. loadTimes++;
  50. setTimeout(function () {
  51. callback(null, a + b);
  52. }, 100);
  53. },
  54. itemMaxAge: function (a, b, result) {
  55. args = arguments;
  56. return 1000;
  57. },
  58. hash: function (a, b) {
  59. return a + '-' + b;
  60. },
  61. max: 10,
  62. maxAge: 600
  63. });
  64. memoized(1,2, function (err, result) {
  65. assert.isNull(err);
  66. assert.strictEqual(args[0], 1);
  67. assert.strictEqual(args[1], 2);
  68. assert.strictEqual(args[2], 3);
  69. done();
  70. });
  71. });
  72. it('should overwrite the default behavior if configured', function (done) {
  73. var maxAge = 0;
  74. var lastKey = null;
  75. memoized = memoizer({
  76. load: function (a, b, callback) {
  77. loadTimes++;
  78. setTimeout(function () {
  79. callback(null, a + b);
  80. }, 100);
  81. },
  82. itemMaxAge: function (a, b, result) {
  83. lastKey = a + '-' + b;
  84. // In this test, we set the maxAge of the current item to (result*100).
  85. // If the result is 3, the max age of this item will be 300.
  86. maxAge = result * 100;
  87. return maxAge;
  88. },
  89. hash: function (a, b) {
  90. return a + '-' + b;
  91. },
  92. max: 10,
  93. maxAge: 600
  94. });
  95. memoized(1,2, function (err, result) {
  96. assert.isNull(err);
  97. assert.strictEqual(maxAge, 300);
  98. assert.strictEqual(lastKey, '1-2');
  99. assert.strictEqual(result, 3);
  100. assert.strictEqual(loadTimes, 1);
  101. // Not expired yet after 200 ms, because the expiration is 300
  102. setTimeout(function() {
  103. memoized(1,2, function (err, result) {
  104. assert.isNull(err);
  105. assert.strictEqual(maxAge, 300);
  106. assert.strictEqual(lastKey, '1-2');
  107. assert.strictEqual(result, 3);
  108. assert.strictEqual(loadTimes, 1);
  109. // Expired because now we are at 350 ms (even though gloabl expiration has been set to 600)
  110. setTimeout(function() {
  111. memoized(1,2, function (err, result) {
  112. assert.isNull(err);
  113. assert.strictEqual(maxAge, 300);
  114. assert.strictEqual(lastKey, '1-2');
  115. assert.strictEqual(result, 3);
  116. assert.strictEqual(loadTimes, 2);
  117. // Expired again, because 350ms have passed again.
  118. setTimeout(function() {
  119. memoized(1,2, function (err, result) {
  120. assert.isNull(err);
  121. assert.strictEqual(maxAge, 300);
  122. assert.strictEqual(lastKey, '1-2');
  123. assert.strictEqual(result, 3);
  124. assert.strictEqual(loadTimes, 3);
  125. done();
  126. });
  127. }, 350);
  128. });
  129. }, 150);
  130. });
  131. }, 200);
  132. });
  133. });
  134. it('should overwrite the default behavior if configured (sync)', function (done) {
  135. var maxAge = 0;
  136. var lastKey = null;
  137. memoized = memoizer.sync({
  138. load: function (a, b) {
  139. loadTimes++;
  140. return a + b;
  141. },
  142. itemMaxAge: function (a, b, result) {
  143. lastKey = a + '-' + b;
  144. // In this test, we set the maxAge of the current item to (result*100).
  145. // If the result is 3, the max age of this item will be 300.
  146. maxAge = result * 100;
  147. return maxAge;
  148. },
  149. hash: function (a, b) {
  150. return a + '-' + b;
  151. },
  152. max: 10,
  153. maxAge: 600
  154. });
  155. var result = memoized(1, 2);
  156. assert.strictEqual(maxAge, 300);
  157. assert.strictEqual(lastKey, '1-2');
  158. assert.strictEqual(result, 3);
  159. assert.strictEqual(loadTimes, 1);
  160. // Not expired yet after 200 ms, because the expiration is 300
  161. setTimeout(function() {
  162. result = memoized(1, 2);
  163. assert.strictEqual(maxAge, 300);
  164. assert.strictEqual(lastKey, '1-2');
  165. assert.strictEqual(result, 3);
  166. assert.strictEqual(loadTimes, 1);
  167. // Expired because now we are at 350 ms (even though gloabl expiration has been set to 600)
  168. setTimeout(function() {
  169. result = memoized(1,2);
  170. assert.strictEqual(maxAge, 300);
  171. assert.strictEqual(lastKey, '1-2');
  172. assert.strictEqual(result, 3);
  173. assert.strictEqual(loadTimes, 2);
  174. // Expired again, because 350ms have passed again.
  175. setTimeout(function() {
  176. result = memoized(1,2);
  177. assert.strictEqual(maxAge, 300);
  178. assert.strictEqual(lastKey, '1-2');
  179. assert.strictEqual(result, 3);
  180. assert.strictEqual(loadTimes, 3);
  181. done();
  182. }, 350);
  183. }, 150);
  184. }, 200);
  185. });
  186. });