-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththrottle_test.go
118 lines (98 loc) · 2.83 KB
/
throttle_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
package goext_test
import (
"errors"
"fmt"
"testing"
"time"
"github.com/ayonli/goext"
"github.com/stretchr/testify/assert"
)
func ExampleThrottle_withoutKey() {
fn := goext.Throttle[int](func(arg int) (int, error) {
return arg * 2, nil
}, time.Millisecond*5, "", false)
fmt.Println(fn(1))
fmt.Println(fn(2))
time.Sleep(time.Millisecond * 6)
fmt.Println(fn(3))
// Output:
// 2 <nil>
// 2 <nil>
// 6 <nil>
}
func ExampleThrottle_withKey() {
res1, err1 := goext.Throttle[int](func(arg int) (int, error) {
return arg * 2, nil
}, time.Millisecond*5, "foo", false)(1)
res2, err2 := goext.Throttle[int](func(arg int) (int, error) {
return arg * 2, nil
}, time.Millisecond*5, "foo", false)(2)
fmt.Println(res1, err1)
fmt.Println(res2, err2)
time.Sleep(time.Millisecond * 6)
res3, err3 := goext.Throttle[int](func(arg int) (int, error) {
return arg * 2, nil
}, time.Millisecond*5, "foo", false)(3)
fmt.Println(res3, err3)
// Output:
// 2 <nil>
// 2 <nil>
// 6 <nil>
}
func TestThrottle(suit *testing.T) {
suit.Run("failedWithoutKey", func(t *testing.T) {
fn := goext.Throttle[int](func(arg int) (int, error) {
if arg == 1 {
return 0, errors.New("something went wrong")
} else {
return 0, errors.New("something else went wrong")
}
}, time.Millisecond*5, "", false)
_, err1 := fn(1)
_, err2 := fn(2)
assert.Equal(t, errors.New("something went wrong"), err1)
assert.Equal(t, err1, err2)
time.Sleep(time.Millisecond * 6)
_, err3 := fn(2)
assert.Equal(t, errors.New("something else went wrong"), err3)
})
suit.Run("failedWithKey", func(t *testing.T) {
_, err1 := goext.Throttle[int](func(arg int) (int, error) {
if arg == 1 {
return 0, errors.New("something went wrong")
} else {
return 0, errors.New("something else went wrong")
}
}, time.Millisecond*5, "bar", false)(1)
_, err2 := goext.Throttle[int](func(arg int) (int, error) {
if arg == 1 {
return 0, errors.New("something went wrong")
} else {
return 0, errors.New("something else went wrong")
}
}, time.Millisecond*5, "bar", false)(2)
assert.Equal(t, errors.New("something went wrong"), err1)
assert.Equal(t, err1, err2)
time.Sleep(time.Millisecond * 6)
_, err3 := goext.Throttle[int](func(arg int) (int, error) {
if arg == 1 {
return 0, errors.New("something went wrong")
} else {
return 0, errors.New("something else went wrong")
}
}, time.Millisecond*5, "bar", false)(2)
assert.Equal(t, errors.New("something else went wrong"), err3)
})
suit.Run("noWait", func(t *testing.T) {
fn := goext.Throttle[int](func(arg int) (int, error) {
return arg * 2, nil
}, time.Millisecond*5, "", true)
res1, _ := fn(1)
time.Sleep(time.Millisecond * 6)
res2, _ := fn(2)
assert.Equal(t, res2, res1)
time.Sleep(time.Millisecond)
res3, _ := fn(3)
assert.Equal(t, res3, 4)
})
}