-
Notifications
You must be signed in to change notification settings - Fork 0
/
counters_test.go
54 lines (42 loc) · 1.03 KB
/
counters_test.go
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
package relay
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestAdd(t *testing.T) {
counters := &Counters{
Window: make([]RequestInfo, 0),
WindowWidth: 1 * time.Second,
}
assert.Equal(t, 0, len(counters.Window))
counters.Add(true)
assert.Equal(t, 1, len(counters.Window))
}
func TestFailuresAndSuccessesCount(t *testing.T) {
counters := &Counters{
Window: make([]RequestInfo, 0),
WindowWidth: 5 * time.Second,
}
relay := &Relay{
counters: *counters,
}
// Add 2 successes and 1 failure.
relay.counters.Add(true)
relay.counters.Add(true)
relay.counters.Add(false)
failures, successes := counters.FailuresAndSuccessesCount(relay)
assert.Equal(t, 1, failures)
assert.Equal(t, 2, successes)
}
func TestClear(t *testing.T) {
counters := &Counters{
Window: make([]RequestInfo, 0),
WindowWidth: 5 * time.Second,
HalfOpenRequests: 5,
}
counters.Add(true)
counters.clear()
assert.Equal(t, 0, len(counters.Window))
assert.Equal(t, 0, int(counters.HalfOpenRequests))
}