-
Notifications
You must be signed in to change notification settings - Fork 0
/
cable_test.go
175 lines (143 loc) · 4.75 KB
/
cable_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
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
167
168
169
170
171
172
173
174
175
// * cable <https://github.com/jahnestacado/cable>
// * Copyright (c) 2020 Ioannis Tzanellis
// * Licensed under the MIT License (MIT).
package cable
import (
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestExecuteEvery(t *testing.T) {
t.Run("should keep calling the function until it returns false", func(t *testing.T) {
assert := assert.New(t)
var wg sync.WaitGroup
interval := time.Millisecond
expectedTimesInvoked := int32(5)
var timesInvoked int32
wg.Add(1)
ExecuteEvery(interval, func() bool {
atomic.AddInt32(×Invoked, 1)
if timesInvoked == expectedTimesInvoked {
wg.Done()
return false
}
return true
})
wg.Wait()
assert.Equal(expectedTimesInvoked, atomic.LoadInt32(×Invoked))
})
t.Run("should keep calling the function until it is canceled", func(t *testing.T) {
assert := assert.New(t)
cancelAfterMillis := 20
intervalMillis := 5
expectedInvocations := cancelAfterMillis / intervalMillis
var timesInvoked int32
var wg sync.WaitGroup
wg.Add(expectedInvocations)
timeBefore := time.Now().UnixNano() / int64(time.Millisecond)
cancel := ExecuteEvery(time.Duration(intervalMillis)*time.Millisecond, func() bool {
defer wg.Done()
atomic.AddInt32(×Invoked, 1)
return true
})
wg.Wait()
timeAfter := time.Now().UnixNano() / int64(time.Millisecond)
cancel()
assert.InDelta(timeAfter, timeBefore, float64(cancelAfterMillis+1))
assert.Equal(expectedInvocations, int(timesInvoked))
})
}
func TestExecuteEveryImmediate(t *testing.T) {
t.Run("should call the function immediately", func(t *testing.T) {
assert := assert.New(t)
interval := 2 * time.Millisecond
expectedTimesInvoked := int32(1)
var timesInvoked int32
var wg sync.WaitGroup
wg.Add(1)
timeBefore := time.Now().UnixNano() / int64(time.Millisecond)
ExecuteEveryImmediate(interval, func() bool {
atomic.AddInt32(×Invoked, 1)
wg.Done()
return false
})
wg.Wait()
timeAfter := time.Now().UnixNano() / int64(time.Millisecond)
assert.InDelta(timeAfter, timeBefore, float64(1))
assert.Equal(expectedTimesInvoked, atomic.LoadInt32(×Invoked))
})
}
func TestThrottle(t *testing.T) {
throttleIntervalMillis := 3
executionIntervalMillis := 1
totalInvocations := 100
expectedInvocations := int32((totalInvocations * executionIntervalMillis) / throttleIntervalMillis)
t.Run("should throttle the function with the expected rate", func(t *testing.T) {
assert := assert.New(t)
var timesInvoked int32
throttledFunc := Throttle(func() {
atomic.AddInt32(×Invoked, 1)
}, time.Duration(throttleIntervalMillis)*time.Millisecond)
for i := 0; i < totalInvocations; i++ {
throttledFunc()
time.Sleep(time.Duration(executionIntervalMillis) * time.Millisecond)
}
// give a leeway of an extra iteration to allow throttling for last invocation to kick in
time.Sleep(time.Duration(throttleIntervalMillis) * time.Millisecond)
assert.InDelta(expectedInvocations, atomic.LoadInt32(×Invoked), 1)
})
}
func TestThrottleImmediate(t *testing.T) {
throttleIntervalMillis := 10
expectedInvocations := int32(1)
t.Run("should invoke the function immediately", func(t *testing.T) {
assert := assert.New(t)
var timesInvoked int32
throttledFunc := ThrottleImmediate(func() {
atomic.AddInt32(×Invoked, 1)
}, time.Duration(throttleIntervalMillis)*time.Millisecond)
throttledFunc()
assert.Equal(expectedInvocations, atomic.LoadInt32(×Invoked))
})
}
func TestDebounce(t *testing.T) {
debounceIntervalMillis := 5
totalInvocations := 100
expectedInvocations := 1
t.Run("should debounce then function with the expected rate", func(t *testing.T) {
assert := assert.New(t)
var wg sync.WaitGroup
var timesInvoked int32
debouncedFunc := Debounce(func() {
defer wg.Done()
atomic.AddInt32(×Invoked, 1)
}, time.Duration(debounceIntervalMillis)*time.Millisecond)
wg.Add(expectedInvocations)
timeBefore := time.Now().UnixNano() / int64(time.Millisecond)
go func() {
for i := 0; i < totalInvocations; i++ {
debouncedFunc()
}
debouncedFunc()
}()
wg.Wait()
timeAfter := time.Now().UnixNano() / int64(time.Millisecond)
assert.Equal(expectedInvocations, int(atomic.LoadInt32(×Invoked)))
assert.InDelta(timeAfter, timeBefore, float64(debounceIntervalMillis+1))
})
}
func TestDebounceImmediate(t *testing.T) {
debounceIntervalMillis := 10
expectedInvocations := int32(1)
t.Run("should invoke the function immediately", func(t *testing.T) {
assert := assert.New(t)
var timesInvoked int32
debouncedFunc := DebounceImmediate(func() {
atomic.AddInt32(×Invoked, 1)
}, time.Duration(debounceIntervalMillis)*time.Millisecond)
debouncedFunc()
assert.Equal(expectedInvocations, atomic.LoadInt32(×Invoked))
})
}