exponentialbackoff_test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.math.ExponentialBackoffTest');
  15. goog.setTestOnly('goog.math.ExponentialBackoffTest');
  16. goog.require('goog.math.ExponentialBackoff');
  17. goog.require('goog.testing.jsunit');
  18. var INITIAL_VALUE = 1;
  19. var MAX_VALUE = 10;
  20. function assertValueAndCounts(value, backoffCount, decayCount, backoff) {
  21. assertEquals('Wrong value', value, backoff.getValue());
  22. assertEquals('Wrong backoff count', backoffCount, backoff.getBackoffCount());
  23. assertEquals('Wrong decay count', decayCount, backoff.getDecayCount());
  24. }
  25. function assertValueAndBackoffCount(value, count, backoff) {
  26. assertEquals('Wrong value', value, backoff.getValue());
  27. assertEquals('Wrong backoff count', count, backoff.getBackoffCount());
  28. }
  29. function assertValueAndDecayCount(value, count, backoff) {
  30. assertEquals('Wrong value', value, backoff.getValue());
  31. assertEquals('Wrong decay count', count, backoff.getDecayCount());
  32. }
  33. function assertValueRangeAndBackoffCount(
  34. minBackoffValue, maxBackoffValue, count, backoff) {
  35. assertTrue('Value too small', backoff.getValue() >= minBackoffValue);
  36. assertTrue('Value too large', backoff.getValue() <= maxBackoffValue);
  37. assertEquals('Wrong backoff count', count, backoff.getBackoffCount());
  38. }
  39. function assertValueRangeAndDecayCount(
  40. minBackoffValue, maxBackoffValue, count, backoff) {
  41. assertTrue('Value too small', backoff.getValue() >= minBackoffValue);
  42. assertTrue('Value too large', backoff.getValue() <= maxBackoffValue);
  43. assertEquals('Wrong decay count', count, backoff.getDecayCount());
  44. }
  45. function getMinBackoff(baseValue, randomFactor) {
  46. return Math.round(baseValue - baseValue * randomFactor);
  47. }
  48. function getMaxBackoff(baseValue, randomFactor) {
  49. return Math.round(baseValue + baseValue * randomFactor);
  50. }
  51. function createBackoff() {
  52. return new goog.math.ExponentialBackoff(INITIAL_VALUE, MAX_VALUE);
  53. }
  54. function testInitialState() {
  55. var backoff = createBackoff();
  56. assertValueAndCounts(INITIAL_VALUE, 0, 0, backoff);
  57. }
  58. function testBackoff() {
  59. var backoff = createBackoff();
  60. backoff.backoff();
  61. assertValueAndBackoffCount(2 /* value */, 1 /* count */, backoff);
  62. backoff.backoff();
  63. assertValueAndBackoffCount(4 /* value */, 2 /* count */, backoff);
  64. backoff.backoff();
  65. assertValueAndBackoffCount(8 /* value */, 3 /* count */, backoff);
  66. backoff.backoff();
  67. assertValueAndBackoffCount(MAX_VALUE, 4 /* count */, backoff);
  68. backoff.backoff();
  69. assertValueAndBackoffCount(MAX_VALUE, 5 /* count */, backoff);
  70. }
  71. function testReset() {
  72. var backoff = createBackoff();
  73. backoff.backoff();
  74. backoff.decay();
  75. backoff.reset();
  76. assertValueAndCounts(
  77. INITIAL_VALUE, 0 /* backoff count */, 0 /* decay count */, backoff);
  78. backoff.backoff();
  79. assertValueAndCounts(
  80. 2 /* value */, 1 /* backoff count */, 0 /* decay count */, backoff);
  81. backoff.decay();
  82. assertValueAndCounts(
  83. INITIAL_VALUE, 1 /* backoff count */, 1 /* decay count */, backoff);
  84. }
  85. function testRandomFactorBackoff() {
  86. var initialValue = 1;
  87. var maxValue = 20;
  88. var randomFactor = 0.5;
  89. var backoff =
  90. new goog.math.ExponentialBackoff(initialValue, maxValue, randomFactor);
  91. assertValueAndBackoffCount(initialValue /* value */, 0 /* count */, backoff);
  92. backoff.backoff();
  93. assertValueRangeAndBackoffCount(
  94. getMinBackoff(2, randomFactor), getMaxBackoff(2, randomFactor),
  95. 1 /* count */, backoff);
  96. backoff.backoff();
  97. assertValueRangeAndBackoffCount(
  98. getMinBackoff(4, randomFactor), getMaxBackoff(4, randomFactor),
  99. 2 /* count */, backoff);
  100. backoff.backoff();
  101. assertValueRangeAndBackoffCount(
  102. getMinBackoff(8, randomFactor), getMaxBackoff(8, randomFactor),
  103. 3 /* count */, backoff);
  104. backoff.backoff();
  105. assertValueRangeAndBackoffCount(
  106. getMinBackoff(16, randomFactor), maxValue /* max backoff value */,
  107. 4 /* count */, backoff);
  108. backoff.backoff();
  109. assertValueRangeAndBackoffCount(
  110. getMinBackoff(maxValue, randomFactor), maxValue /* max backoff value */,
  111. 5 /* count */, backoff);
  112. }
  113. function testRandomFactorDecay() {
  114. var initialValue = 1;
  115. var maxValue = 8;
  116. var randomFactor = 0.5;
  117. var backoff =
  118. new goog.math.ExponentialBackoff(initialValue, maxValue, randomFactor);
  119. backoff.backoff();
  120. backoff.backoff();
  121. backoff.backoff();
  122. backoff.backoff();
  123. backoff.backoff();
  124. assertValueRangeAndBackoffCount(
  125. getMinBackoff(maxValue, randomFactor), maxValue /* max backoff value */,
  126. 5 /* count */, backoff);
  127. backoff.decay();
  128. assertValueRangeAndDecayCount(
  129. getMinBackoff(4, randomFactor), getMaxBackoff(4, randomFactor),
  130. 1 /* count */, backoff);
  131. backoff.decay();
  132. assertValueRangeAndDecayCount(
  133. getMinBackoff(2, randomFactor), getMaxBackoff(2, randomFactor),
  134. 2 /* count */, backoff);
  135. backoff.decay();
  136. assertValueRangeAndDecayCount(
  137. initialValue, getMaxBackoff(initialValue, randomFactor), 3 /* count */,
  138. backoff);
  139. }
  140. function testBackoffFactor() {
  141. var initialValue = 1;
  142. var maxValue = 30;
  143. var randomFactor = undefined;
  144. var backoffFactor = 3;
  145. var backoff = new goog.math.ExponentialBackoff(
  146. initialValue, maxValue, randomFactor, backoffFactor);
  147. backoff.backoff();
  148. assertValueAndBackoffCount(3 /* value */, 1 /* count */, backoff);
  149. backoff.backoff();
  150. assertValueAndBackoffCount(9 /* value */, 2 /* count */, backoff);
  151. backoff.backoff();
  152. assertValueAndBackoffCount(27 /* value */, 3 /* count */, backoff);
  153. backoff.backoff();
  154. assertValueAndBackoffCount(maxValue, 4 /* count */, backoff);
  155. backoff.backoff();
  156. assertValueAndBackoffCount(maxValue, 5 /* count */, backoff);
  157. }
  158. function testDecayFactor() {
  159. var initialValue = 1;
  160. var maxValue = 27;
  161. var randomFactor = undefined;
  162. var backoffFactor = undefined;
  163. var decayFactor = 3;
  164. var backoff = new goog.math.ExponentialBackoff(
  165. initialValue, maxValue, randomFactor, backoffFactor, decayFactor);
  166. backoff.backoff();
  167. backoff.backoff();
  168. backoff.backoff();
  169. backoff.backoff();
  170. backoff.backoff();
  171. assertValueAndCounts(
  172. maxValue, 5 /* backoff count */, 0 /* decay count */, backoff);
  173. backoff.decay();
  174. assertValueAndDecayCount(9, 1 /* count */, backoff);
  175. backoff.decay();
  176. assertValueAndDecayCount(3, 2 /* count */, backoff);
  177. backoff.decay();
  178. assertValueAndDecayCount(initialValue, 3 /* count */, backoff);
  179. backoff.decay();
  180. assertValueAndDecayCount(initialValue, 4 /* count */, backoff);
  181. }