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
/*
 * Copyright (c) 2012 Mathieu Turcotte
 * Licensed under the MIT license.
 */
 
var sinon = require('sinon');
 
var ExponentialBackoffStrategy = require('../lib/strategy/exponential');
 
exports["ExponentialBackoffStrategy"] = {
 
    "backoff delays should follow an exponential sequence": function(test) {
        var strategy = new ExponentialBackoffStrategy({
            initialDelay: 10,
            maxDelay: 1000
        });
 
        // Exponential sequence: x[i] = x[i-1] * 2.
        var expectedDelays = [10, 20, 40, 80, 160, 320, 640, 1000, 1000];
        var actualDelays = expectedDelays.map(function () {
            return strategy.next();
        });
 
        test.deepEqual(expectedDelays, actualDelays,
            'Generated delays should follow an exponential sequence.');
        test.done();
    },
 
    "backoff delay factor should be configurable": function (test) {
        var strategy = new ExponentialBackoffStrategy({
            initialDelay: 10,
            maxDelay: 270,
            factor: 3
        });
 
        // Exponential sequence: x[i] = x[i-1] * 3.
        var expectedDelays = [10, 30, 90, 270, 270];
        var actualDelays = expectedDelays.map(function () {
            return strategy.next();
        });
 
        test.deepEqual(expectedDelays, actualDelays,
            'Generated delays should follow a configurable exponential sequence.');
        test.done();
    },
 
    "backoff delays should restart from the initial delay after reset": function(test) {
        var strategy = new ExponentialBackoffStrategy({
            initialDelay: 10,
            maxDelay: 1000
        });
 
        strategy.next();
        strategy.reset();
 
        var backoffDelay = strategy.next();
        test.equals(backoffDelay, 10,
            'Strategy should return the initial delay after reset.');
        test.done();
    }
};