wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Copyright (c) 2012 Mathieu Turcotte
 * Licensed under the MIT license.
 */
 
var assert = require('assert');
var events = require('events');
var sinon = require('sinon');
var util = require('util');
 
var FunctionCall = require('../lib/function_call');
 
function MockBackoff() {
    events.EventEmitter.call(this);
 
    this.reset = sinon.spy();
    this.backoff = sinon.spy();
    this.failAfter = sinon.spy();
}
util.inherits(MockBackoff, events.EventEmitter);
 
exports["FunctionCall"] = {
    setUp: function(callback) {
        this.wrappedFn = sinon.stub();
        this.callback = sinon.stub();
        this.backoff = new MockBackoff();
        this.backoffFactory = sinon.stub();
        this.backoffFactory.returns(this.backoff);
        callback();
    },
 
    tearDown: function(callback) {
        callback();
    },
 
    "constructor's first argument should be a function": function(test) {
        test.throws(function() {
            new FunctionCall(1, [], function() {});
        }, /Expected fn to be a function./);
        test.done();
    },
 
    "constructor's last argument should be a function": function(test) {
        test.throws(function() {
            new FunctionCall(function() {}, [], 3);
        }, /Expected callback to be a function./);
        test.done();
    },
 
    "isPending should return false once the call is started": function(test) {
        this.wrappedFn.
            onFirstCall().yields(new Error()).
            onSecondCall().yields(null, 'Success!');
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
 
        test.ok(call.isPending());
 
        call.start(this.backoffFactory);
        test.ok(!call.isPending());
 
        this.backoff.emit('ready');
        test.ok(!call.isPending());
 
        test.done();
    },
 
    "isRunning should return true when call is in progress": function(test) {
        this.wrappedFn.
            onFirstCall().yields(new Error()).
            onSecondCall().yields(null, 'Success!');
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
 
        test.ok(!call.isRunning());
 
        call.start(this.backoffFactory);
        test.ok(call.isRunning());
 
        this.backoff.emit('ready');
        test.ok(!call.isRunning());
 
        test.done();
    },
 
    "isCompleted should return true once the call completes": function(test) {
        this.wrappedFn.
            onFirstCall().yields(new Error()).
            onSecondCall().yields(null, 'Success!');
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
 
        test.ok(!call.isCompleted());
 
        call.start(this.backoffFactory);
        test.ok(!call.isCompleted());
 
        this.backoff.emit('ready');
        test.ok(call.isCompleted());
 
        test.done();
    },
 
    "isAborted should return true once the call is aborted": function(test) {
        this.wrappedFn.
            onFirstCall().yields(new Error()).
            onSecondCall().yields(null, 'Success!');
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
 
        test.ok(!call.isAborted());
        call.abort();
        test.ok(call.isAborted());
 
        test.done();
    },
 
    "setStrategy should overwrite the default strategy": function(test) {
        var replacementStrategy = {};
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        call.setStrategy(replacementStrategy);
        call.start(this.backoffFactory);
        test.ok(this.backoffFactory.calledWith(replacementStrategy),
            'User defined strategy should be used to instantiate ' +
            'the backoff instance.');
        test.done();
    },
 
    "setStrategy should throw if the call is in progress": function(test) {
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        call.start(this.backoffFactory);
        test.throws(function() {
            call.setStrategy({});
        }, /in progress/);
        test.done();
    },
 
    "failAfter should not be set by default": function(test) {
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
            call.start(this.backoffFactory);
            test.equal(0, this.backoff.failAfter.callCount);
        test.done();
    },
 
    "failAfter should be used as the maximum number of backoffs": function(test) {
        var failAfterValue = 99;
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        call.failAfter(failAfterValue);
        call.start(this.backoffFactory);
        test.ok(this.backoff.failAfter.calledWith(failAfterValue),
            'User defined maximum number of backoffs shoud be ' +
            'used to configure the backoff instance.');
        test.done();
    },
 
    "failAfter should throw if the call is in progress": function(test) {
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        call.start(this.backoffFactory);
        test.throws(function() {
            call.failAfter(1234);
        }, /in progress/);
        test.done();
    },
 
    "start shouldn't allow overlapping invocation": function(test) {
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        var backoffFactory = this.backoffFactory;
 
        call.start(backoffFactory);
        test.throws(function() {
            call.start(backoffFactory);
        }, /already started/);
        test.done();
    },
 
    "start shouldn't allow invocation of aborted call": function(test) {
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        var backoffFactory = this.backoffFactory;
 
        call.abort();
        test.throws(function() {
            call.start(backoffFactory);
        }, /aborted/);
        test.done();
    },
 
    "call should forward its arguments to the wrapped function": function(test) {
        var call = new FunctionCall(this.wrappedFn, [1, 2, 3], this.callback);
        call.start(this.backoffFactory);
        test.ok(this.wrappedFn.calledWith(1, 2, 3));
        test.done();
    },
 
    "call should complete when the wrapped function succeeds": function(test) {
        var call = new FunctionCall(this.wrappedFn, [1, 2, 3], this.callback);
        this.wrappedFn.
            onCall(0).yields(new Error()).
            onCall(1).yields(new Error()).
            onCall(2).yields(new Error()).
            onCall(3).yields(null, 'Success!');
 
        call.start(this.backoffFactory);
 
        for (var i = 0; i < 2; i++) {
            this.backoff.emit('ready');
        }
 
        test.equals(this.callback.callCount, 0);
        this.backoff.emit('ready');
 
        test.ok(this.callback.calledWith(null, 'Success!'));
        test.ok(this.wrappedFn.alwaysCalledWith(1, 2, 3));
        test.done();
    },
 
    "call should fail when the backoff limit is reached": function(test) {
        var call = new FunctionCall(this.wrappedFn, [1, 2, 3], this.callback);
        var error = new Error();
        this.wrappedFn.yields(error);
        call.start(this.backoffFactory);
 
        for (var i = 0; i < 3; i++) {
            this.backoff.emit('ready');
        }
 
        test.equals(this.callback.callCount, 0);
 
        this.backoff.emit('fail');
 
        test.ok(this.callback.calledWith(error));
        test.ok(this.wrappedFn.alwaysCalledWith(1, 2, 3));
        test.done();
    },
 
    "call should fail when the retry predicate returns false": function(test) {
        var call = new FunctionCall(this.wrappedFn, [1, 2, 3], this.callback);
        call.retryIf(function(err) { return err.retriable; });
 
        var retriableError = new Error();
        retriableError.retriable = true;
 
        var fatalError = new Error();
        fatalError.retriable = false;
 
        this.wrappedFn.
            onCall(0).yields(retriableError).
            onCall(1).yields(retriableError).
            onCall(2).yields(fatalError);
 
        call.start(this.backoffFactory);
 
        for (var i = 0; i < 2; i++) {
            this.backoff.emit('ready');
        }
 
        test.equals(this.callback.callCount, 1);
        test.ok(this.callback.calledWith(fatalError));
        test.ok(this.wrappedFn.alwaysCalledWith(1, 2, 3));
        test.done();
    },
 
    "wrapped function's callback shouldn't be called after abort": function(test) {
        var call = new FunctionCall(function(callback) {
            call.abort(); // Abort in middle of wrapped function's execution.
            callback(null, 'ok');
        }, [], this.callback);
 
        call.start(this.backoffFactory);
 
        test.equals(this.callback.callCount, 1,
            'Wrapped function\'s callback shouldn\'t be called after abort.');
        test.ok(this.callback.calledWithMatch(sinon.match(function (err) {
            return !!err.message.match(/Backoff aborted/);
        }, "abort error")));
        test.done();
    },
 
    "abort event is emitted once when abort is called": function(test) {
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        this.wrappedFn.yields(new Error());
        var callEventSpy = sinon.spy();
 
        call.on('abort', callEventSpy);
        call.start(this.backoffFactory);
 
        call.abort();
        call.abort();
        call.abort();
 
        test.equals(callEventSpy.callCount, 1);
        test.done();
    },
 
    "getLastResult should return the last intermediary result": function(test) {
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        this.wrappedFn.yields(1);
        call.start(this.backoffFactory);
 
        for (var i = 2; i < 5; i++) {
            this.wrappedFn.yields(i);
            this.backoff.emit('ready');
            test.deepEqual([i], call.getLastResult());
        }
 
        this.wrappedFn.yields(null);
        this.backoff.emit('ready');
        test.deepEqual([null], call.getLastResult());
 
        test.done();
    },
 
    "getNumRetries should return the number of retries": function(test) {
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
 
        this.wrappedFn.yields(1);
        call.start(this.backoffFactory);
        // The inital call doesn't count as a retry.
        test.equals(0, call.getNumRetries());
 
        for (var i = 2; i < 5; i++) {
            this.wrappedFn.yields(i);
            this.backoff.emit('ready');
            test.equals(i - 1, call.getNumRetries());
        }
 
        this.wrappedFn.yields(null);
        this.backoff.emit('ready');
        test.equals(4, call.getNumRetries());
 
        test.done();
    },
 
    "wrapped function's errors should be propagated": function(test) {
        var call = new FunctionCall(this.wrappedFn, [1, 2, 3], this.callback);
        this.wrappedFn.throws(new Error());
        test.throws(function() {
            call.start(this.backoffFactory);
        }, Error);
        test.done();
    },
 
    "wrapped callback's errors should be propagated": function(test) {
        var call = new FunctionCall(this.wrappedFn, [1, 2, 3], this.callback);
        this.wrappedFn.yields(null, 'Success!');
        this.callback.throws(new Error());
        test.throws(function() {
            call.start(this.backoffFactory);
        }, Error);
        test.done();
    },
 
    "call event should be emitted when wrapped function gets called": function(test) {
        this.wrappedFn.yields(1);
        var callEventSpy = sinon.spy();
 
        var call = new FunctionCall(this.wrappedFn, [1, 'two'], this.callback);
        call.on('call', callEventSpy);
        call.start(this.backoffFactory);
 
        for (var i = 1; i < 5; i++) {
            this.backoff.emit('ready');
        }
 
        test.equal(5, callEventSpy.callCount,
            'The call event should have been emitted 5 times.');
        test.deepEqual([1, 'two'], callEventSpy.getCall(0).args,
            'The call event should carry function\'s args.');
        test.done();
    },
 
    "callback event should be emitted when callback is called": function(test) {
        var call = new FunctionCall(this.wrappedFn, [1, 'two'], this.callback);
        var callbackSpy = sinon.spy();
        call.on('callback', callbackSpy);
 
        this.wrappedFn.yields('error');
        call.start(this.backoffFactory);
 
        this.wrappedFn.yields(null, 'done');
        this.backoff.emit('ready');
 
        test.equal(2, callbackSpy.callCount,
            'Callback event should have been emitted 2 times.');
        test.deepEqual(['error'], callbackSpy.firstCall.args,
            'First callback event should carry first call\'s results.');
        test.deepEqual([null, 'done'], callbackSpy.secondCall.args,
            'Second callback event should carry second call\'s results.');
        test.done();
    },
 
    "backoff event should be emitted on backoff start": function(test) {
        var err = new Error('backoff event error');
        var call = new FunctionCall(this.wrappedFn, [1, 'two'], this.callback);
        var backoffSpy = sinon.spy();
 
        call.on('backoff', backoffSpy);
 
        this.wrappedFn.yields(err);
        call.start(this.backoffFactory);
        this.backoff.emit('backoff', 3, 1234, err);
 
        test.ok(this.backoff.backoff.calledWith(err),
            'The backoff instance should have been called with the error.');
        test.equal(1, backoffSpy.callCount,
            'Backoff event should have been emitted 1 time.');
        test.deepEqual([3, 1234, err], backoffSpy.firstCall.args,
            'Backoff event should carry the backoff number, delay and error.');
        test.done();
    }
};