timer_test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2006 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.TimerTest');
  15. goog.setTestOnly('goog.TimerTest');
  16. goog.require('goog.Promise');
  17. goog.require('goog.Timer');
  18. goog.require('goog.events');
  19. goog.require('goog.testing.MockClock');
  20. goog.require('goog.testing.jsunit');
  21. var intervalIds = {};
  22. var intervalIdCounter = 0;
  23. var mockClock;
  24. var maxDuration = 60 * 1000; // 60s
  25. function setUp() {
  26. mockClock = new goog.testing.MockClock(true /* install */);
  27. }
  28. function tearDown() {
  29. mockClock.dispose();
  30. }
  31. // Run a test for 60s and see how many counts we get
  32. function runTest(string, ticks, number) {
  33. var t = new goog.Timer(ticks);
  34. var count = 0;
  35. goog.events.listen(t, 'tick', function(evt) { count++; });
  36. t.start();
  37. mockClock.tick(maxDuration);
  38. assertEquals(string, number, count);
  39. t.stop();
  40. goog.events.removeAll(t);
  41. }
  42. function test100msTicks() {
  43. // Desc, interval in ms, expected ticks in 60s
  44. runTest('10 ticks per second for 60 seconds', 100, 600);
  45. }
  46. function test500msTicks() {
  47. runTest('2 ticks per second for 60 seconds', 500, 120);
  48. }
  49. function test1sTicks() {
  50. runTest('1 tick per second for 60 seconds', 1000, 60);
  51. }
  52. function test2sTicks() {
  53. runTest('1 tick every 2 seconds for 60 seconds', 2000, 30);
  54. }
  55. function test5sTicks() {
  56. runTest('1 tick every 5 seconds for 60 seconds', 5000, 12);
  57. }
  58. function test10sTicks() {
  59. runTest('1 tick every 10 seconds for 60 seconds', 10000, 6);
  60. }
  61. function test30sTicks() {
  62. runTest('1 tick every 30 seconds for 60 seconds', 30000, 2);
  63. }
  64. function test60sTicks() {
  65. runTest('1 tick every 60 seconds', 60000, 1);
  66. }
  67. function testCallOnce() {
  68. var c = 0;
  69. var expectedTimeoutId = goog.testing.MockClock.nextId;
  70. var actualTimeoutId = goog.Timer.callOnce(function() {
  71. if (c > 0) {
  72. assertTrue('callOnce should only be called once', false);
  73. }
  74. c++;
  75. });
  76. assertEquals(
  77. 'callOnce should return the timeout ID', expectedTimeoutId,
  78. actualTimeoutId);
  79. var obj = {c: 0};
  80. goog.Timer.callOnce(function() {
  81. if (this.c > 0) {
  82. assertTrue('callOnce should only be called once', false);
  83. }
  84. assertEquals(obj, this);
  85. this.c++;
  86. }, 1, obj);
  87. mockClock.tick(maxDuration);
  88. }
  89. function testCallOnceIgnoresTimeoutsTooLarge() {
  90. var failCallback = goog.partial(fail, 'Timeout should never be called');
  91. assertEquals(
  92. 'Timeouts slightly too large should yield a timer ID of -1', -1,
  93. goog.Timer.callOnce(failCallback, 2147483648));
  94. assertEquals(
  95. 'Infinite timeouts should yield a timer ID of -1', -1,
  96. goog.Timer.callOnce(failCallback, Infinity));
  97. }
  98. function testPromise() {
  99. var c = 0;
  100. goog.Timer.promise(1, 'A').then(function(result) {
  101. c++;
  102. assertEquals('promise should return resolved value', 'A', result);
  103. });
  104. mockClock.tick(10);
  105. assertEquals('promise must be yielded once and only once', 1, c);
  106. }
  107. function testPromise_cancel() {
  108. var c = 0;
  109. goog.Timer.promise(1, 'A')
  110. .then(
  111. function(result) { fail('promise must not be resolved'); },
  112. function(reason) {
  113. c++;
  114. assertTrue(
  115. 'promise must fail due to cancel signal',
  116. reason instanceof goog.Promise.CancellationError);
  117. })
  118. .cancel();
  119. mockClock.tick(10);
  120. assertEquals('promise must be canceled once and only once', 1, c);
  121. }
  122. function testPromise_timeoutTooLarge() {
  123. var c = 0;
  124. goog.Timer.promise(2147483648, 'A')
  125. .then(
  126. function(result) { fail('promise must not be resolved'); },
  127. function(reason) {
  128. c++;
  129. assertTrue('promise must be rejected', reason instanceof Error);
  130. });
  131. mockClock.tick(10);
  132. assertEquals('promise must be rejected once and only once', 1, c);
  133. }