]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/retry/test/integration/test-retry-operation.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / retry / test / integration / test-retry-operation.js
1 var common = require('../common');
2 var assert = common.assert;
3 var fake = common.fake.create();
4 var retry = require(common.dir.lib + '/retry');
5
6 (function testErrors() {
7   var operation = retry.operation();
8
9   var error = new Error('some error');
10   var error2 = new Error('some other error');
11   operation._errors.push(error);
12   operation._errors.push(error2);
13
14   assert.deepEqual(operation.errors(), [error, error2]);
15 })();
16
17 (function testMainErrorReturnsMostFrequentError() {
18   var operation = retry.operation();
19   var error = new Error('some error');
20   var error2 = new Error('some other error');
21
22   operation._errors.push(error);
23   operation._errors.push(error2);
24   operation._errors.push(error);
25
26   assert.strictEqual(operation.mainError(), error);
27 })();
28
29 (function testMainErrorReturnsLastErrorOnEqualCount() {
30   var operation = retry.operation();
31   var error = new Error('some error');
32   var error2 = new Error('some other error');
33
34   operation._errors.push(error);
35   operation._errors.push(error2);
36
37   assert.strictEqual(operation.mainError(), error2);
38 })();
39
40 (function testAttempt() {
41   var operation = retry.operation();
42   var fn = new Function();
43
44   var timeoutOpts = {
45     timeout: 1,
46     cb: function() {}
47   };
48   operation.attempt(fn, timeoutOpts);
49
50   assert.strictEqual(fn, operation._fn);
51   assert.strictEqual(timeoutOpts.timeout, operation._operationTimeout);
52   assert.strictEqual(timeoutOpts.cb, operation._operationTimeoutCb);
53 })();
54
55 (function testRetry() {
56   var times = 3;
57   var error = new Error('some error');
58   var operation = retry.operation([1, 2, 3]);
59   var attempts = 0;
60
61   var finalCallback = fake.callback('finalCallback');
62   fake.expectAnytime(finalCallback);
63
64   var fn = function() {
65     operation.attempt(function(currentAttempt) {
66       attempts++;
67       assert.equal(currentAttempt, attempts);
68       if (operation.retry(error)) {
69         return;
70       }
71
72       assert.strictEqual(attempts, 4);
73       assert.strictEqual(operation.attempts(), attempts);
74       assert.strictEqual(operation.mainError(), error);
75       finalCallback();
76     });
77   };
78
79   fn();
80 })();
81
82 (function testRetryForever() {
83   var error = new Error('some error');
84   var operation = retry.operation({ retries: 3, forever: true });
85   var attempts = 0;
86
87   var finalCallback = fake.callback('finalCallback');
88   fake.expectAnytime(finalCallback);
89
90   var fn = function() {
91     operation.attempt(function(currentAttempt) {
92       attempts++;
93       assert.equal(currentAttempt, attempts);
94       if (attempts !== 6 && operation.retry(error)) {
95         return;
96       }
97
98       assert.strictEqual(attempts, 6);
99       assert.strictEqual(operation.attempts(), attempts);
100       assert.strictEqual(operation.mainError(), error);
101       finalCallback();
102     });
103   };
104
105   fn();
106 })();
107
108 (function testRetryForeverNoRetries() {
109   var error = new Error('some error');
110   var delay = 50
111   var operation = retry.operation({
112     retries: null,
113     forever: true,
114     minTimeout: delay,
115     maxTimeout: delay
116   });
117
118   var attempts = 0;
119   var startTime = new Date().getTime();
120
121   var finalCallback = fake.callback('finalCallback');
122   fake.expectAnytime(finalCallback);
123
124   var fn = function() {
125     operation.attempt(function(currentAttempt) {
126       attempts++;
127       assert.equal(currentAttempt, attempts);
128       if (attempts !== 4 && operation.retry(error)) {
129         return;
130       }
131
132       var endTime = new Date().getTime();
133       var minTime = startTime + (delay * 3);
134       var maxTime = minTime + 20 // add a little headroom for code execution time
135       assert(endTime > minTime)
136       assert(endTime < maxTime)
137       assert.strictEqual(attempts, 4);
138       assert.strictEqual(operation.attempts(), attempts);
139       assert.strictEqual(operation.mainError(), error);
140       finalCallback();
141     });
142   };
143
144   fn();
145 })();
146
147 (function testStop() {
148   var error = new Error('some error');
149   var operation = retry.operation([1, 2, 3]);
150   var attempts = 0;
151
152   var finalCallback = fake.callback('finalCallback');
153   fake.expectAnytime(finalCallback);
154
155   var fn = function() {
156     operation.attempt(function(currentAttempt) {
157       attempts++;
158       assert.equal(currentAttempt, attempts);
159
160       if (attempts === 2) {
161         operation.stop();
162
163         assert.strictEqual(attempts, 2);
164         assert.strictEqual(operation.attempts(), attempts);
165         assert.strictEqual(operation.mainError(), error);
166         finalCallback();
167       }
168
169       if (operation.retry(error)) {
170         return;
171       }
172     });
173   };
174
175   fn();
176 })();