-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclient_test.go
More file actions
264 lines (211 loc) · 6.84 KB
/
Copy pathclient_test.go
File metadata and controls
264 lines (211 loc) · 6.84 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package httprc_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"sync"
"testing"
"time"
"github.com/lestrrat-go/httprc/v3"
"github.com/lestrrat-go/httprc/v3/errsink"
"github.com/lestrrat-go/httprc/v3/tracesink"
"github.com/stretchr/testify/require"
)
func TestNewClient(t *testing.T) {
t.Parallel()
t.Run("default client", func(t *testing.T) {
t.Parallel()
cl := httprc.NewClient()
require.NotNil(t, cl)
})
t.Run("with custom options", func(t *testing.T) {
t.Parallel()
// Test with custom worker count
cl := httprc.NewClient(httprc.WithWorkers(10))
require.NotNil(t, cl)
// Test with custom HTTP client
customHTTPClient := &http.Client{Timeout: 5 * time.Second}
cl = httprc.NewClient(httprc.WithHTTPClient(customHTTPClient))
require.NotNil(t, cl)
// Test with custom error sink
cl = httprc.NewClient(httprc.WithErrorSink(errsink.NewNop()))
require.NotNil(t, cl)
// Test with custom trace sink
cl = httprc.NewClient(httprc.WithTraceSink(tracesink.NewNop()))
require.NotNil(t, cl)
// Test with whitelist
cl = httprc.NewClient(httprc.WithWhitelist(httprc.NewInsecureWhitelist()))
require.NotNil(t, cl)
})
t.Run("with zero workers", func(t *testing.T) {
// Should default to 1 worker when 0 is specified
cl := httprc.NewClient(httprc.WithWorkers(0))
require.NotNil(t, cl)
})
t.Run("with negative workers", func(t *testing.T) {
// Should default to 1 worker when negative is specified
cl := httprc.NewClient(httprc.WithWorkers(-1))
require.NotNil(t, cl)
})
}
func TestClientStart(t *testing.T) {
t.Parallel()
t.Run("successful start", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cl := httprc.NewClient()
ctrl, err := cl.Start(ctx)
require.NoError(t, err)
require.NotNil(t, ctrl)
t.Cleanup(func() { ctrl.Shutdown(time.Second) })
})
t.Run("start twice should fail", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cl := httprc.NewClient()
ctrl1, err := cl.Start(ctx)
require.NoError(t, err)
require.NotNil(t, ctrl1)
defer ctrl1.Shutdown(time.Second)
// Second start should fail
ctrl2, err := cl.Start(ctx)
require.Error(t, err)
require.Nil(t, ctrl2)
})
t.Run("start with canceled context", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // Cancel immediately
cl := httprc.NewClient()
ctrl, err := cl.Start(ctx)
require.NoError(t, err) // Start should succeed even with canceled context
require.NotNil(t, ctrl)
ctrl.Shutdown(time.Second)
})
}
func TestClientConcurrentStart(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cl := httprc.NewClient()
const numGoroutines = 10
var wg sync.WaitGroup
var mu sync.Mutex
var successCount, errorCount int
var successCtrl httprc.Controller
for range numGoroutines {
wg.Add(1)
go func() {
defer wg.Done()
ctrl, err := cl.Start(ctx)
mu.Lock()
defer mu.Unlock()
if err != nil {
errorCount++
} else {
successCount++
if successCtrl == nil {
successCtrl = ctrl
} else {
// If we somehow got multiple successes, clean up
ctrl.Shutdown(time.Second)
}
}
}()
}
wg.Wait()
// Exactly one should succeed, others should fail
require.Equal(t, 1, successCount, "exactly one start should succeed")
require.Equal(t, numGoroutines-1, errorCount, "all other starts should fail")
require.NotNil(t, successCtrl, "should have one successful controller")
successCtrl.Shutdown(time.Second)
}
func TestClientWithCustomSinks(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Create mock sinks to capture messages
var errorMessages []string
var traceMessages []string
var mu sync.Mutex
errorSink := errsink.NewFunc(func(_ context.Context, err error) {
mu.Lock()
defer mu.Unlock()
errorMessages = append(errorMessages, err.Error())
})
traceSink := tracesink.Func(func(_ context.Context, msg string) {
mu.Lock()
defer mu.Unlock()
traceMessages = append(traceMessages, msg)
})
cl := httprc.NewClient(
httprc.WithErrorSink(errorSink),
httprc.WithTraceSink(traceSink),
)
ctrl, err := cl.Start(ctx)
require.NoError(t, err)
t.Cleanup(func() { ctrl.Shutdown(time.Second) })
// Add a resource to generate some trace messages
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"test": "data"})
}))
defer srv.Close()
resource, err := httprc.NewResource[map[string]string](
srv.URL,
httprc.JSONTransformer[map[string]string](),
)
require.NoError(t, err, "custom sinks test resource creation should succeed")
require.NoError(t, ctrl.Add(ctx, resource), "adding custom sinks test resource should succeed")
// Wait a bit for traces to be generated
time.Sleep(100 * time.Millisecond)
mu.Lock()
defer mu.Unlock()
// Should have some trace messages
require.NotEmpty(t, traceMessages, "should have received trace messages")
// Error messages might be empty if no errors occurred, which is fine
// but we test that the sink was properly set up
}
func TestClientMultipleResources(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Create multiple test servers
srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"server": "1"})
}))
defer srv1.Close()
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"server": "2"})
}))
defer srv2.Close()
srv3 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"server": "3"})
}))
defer srv3.Close()
cl := httprc.NewClient(httprc.WithWorkers(3))
ctrl, err := cl.Start(ctx)
require.NoError(t, err)
t.Cleanup(func() { ctrl.Shutdown(time.Second) })
// Create multiple resources
resources := make([]httprc.Resource, 0, 3)
servers := []string{srv1.URL, srv2.URL, srv3.URL}
for i, serverURL := range servers {
resource, err := httprc.NewResource[map[string]string](
serverURL,
httprc.JSONTransformer[map[string]string](),
)
require.NoError(t, err, "creating resource %d", i)
resources = append(resources, resource)
require.NoError(t, ctrl.Add(ctx, resource), "adding resource %d", i)
}
// Verify all resources are working
for i, resource := range resources {
require.NoError(t, resource.Ready(ctx), "resource %d should be ready", i)
var data map[string]string
require.NoError(t, resource.Get(&data), "getting data from resource %d", i)
require.Equal(t, strconv.Itoa(i+1), data["server"], "resource %d should return correct server ID", i)
}
}