expiringstorage_test.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. goog.provide('goog.storage.ExpiringStorageTest');
  15. goog.setTestOnly('goog.storage.ExpiringStorageTest');
  16. goog.require('goog.storage.ExpiringStorage');
  17. goog.require('goog.storage.storageTester');
  18. goog.require('goog.testing.MockClock');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.testing.storage.FakeMechanism');
  21. function testBasicOperations() {
  22. var mechanism = new goog.testing.storage.FakeMechanism();
  23. var storage = new goog.storage.ExpiringStorage(mechanism);
  24. goog.storage.storageTester.runBasicTests(storage);
  25. }
  26. function testExpiration() {
  27. var mechanism = new goog.testing.storage.FakeMechanism();
  28. var clock = new goog.testing.MockClock(true);
  29. var storage = new goog.storage.ExpiringStorage(mechanism);
  30. // No expiration.
  31. storage.set('first', 'one second', 1000);
  32. storage.set('second', 'permanent');
  33. storage.set('third', 'two seconds', 2000);
  34. storage.set('fourth', 'permanent');
  35. clock.tick(100);
  36. assertEquals('one second', storage.get('first'));
  37. assertEquals('permanent', storage.get('second'));
  38. assertEquals('two seconds', storage.get('third'));
  39. assertEquals('permanent', storage.get('fourth'));
  40. // A key has expired.
  41. clock.tick(1000);
  42. assertUndefined(storage.get('first'));
  43. assertEquals('permanent', storage.get('second'));
  44. assertEquals('two seconds', storage.get('third'));
  45. assertEquals('permanent', storage.get('fourth'));
  46. assertNull(mechanism.get('first'));
  47. // Add an already expired key.
  48. storage.set('fourth', 'one second again', 1000);
  49. assertNull(mechanism.get('fourth'));
  50. assertUndefined(storage.get('fourth'));
  51. // Another key has expired.
  52. clock.tick(1000);
  53. assertEquals('permanent', storage.get('second'));
  54. assertUndefined(storage.get('third'));
  55. assertNull(mechanism.get('third'));
  56. // Clean up.
  57. storage.remove('second');
  58. assertNull(mechanism.get('second'));
  59. assertUndefined(storage.get('second'));
  60. clock.uninstall();
  61. }
  62. function testClockSkew() {
  63. var mechanism = new goog.testing.storage.FakeMechanism();
  64. var storage = new goog.storage.ExpiringStorage(mechanism);
  65. var clock = new goog.testing.MockClock(true);
  66. // Simulate clock skew.
  67. clock.tick(100);
  68. storage.set('first', 'one second', 1000);
  69. clock.reset();
  70. assertUndefined(storage.get('first'));
  71. assertNull(mechanism.get('first'));
  72. clock.uninstall();
  73. }