collectablestoragetester.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2011 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. /**
  15. * @fileoverview Unit tests for the collectable storage interface.
  16. *
  17. */
  18. goog.provide('goog.storage.collectableStorageTester');
  19. goog.require('goog.testing.asserts');
  20. goog.setTestOnly('collectablestorage_test');
  21. /**
  22. * Tests basic operation: expiration and collection of collectable storage.
  23. *
  24. * @param {goog.storage.mechanism.IterableMechanism} mechanism
  25. * @param {goog.testing.MockClock} clock
  26. * @param {goog.storage.CollectableStorage} storage
  27. */
  28. goog.storage.collectableStorageTester.runBasicTests = function(
  29. mechanism, clock, storage) {
  30. // No expiration.
  31. storage.set('first', 'three seconds', 3000);
  32. storage.set('second', 'one second', 1000);
  33. storage.set('third', 'permanent');
  34. storage.set('fourth', 'two seconds', 2000);
  35. clock.tick(100);
  36. storage.collect();
  37. assertEquals('three seconds', storage.get('first'));
  38. assertEquals('one second', storage.get('second'));
  39. assertEquals('permanent', storage.get('third'));
  40. assertEquals('two seconds', storage.get('fourth'));
  41. // A key has expired.
  42. clock.tick(1000);
  43. storage.collect();
  44. assertNull(mechanism.get('second'));
  45. assertEquals('three seconds', storage.get('first'));
  46. assertUndefined(storage.get('second'));
  47. assertEquals('permanent', storage.get('third'));
  48. assertEquals('two seconds', storage.get('fourth'));
  49. // Another two keys have expired.
  50. clock.tick(2000);
  51. storage.collect();
  52. assertNull(mechanism.get('first'));
  53. assertNull(mechanism.get('fourth'));
  54. assertUndefined(storage.get('first'));
  55. assertEquals('permanent', storage.get('third'));
  56. assertUndefined(storage.get('fourth'));
  57. // Clean up.
  58. storage.remove('third');
  59. assertNull(mechanism.get('third'));
  60. assertUndefined(storage.get('third'));
  61. storage.collect();
  62. clock.uninstall();
  63. };