-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhotstreak_test.go
99 lines (88 loc) · 3.03 KB
/
hotstreak_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
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
package hotstreak
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCreate(t *testing.T) {
hotstreak := New(Config{})
assert.False(t, hotstreak.IsActive())
assert.False(t, hotstreak.IsHot())
assert.Equal(t, hotstreak.counter, 0)
assert.Equal(t, hotstreak.config.Limit, 20)
assert.Equal(t, hotstreak.config.ActiveWait, time.Minute*5)
assert.Equal(t, hotstreak.config.HotWait, time.Minute*5)
}
func TestActivation(t *testing.T) {
t.Run("Should be able to activate", func(t *testing.T) {
hotstreak := New(Config{})
hotstreak.Activate()
assert.True(t, hotstreak.IsActive())
})
t.Run("Should not deactivate if any hit has been made in the activation time", func(t *testing.T) {
hotstreak := New(Config{ActiveWait: time.Millisecond * 500})
hotstreak.Activate()
assert.True(t, hotstreak.Hit().IsActive())
assert.Equal(t, hotstreak.counter, 1)
<-time.After(time.Millisecond * 600)
assert.True(t, hotstreak.IsActive())
assert.Equal(t, hotstreak.counter, 0)
<-time.After(time.Millisecond * 600)
assert.False(t, hotstreak.IsActive())
})
}
func TestDeactivation(t *testing.T) {
t.Run("Should be able to end activation after wait time", func(t *testing.T) {
hotstreak := New(Config{ActiveWait: time.Millisecond * 500})
hotstreak.Activate()
assert.True(t, hotstreak.IsActive())
<-time.After(time.Second)
assert.False(t, hotstreak.IsActive())
})
t.Run("Should not be able to end activation after wait time if active set to always", func(t *testing.T) {
hotstreak := New(Config{ActiveWait: time.Millisecond * 500, AlwaysActive: true})
hotstreak.Activate()
assert.True(t, hotstreak.IsActive())
<-time.After(time.Second)
assert.True(t, hotstreak.IsActive())
})
}
func TestHitting(t *testing.T) {
t.Run("Should be able to reach being hot", func(t *testing.T) {
hotstreak := New(Config{Limit: 2})
hotstreak.Hit()
assert.Equal(t, hotstreak.counter, 1)
hotstreak.Hit()
assert.Equal(t, hotstreak.counter, 2)
assert.True(t, hotstreak.IsHot())
})
t.Run("Should not remain hot when deactivated", func(t *testing.T) {
hotstreak := New(Config{Limit: 2})
hotstreak.Hit()
assert.Equal(t, hotstreak.counter, 1)
hotstreak.Hit()
assert.Equal(t, hotstreak.counter, 2)
assert.True(t, hotstreak.IsHot())
assert.False(t, hotstreak.Deactivate().IsHot())
assert.Equal(t, hotstreak.counter, 0)
})
t.Run("Should not be able to increase counter after getting hot", func(t *testing.T) {
hotstreak := New(Config{Limit: 2})
hotstreak.Hit()
assert.Equal(t, hotstreak.counter, 1)
hotstreak.Hit()
assert.Equal(t, hotstreak.counter, 2)
assert.True(t, hotstreak.IsHot())
assert.Equal(t, hotstreak.Hit().counter, 2)
})
}
func TestHot(t *testing.T) {
t.Run("Should be able to cool down after hot wait time", func(t *testing.T) {
hotstreak := New(Config{Limit: 2, HotWait: time.Millisecond * 500})
assert.True(t, hotstreak.Activate().Hit().Hit().IsHot())
assert.True(t, hotstreak.IsActive())
<-time.After(time.Second)
assert.False(t, hotstreak.IsHot())
assert.True(t, hotstreak.IsActive())
})
}