-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger_test.go
93 lines (79 loc) · 2.52 KB
/
logger_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
package requests
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type mockLoggerRecorder struct {
Records []string
}
func (m *mockLoggerRecorder) Write(p []byte) (n int, err error) {
m.Records = append(m.Records, string(p))
return len(p), nil
}
func TestDefaultLoggerLevels(t *testing.T) {
rec := &mockLoggerRecorder{}
logger := NewDefaultLogger(rec, LevelDebug)
logger.Debugf("debug %s", "message")
logger.Infof("info %s", "message")
logger.Warnf("warn %s", "message")
logger.Errorf("error %s", "message")
assert.Len(t, rec.Records, 4, "Should log 4 messages")
assert.Contains(t, rec.Records[0], "debug message", "Debug log message should match")
assert.Contains(t, rec.Records[1], "info message", "Info log message should match")
assert.Contains(t, rec.Records[2], "warn message", "Warn log message should match")
assert.Contains(t, rec.Records[3], "error message", "Error log message should match")
}
type mockLogger struct {
Infos []string
Errors []string
}
func (m *mockLogger) Debugf(format string, v ...any) {}
func (m *mockLogger) Infof(format string, v ...any) {
m.Infos = append(m.Infos, fmt.Sprintf(format, v...))
}
func (m *mockLogger) Warnf(format string, v ...any) {}
func (m *mockLogger) Errorf(format string, v ...any) {
m.Errors = append(m.Errors, fmt.Sprintf(format, v...))
}
func (m *mockLogger) SetLevel(level Level) {}
func TestRetryLogMessage(t *testing.T) {
// Initialize attempt counter
var attempts int
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attempts++
if attempts == 1 {
// Fail initially to trigger a retry
w.WriteHeader(http.StatusServiceUnavailable)
} else {
// Succeed in the next attempt
w.WriteHeader(http.StatusOK)
}
}))
defer server.Close()
mockLogger := &mockLogger{}
client := Create(&Config{
BaseURL: server.URL,
Logger: mockLogger,
}).SetMaxRetries(1).SetRetryStrategy(func(attempt int) time.Duration {
return 0 // No delay for testing
})
// Making a request that should trigger a retry
_, err := client.Get("/test").Send(context.Background())
assert.Nil(t, err, "Did not expect an error after retry")
// Check if the retry log message was recorded
expectedLogMessage := "Retrying request (attempt 1) after backoff"
found := false
for _, logMsg := range mockLogger.Infos {
if strings.Contains(logMsg, expectedLogMessage) {
found = true
break
}
}
assert.True(t, found, "Expected retry log message was not recorded")
}