-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_logger_test.go
More file actions
191 lines (163 loc) · 5.68 KB
/
example_logger_test.go
File metadata and controls
191 lines (163 loc) · 5.68 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package httpx_test
import (
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"sync/atomic"
"testing"
"time"
"github.com/slashdevops/httpx"
)
// TestExample_withLogger_retryLogging demonstrates how to enable logging for HTTP retries.
// By default, logging is disabled. Pass a *slog.Logger to see retry attempts.
func TestExample_withLogger_retryLogging(t *testing.T) {
t.Skip("This is a demonstration of logger usage, not an automated test")
attempts := atomic.Int32{}
// Create a test server that fails twice, then succeeds
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attempt := attempts.Add(1)
if attempt <= 2 {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "Success")
}))
defer server.Close()
// Create a logger with a custom level (Info level to see retry warnings)
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))
// Create client with logging enabled using ClientBuilder
client := httpx.NewClientBuilder().
WithMaxRetries(3).
WithRetryBaseDelay(500 * time.Millisecond). // Use valid delay (>= 300ms)
WithRetryMaxDelay(10 * time.Second). // Use valid delay (>= 300ms)
WithRetryStrategy(httpx.ExponentialBackoffStrategy).
WithLogger(logger).
Build()
fmt.Println("Making request with retry logging enabled...")
resp, err := client.Get(server.URL)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Response: %s\n", body)
// Logs will show retry warnings (omitted from output due to timestamps)
fmt.Println("Request succeeded after retries")
}
// TestExample_withLogger_genericClient demonstrates logging with the GenericClient.
func TestExample_withLogger_genericClient(t *testing.T) {
t.Skip("This is a demonstration of logger usage, not an automated test")
type Response struct {
Message string `json:"message"`
}
attempts := atomic.Int32{}
// Create a test server that fails once, then succeeds
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attempt := attempts.Add(1)
if attempt == 1 {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"message":"Success"}`)
}))
defer server.Close()
// Create a logger
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelWarn,
}))
// Create generic client with logging
client := httpx.NewGenericClient[Response](
httpx.WithMaxRetries[Response](2),
httpx.WithRetryBaseDelay[Response](500*time.Millisecond), // Use valid delay
httpx.WithRetryMaxDelay[Response](10*time.Second), // Use valid delay
httpx.WithRetryStrategy[Response](httpx.ExponentialBackoffStrategy),
httpx.WithLogger[Response](logger),
)
fmt.Println("Making typed request with retry logging...")
resp, err := client.Get(server.URL)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Response: %s\n", resp.Data.Message)
}
// Example_withLogger_disabled demonstrates the default behavior with no logging.
// This example shows that by default, logging is disabled for clean, silent operation.
func Example_withLogger_disabled() {
attempts := atomic.Int32{}
// Create a test server that fails twice, then succeeds
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attempt := attempts.Add(1)
if attempt <= 2 {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "Success")
}))
defer server.Close()
// Create client WITHOUT logging (default behavior)
// No logger means silent retries - clean operation without log noise
client := httpx.NewClientBuilder().
WithMaxRetries(3).
WithRetryBaseDelay(500 * time.Millisecond). // Use valid delay
WithRetryMaxDelay(10 * time.Second). // Use valid delay
Build() // No WithLogger call = no logging
fmt.Println("Making request with logging disabled (default)...")
resp, err := client.Get(server.URL)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Response: %s\n", body)
fmt.Println("No retry logs appear - silent operation")
// Output:
// Making request with logging disabled (default)...
// Response: Success
// No retry logs appear - silent operation
}
// TestExample_newHTTPRetryClient_withLogger demonstrates using NewHTTPRetryClient with logging.
func TestExample_newHTTPRetryClient_withLogger(t *testing.T) {
t.Skip("This is a demonstration of logger usage, not an automated test")
attempts := atomic.Int32{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attempt := attempts.Add(1)
if attempt == 1 {
w.WriteHeader(http.StatusBadGateway)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "Success")
}))
defer server.Close()
// Create a logger
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelWarn,
}))
// Create retry client with logger
client := httpx.NewHTTPRetryClient(
httpx.WithMaxRetriesRetry(3),
httpx.WithRetryStrategyRetry(httpx.ExponentialBackoff(500*time.Millisecond, 10*time.Second)),
httpx.WithLoggerRetry(logger),
)
fmt.Println("Making request with NewHTTPRetryClient and logging...")
resp, err := client.Get(server.URL)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Response: %s\n", body)
}