-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_test.go
157 lines (119 loc) · 3.83 KB
/
app_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
// app_test.go
package main
import (
"context"
"errors"
"math"
"testing"
"time"
"fortio.org/log"
"github.com/prometheus/client_golang/prometheus"
)
type MockSlackMessenger struct {
shouldError bool
}
func (m *MockSlackMessenger) PostMessage(_ SlackPostMessageRequest, _ string, _ string) error {
if m.shouldError {
return errors.New("mock error")
}
return nil
}
func TestApp_singleBurst_Success(t *testing.T) {
r := prometheus.NewRegistry()
metrics := NewMetrics(r)
messenger := &MockSlackMessenger{}
app := &App{
slackQueue: make(chan SlackPostMessageRequest, 2),
messenger: messenger,
metrics: metrics,
SlackPostMessageURL: "http://mock.url",
SlackToken: "mockToken",
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
go app.processQueue(ctx, 3, 1000*time.Millisecond, 1, 1000*time.Millisecond)
startTime := time.Now()
count := 10
for range count {
app.wg.Add(1)
app.slackQueue <- SlackPostMessageRequest{
Channel: "mockChannel",
}
}
log.S(log.Debug, "Posting messages done")
app.wg.Wait()
endTime := time.Now()
diffInSeconds := endTime.Sub(startTime).Seconds()
log.S(log.Debug, "diffInSeconds", log.Float64("diffInSeconds", diffInSeconds))
// The sum is always: (Amount of messages * RPS * delay in seconds) minus burst. In this case 20 *
// 1 - 10 = 10 seconds.
if math.RoundToEven(diffInSeconds) != 9 {
t.Fatal("Expected processQueue finish the job in ~9 seconds, give or take. Got", diffInSeconds)
}
}
func TestApp_MultiBurst_Success(t *testing.T) {
r := prometheus.NewRegistry()
metrics := NewMetrics(r)
messenger := &MockSlackMessenger{}
app := &App{
slackQueue: make(chan SlackPostMessageRequest, 2),
messenger: messenger,
metrics: metrics,
SlackPostMessageURL: "http://mock.url",
SlackToken: "mockToken",
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
go app.processQueue(ctx, 3, 1000*time.Millisecond, 10, 1000*time.Millisecond)
startTime := time.Now()
count := 20
for range count {
app.wg.Add(1)
app.slackQueue <- SlackPostMessageRequest{
Channel: "mockChannel",
}
}
log.S(log.Debug, "Posting messages done")
app.wg.Wait()
endTime := time.Now()
diffInSeconds := endTime.Sub(startTime).Seconds()
log.S(log.Debug, "diffInSeconds", log.Float64("diffInSeconds", diffInSeconds))
// The sum is always: (Amount of messages * RPS * delay in seconds) minus burst. In this case 20 *
// 1 - 10 = 10 seconds.
if math.RoundToEven(diffInSeconds) != 10 {
t.Fatal("Expected processQueue finish the job in ~9 seconds, give or take. Got", diffInSeconds)
}
}
func TestApp_TestSlackRequestRate(t *testing.T) {
r := prometheus.NewRegistry()
metrics := NewMetrics(r)
messenger := &MockSlackMessenger{}
app := &App{
slackQueue: make(chan SlackPostMessageRequest, 2),
messenger: messenger,
metrics: metrics,
SlackPostMessageURL: "http://mock.url",
SlackToken: "mockToken",
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
go app.processQueue(ctx, 3, 1000*time.Millisecond, 1, 250*time.Millisecond)
startTime := time.Now()
count := 20
for range count {
app.wg.Add(1)
app.slackQueue <- SlackPostMessageRequest{
Channel: "mockChannel",
}
}
log.S(log.Debug, "Posting messages done")
app.wg.Wait()
endTime := time.Now()
diffInSeconds := endTime.Sub(startTime).Seconds()
log.S(log.Debug, "diffInSeconds", log.Float64("diffInSeconds", diffInSeconds))
// The sum is always: (Amount of messages * RPS * delay in seconds) minus burst. In this case 20 *
// 4 * 1 - 10 = 5 seconds.
if math.RoundToEven(diffInSeconds) != 5 {
t.Fatal("Expected processQueue finish the job in ~5 seconds, give or take. Got", diffInSeconds)
}
}