boundedcollectablestorage_test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2013 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.labs.storage.BoundedCollectableStorageTest');
  15. goog.setTestOnly('goog.labs.storage.BoundedCollectableStorageTest');
  16. goog.require('goog.labs.storage.BoundedCollectableStorage');
  17. goog.require('goog.storage.collectableStorageTester');
  18. goog.require('goog.storage.storageTester');
  19. goog.require('goog.testing.MockClock');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.testing.storage.FakeMechanism');
  22. function testBasicOperations() {
  23. var mechanism = new goog.testing.storage.FakeMechanism();
  24. var storage = new goog.labs.storage.BoundedCollectableStorage(mechanism, 5);
  25. goog.storage.storageTester.runBasicTests(storage);
  26. }
  27. function testExpiredKeyCollection() {
  28. var mechanism = new goog.testing.storage.FakeMechanism();
  29. var clock = new goog.testing.MockClock(true);
  30. var storage = new goog.labs.storage.BoundedCollectableStorage(mechanism, 15);
  31. goog.storage.collectableStorageTester.runBasicTests(
  32. mechanism, clock, storage);
  33. }
  34. function testLimitingNumberOfItems() {
  35. var mechanism = new goog.testing.storage.FakeMechanism();
  36. var clock = new goog.testing.MockClock(true);
  37. var storage = new goog.labs.storage.BoundedCollectableStorage(mechanism, 2);
  38. // First item should fit.
  39. storage.set('item-1', 'one', 10000);
  40. clock.tick(100);
  41. assertEquals('one', storage.get('item-1'));
  42. // Second item should fit.
  43. storage.set('item-2', 'two', 10000);
  44. assertEquals('one', storage.get('item-1'));
  45. assertEquals('two', storage.get('item-2'));
  46. // Third item is too much, 'item-1' should be removed.
  47. storage.set('item-3', 'three', 5000);
  48. clock.tick(100);
  49. assertUndefined(storage.get('item-1'));
  50. assertEquals('two', storage.get('item-2'));
  51. assertEquals('three', storage.get('item-3'));
  52. clock.tick(5000);
  53. // 'item-3' item has expired, should be removed instead an older 'item-2'.
  54. storage.set('item-4', 'four', 10000);
  55. assertUndefined(storage.get('item-1'));
  56. assertUndefined(storage.get('item-3'));
  57. assertEquals('two', storage.get('item-2'));
  58. assertEquals('four', storage.get('item-4'));
  59. storage.remove('item-2');
  60. storage.remove('item-4');
  61. clock.uninstall();
  62. }