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
/*
 * Copyright (c) 2012 Mathieu Turcotte
 * Licensed under the MIT license.
 */
 
var sinon = require('sinon');
 
var Backoff = require('../lib/backoff');
var BackoffStrategy = require('../lib/strategy/strategy');
 
exports["Backoff"] = {
    setUp: function(callback) {
        this.backoffStrategy = sinon.stub(new BackoffStrategy());
        this.backoff = new Backoff(this.backoffStrategy);
        this.clock = sinon.useFakeTimers();
        this.spy = new sinon.spy();
        callback();
    },
 
    tearDown: function(callback) {
        this.clock.restore();
        callback();
    },
 
    "the backoff event should be emitted when backoff starts": function(test) {
        this.backoffStrategy.next.returns(10);
        this.backoff.on('backoff', this.spy);
 
        this.backoff.backoff();
 
        test.ok(this.spy.calledOnce,
            'Backoff event should be emitted when backoff starts.');
        test.done();
    },
 
    "the ready event should be emitted on backoff completion": function(test) {
        this.backoffStrategy.next.returns(10);
        this.backoff.on('ready', this.spy);
 
        this.backoff.backoff();
        this.clock.tick(10);
 
        test.ok(this.spy.calledOnce,
            'Ready event should be emitted when backoff ends.');
        test.done();
    },
 
    "the backoff event should be passed the backoff delay": function(test) {
        this.backoffStrategy.next.returns(989);
        this.backoff.on('backoff', this.spy);
 
        this.backoff.backoff();
 
        test.equal(this.spy.getCall(0).args[1], 989, 'Backoff event should ' +
            'carry the backoff delay as its second argument.');
        test.done();
    },
 
    "the ready event should be passed the backoff delay": function(test) {
        this.backoffStrategy.next.returns(989);
        this.backoff.on('ready', this.spy);
 
        this.backoff.backoff();
        this.clock.tick(989);
 
        test.equal(this.spy.getCall(0).args[1], 989, 'Ready event should ' +
            'carry the backoff delay as its second argument.');
        test.done();
    },
 
    "the fail event should be emitted when backoff limit is reached": function(test) {
        var err = new Error('Fail');
 
        this.backoffStrategy.next.returns(10);
        this.backoff.on('fail', this.spy);
 
        this.backoff.failAfter(2);
 
        // Consume first 2 backoffs.
        for (var i = 0; i < 2; i++) {
            this.backoff.backoff();
            this.clock.tick(10);
        }
 
        // Failure should occur on the third call, and not before.
        test.ok(!this.spy.calledOnce, 'Fail event shouldn\'t have been emitted.');
        this.backoff.backoff(err);
        test.ok(this.spy.calledOnce, 'Fail event should have been emitted.');
        test.equal(this.spy.getCall(0).args[0], err, 'Error should be passed');
 
        test.done();
    },
 
    "calling backoff while a backoff is in progress should throw an error": function(test) {
        this.backoffStrategy.next.returns(10);
        var backoff = this.backoff;
 
        backoff.backoff();
 
        test.throws(function() {
            backoff.backoff();
        }, /in progress/);
 
        test.done();
    },
 
    "backoff limit should be greater than 0": function(test) {
        var backoff = this.backoff;
        test.throws(function() {
            backoff.failAfter(0);
        }, /greater than 0 but got 0/);
        test.done();
    },
 
    "reset should cancel any backoff in progress": function(test) {
        this.backoffStrategy.next.returns(10);
        this.backoff.on('ready', this.spy);
 
        this.backoff.backoff();
 
        this.backoff.reset();
        this.clock.tick(100);   // 'ready' should not be emitted.
 
        test.equals(this.spy.callCount, 0, 'Reset should have aborted the backoff.');
        test.done();
    },
 
    "reset should reset the backoff strategy": function(test) {
        this.backoff.reset();
        test.ok(this.backoffStrategy.reset.calledOnce,
            'The backoff strategy should have been resetted.');
        test.done();
    },
 
    "backoff should be reset after fail": function(test) {
        this.backoffStrategy.next.returns(10);
 
        this.backoff.failAfter(1);
 
        this.backoff.backoff();
        this.clock.tick(10);
        this.backoff.backoff();
 
        test.ok(this.backoffStrategy.reset.calledOnce,
            'Backoff should have been resetted after failure.');
        test.done();
    },
 
    "the backoff number should increase from 0 to N - 1": function(test) {
        this.backoffStrategy.next.returns(10);
        this.backoff.on('backoff', this.spy);
 
        var expectedNumbers = [0, 1, 2, 3, 4];
        var actualNumbers = [];
 
        for (var i = 0; i < expectedNumbers.length; i++) {
            this.backoff.backoff();
            this.clock.tick(10);
            actualNumbers.push(this.spy.getCall(i).args[0]);
        }
 
        test.deepEqual(expectedNumbers, actualNumbers,
            'Backoff number should increase from 0 to N - 1.');
        test.done();
    }
};