-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.test.ts
84 lines (73 loc) · 2.79 KB
/
core.test.ts
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
import { BackoffPool } from '../src/core'
import { expect } from 'chai'
let loginBackoffPool: BackoffPool
let ip = 'fake-ip'
let defaultInterval = 0
let initialBackoffInterval = 1000
let backoffFactor = 2
beforeEach(() => {
loginBackoffPool = new BackoffPool({
defaultInterval,
initialBackoffInterval,
backoffFactor,
})
})
it('should get default interval initially', () => {
expect(loginBackoffPool.getInterval(ip)).to.equal(defaultInterval)
})
it('should be unlocked by default', () => {
expect(loginBackoffPool.isLocked(ip)).to.be.false
expect(loginBackoffPool.isAvailable(ip)).to.be.true
expect(loginBackoffPool.getUnlockTime(ip)).to.be.undefined
})
it('should get initial backoff interval after first failure', () => {
loginBackoffPool.fail(ip)
expect(loginBackoffPool.getInterval(ip)).to.equal(initialBackoffInterval)
expect(loginBackoffPool.getUnlockTime(ip)).to.equal(
Date.now() + initialBackoffInterval,
)
})
it('should apply backoff factor once after two failures', () => {
loginBackoffPool.fail(ip)
loginBackoffPool.fail(ip)
let expectedInterval = initialBackoffInterval * backoffFactor
expect(loginBackoffPool.getInterval(ip)).to.equal(expectedInterval)
expect(loginBackoffPool.getUnlockTime(ip)).to.equal(
Date.now() + expectedInterval,
)
})
it('should apply backoff factor twice after three failures', () => {
loginBackoffPool.fail(ip)
loginBackoffPool.fail(ip)
loginBackoffPool.fail(ip)
let expectedInterval = initialBackoffInterval * backoffFactor ** 2
expect(loginBackoffPool.getInterval(ip)).to.equal(expectedInterval)
expect(loginBackoffPool.getUnlockTime(ip)).to.equal(
Date.now() + expectedInterval,
)
})
it('should reset to default interval after a successful attempt', () => {
loginBackoffPool.fail(ip)
expect(loginBackoffPool.getInterval(ip)).to.not.equal(defaultInterval)
loginBackoffPool.success(ip)
expect(loginBackoffPool.getInterval(ip)).to.equal(defaultInterval)
loginBackoffPool.fail(ip)
expect(loginBackoffPool.getInterval(ip)).to.not.equal(defaultInterval)
loginBackoffPool.fail(ip)
expect(loginBackoffPool.getInterval(ip)).to.not.equal(defaultInterval)
loginBackoffPool.success(ip)
expect(loginBackoffPool.getInterval(ip)).to.equal(defaultInterval)
expect(loginBackoffPool.getUnlockTime(ip)).to.be.undefined
})
it('should apply random backoff within range', () => {
let originInterval = loginBackoffPool.getInterval(ip)
let range = 1000
loginBackoffPool.applyRandomBackoff(ip, range)
let newInterval = loginBackoffPool.getInterval(ip)
let diff = newInterval - originInterval
expect(diff).to.below(originInterval + range)
expect(diff).to.above(originInterval)
let unlockTime = loginBackoffPool.getUnlockTime(ip)
expect(unlockTime).to.above(Date.now())
expect(unlockTime).to.below(Date.now() + range)
})