-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathlogthrottle_test.go
More file actions
167 lines (137 loc) · 4.67 KB
/
Copy pathlogthrottle_test.go
File metadata and controls
167 lines (137 loc) · 4.67 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package retryer
import (
"fmt"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
)
type testLogger struct {
debugs, infos, warns, errors []string
}
func (l *testLogger) Errorf(format string, args ...interface{}) {
line := fmt.Sprintf(format, args...)
l.errors = append(l.errors, line)
}
func (l *testLogger) Error(args ...interface{}) {
line := fmt.Sprint(args...)
l.errors = append(l.errors, line)
}
func (l *testLogger) Debugf(format string, args ...interface{}) {
line := fmt.Sprintf(format, args...)
l.debugs = append(l.debugs, line)
}
func (l *testLogger) Debug(args ...interface{}) {
line := fmt.Sprint(args...)
l.debugs = append(l.debugs, line)
}
func (l *testLogger) Warnf(format string, args ...interface{}) {
line := fmt.Sprintf(format, args...)
l.warns = append(l.warns, line)
}
func (l *testLogger) Warn(args ...interface{}) {
line := fmt.Sprint(args...)
l.warns = append(l.warns, line)
}
func (l *testLogger) Infof(format string, args ...interface{}) {
line := fmt.Sprintf(format, args...)
l.infos = append(l.infos, line)
}
func (l *testLogger) Info(args ...interface{}) {
line := fmt.Sprint(args...)
l.infos = append(l.infos, line)
}
func TestLogThrottleRetryerLogging(t *testing.T) {
setup()
defer tearDown()
const throttleDebugLine = "AWS API call throttled: Operation: Test, Error: RequestLimitExceeded: Test AWS Error"
const watchGoroutineExitLine = "LogThrottleRetryer watch throttle events goroutine exiting"
const throttleSummaryLinePrefix = "AWS API call has been throttled"
const throttleBatchSize = 100
const totalThrottleCnt = throttleBatchSize * 2 // Test total 2 batches
var throttleDetectedLine = fmt.Sprintf("AWS API call throttling detected, further throttling messages may be suppressed for up to %v depending on the log level, error message: Operation: Test, Error: RequestLimitExceeded: Test AWS Error", throttleReportTimeout)
l := &testLogger{}
r := NewLogThrottleRetryer(l)
req := &request.Request{
Error: awserr.New("RequestLimitExceeded", "Test AWS Error", nil),
Operation: &request.Operation{Name: "Test"},
}
// Generate 200 throttles with a time gap between
for i := 0; i < throttleBatchSize; i++ {
r.ShouldRetry(req)
time.Sleep(10 * time.Millisecond)
}
time.Sleep(1500 * time.Millisecond)
for i := 0; i < throttleBatchSize; i++ {
r.ShouldRetry(req)
time.Sleep(10 * time.Millisecond)
}
r.Stop()
time.Sleep(200 * time.Millisecond) // Wait a bit to collect all logs
// Check the debug level log messages
debugCnt := 0
for _, d := range l.debugs {
if d == throttleDebugLine {
debugCnt++
} else if d != watchGoroutineExitLine {
t.Errorf("unexpected debug log found: %v", d)
}
}
// Check the info level log messages
detectCnt := 0
throttleCnt := 0
for _, info := range l.infos {
if info == throttleDetectedLine {
detectCnt++
} else if strings.HasPrefix(info, throttleSummaryLinePrefix) {
n := 0
fmt.Sscanf(info, throttleSummaryLinePrefix+" %d", &n)
throttleCnt += n
}
}
if detectCnt+debugCnt != totalThrottleCnt {
t.Errorf("wrong number of throttle detected log found, expecting %v, got %v", totalThrottleCnt, detectCnt+debugCnt)
}
if throttleCnt != totalThrottleCnt {
t.Errorf("wrong number of throttle count sum reported from info logs, expecting %v, got %v", totalThrottleCnt, throttleCnt)
}
}
// TestShouldRetryDoesNotBlockAfterStop verifies ShouldRetry does not block once the
// retryer is stopped (its consumer goroutine no longer drains the throttle channel).
func TestShouldRetryDoesNotBlockAfterStop(t *testing.T) {
l := &testLogger{}
r := NewLogThrottleRetryer(l)
// Stop the retryer, which closes the done channel and exits the consumer goroutine
r.Stop()
time.Sleep(50 * time.Millisecond) // Give the goroutine time to exit
req := &request.Request{
Error: awserr.New("RequestLimitExceeded", "Test AWS Error", nil),
Operation: &request.Operation{Name: "Test"},
}
// Call ShouldRetry in a goroutine and use a timeout to detect blocking
done := make(chan bool, 1)
go func() {
// Call ShouldRetry multiple times to exceed channel capacity (1)
for i := 0; i < 10; i++ {
r.ShouldRetry(req)
}
done <- true
}()
select {
case <-done:
// Success: ShouldRetry did not block
case <-time.After(2 * time.Second):
t.Fatal("ShouldRetry blocked after retryer was stopped - potential deadlock")
}
}
func setup() {
throttleReportTimeout = 400 * time.Millisecond
throttleReportCheckPeriod = 50 * time.Millisecond
}
func tearDown() {
throttleReportTimeout = 1 * time.Minute
throttleReportCheckPeriod = 5 * time.Second
}