-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsender_test.go
103 lines (89 loc) · 1.75 KB
/
sender_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
package tsender
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
type mockProvider struct {
send func(msg interface{})
}
func (p *mockProvider) Send(msg interface{}) {
if p.send != nil {
p.send(msg)
}
}
func TestNewSender(t *testing.T) {
s := NewSender(nil)
require.NotNil(t, s)
require.Nil(t, s.provider)
require.NotNil(t, s.queue)
require.NotNil(t, s.distribute)
require.NotNil(t, s.done)
}
func TestSender_Run(t *testing.T) {
t.Parallel()
s := NewSender(nil)
done := make(chan struct{})
go func() {
s.Run(0)
close(done)
}()
s.Stop()
select {
case <-done:
case <-time.After(time.Second):
t.Error("timeout")
}
}
func TestSender_Send(t *testing.T) {
t.Parallel()
execute(t, 30, int64(duration))
}
func TestSender_SendOrdinary(t *testing.T) {
t.Parallel()
execute(t, 3, latencyOrdinary)
}
func TestSender_SendGroup(t *testing.T) {
t.Parallel()
execute(t, 2, latencyGroup)
}
func execute(t *testing.T, msgCount int, latency int64) {
var (
prevTime int64
count int
done = make(chan struct{})
)
mock := &mockProvider{
send: func(msg interface{}) {
msgTime := time.Now().UnixNano()
diff := msgTime - (prevTime + int64(duration))
approximateError := int64(time.Millisecond / 2)
minNextTime := prevTime + latency - approximateError
require.GreaterOrEqual(t, msgTime, minNextTime, diff)
prevTime = msgTime
count++
if count == msgCount {
close(done)
}
},
}
s := NewSender(mock)
go s.Run(1)
for i := 0; i < msgCount; i++ {
id := i
switch latency {
case latencyGroup:
id = -1
case latencyOrdinary:
id = 1
}
s.Send(int64(id), nil)
}
select {
case <-done:
s.Stop()
case <-time.After(time.Duration(latency * int64(msgCount))):
t.Error("timeout")
return
}
}