Debian Codegraph @

closure-library/closure/goog/math/exponentialbackoff_test.js

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