-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcoverage_micro_test.go
More file actions
566 lines (495 loc) · 16.1 KB
/
Copy pathcoverage_micro_test.go
File metadata and controls
566 lines (495 loc) · 16.1 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package main
import (
"bytes"
"context"
"net/http/httptest"
"sort"
"strings"
"testing"
"time"
"github.com/gofiber/fiber/v3"
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
"github.com/valyala/fasthttp"
)
// ---------------------------------------------------------------------------
// buffer_pool.go
// ---------------------------------------------------------------------------
func TestCoverageMicro_GzipWriterPool(t *testing.T) {
t.Run("GetGzipWriter returns non-nil", func(t *testing.T) {
var buf bytes.Buffer
gz := GetGzipWriter(&buf)
if gz == nil {
t.Fatal("expected non-nil gzip.Writer")
}
// Write something so Reset works correctly later
_, _ = gz.Write([]byte("hello"))
_ = gz.Flush()
PutGzipWriter(gz)
})
t.Run("Put then Get round-trip still usable", func(t *testing.T) {
var buf1 bytes.Buffer
gz := GetGzipWriter(&buf1)
if gz == nil {
t.Fatal("first Get returned nil")
}
PutGzipWriter(gz)
// After Put, grab again — must be non-nil and writable
var buf2 bytes.Buffer
gz2 := GetGzipWriter(&buf2)
if gz2 == nil {
t.Fatal("second Get after Put returned nil")
}
_, err := gz2.Write([]byte("world"))
if err != nil {
t.Fatalf("write after round-trip failed: %v", err)
}
_ = gz2.Close()
})
}
// ---------------------------------------------------------------------------
// circuit_breaker_metrics.go
// ---------------------------------------------------------------------------
func TestCoverageMicro_CircuitBreakerMetrics_GetState(t *testing.T) {
cbm := &CircuitBreakerMetrics{}
cbm.stateValue.Store(float64(0))
t.Run("initial value is zero", func(t *testing.T) {
if got := cbm.GetState(); got != 0.0 {
t.Fatalf("want 0.0, got %v", got)
}
})
t.Run("set then get returns correct value", func(t *testing.T) {
cbm.UpdateState(2.0)
if got := cbm.GetState(); got != 2.0 {
t.Fatalf("want 2.0, got %v", got)
}
})
t.Run("nil atomic value falls back to zero", func(t *testing.T) {
fresh := &CircuitBreakerMetrics{} // stateValue not initialised
// Load on unset atomic.Value returns nil
if got := fresh.GetState(); got != 0.0 {
t.Fatalf("want 0.0, got %v", got)
}
})
}
// ---------------------------------------------------------------------------
// errors.go
// ---------------------------------------------------------------------------
func TestCoverageMicro_TruncateString(t *testing.T) {
tests := []struct {
name string
input string
maxLen int
want string
}{
{"short string unchanged", "hi", 10, "hi"},
{"exact length unchanged", "hello", 5, "hello"},
{"longer than max gets truncated", "hello world", 5, "hello..."},
{"empty string", "", 5, ""},
{"max zero", "abc", 0, "..."},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := truncateString(tt.input, tt.maxLen)
if got != tt.want {
t.Fatalf("truncateString(%q, %d) = %q, want %q", tt.input, tt.maxLen, got, tt.want)
}
})
}
}
func TestCoverageMicro_IsRetryable(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{"nil error", nil, false},
{"retryable proxy error", NewProxyError(ErrCodeTimeout, "timeout", 503, true), true},
{"non-retryable proxy error", NewProxyError(ErrCodeUnauthorized, "unauth", 401, false), false},
{"plain error", &RateLimitConfigError{Paths: []string{"/tmp"}, PathErrors: map[string]string{"/tmp": "not found"}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsRetryable(tt.err); got != tt.want {
t.Fatalf("IsRetryable() = %v, want %v", got, tt.want)
}
})
}
}
func TestCoverageMicro_GetStatusCode(t *testing.T) {
tests := []struct {
name string
err error
want int
}{
{"nil error returns 200", nil, 200},
{"proxy error returns status code", NewProxyError(ErrCodeBadGateway, "bad gw", 502, false), 502},
{"non-proxy error returns 500", &RateLimitConfigError{Paths: []string{}, PathErrors: map[string]string{}}, 500},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetStatusCode(tt.err); got != tt.want {
t.Fatalf("GetStatusCode() = %d, want %d", got, tt.want)
}
})
}
}
// ---------------------------------------------------------------------------
// ratelimit_errors.go
// ---------------------------------------------------------------------------
func TestCoverageMicro_RateLimitConfigError_Error(t *testing.T) {
t.Run("contains paths in output", func(t *testing.T) {
paths := []string{"/etc/ratelimit.json", "/app/ratelimit.json"}
e := NewRateLimitConfigError(paths)
e.PathErrors["/etc/ratelimit.json"] = "permission denied"
e.PathErrors["/app/ratelimit.json"] = "file not found"
msg := e.Error()
if !strings.Contains(msg, "/etc/ratelimit.json") {
t.Error("expected path /etc/ratelimit.json in error message")
}
if !strings.Contains(msg, "permission denied") {
t.Error("expected error detail in message")
}
})
t.Run("empty paths produces valid string", func(t *testing.T) {
e := NewRateLimitConfigError(nil)
msg := e.Error()
if msg == "" {
t.Error("expected non-empty error message even with no paths")
}
})
}
// ---------------------------------------------------------------------------
// backend_health.go
// ---------------------------------------------------------------------------
func TestCoverageMicro_BackendHealth(t *testing.T) {
logger := libpack_logger.New()
client := &fasthttp.Client{}
t.Run("updateHealthStatus healthy→unhealthy transition", func(t *testing.T) {
bhm := NewBackendHealthManager(client, "http://localhost:9999", logger)
defer bhm.Shutdown()
// Start healthy
bhm.isHealthy.Store(true)
bhm.updateHealthStatus(false)
if bhm.IsHealthy() {
t.Error("expected unhealthy after updateHealthStatus(false)")
}
if bhm.GetConsecutiveFailures() != 1 {
t.Errorf("expected 1 consecutive failure, got %d", bhm.GetConsecutiveFailures())
}
})
t.Run("updateHealthStatus unhealthy→healthy resets counter", func(t *testing.T) {
bhm := NewBackendHealthManager(client, "http://localhost:9999", logger)
defer bhm.Shutdown()
bhm.isHealthy.Store(false)
bhm.consecutiveFails.Store(5)
bhm.updateHealthStatus(true)
if !bhm.IsHealthy() {
t.Error("expected healthy after updateHealthStatus(true)")
}
if bhm.GetConsecutiveFailures() != 0 {
t.Errorf("expected 0 failures after recovery, got %d", bhm.GetConsecutiveFailures())
}
})
t.Run("GetLastHealthCheck round-trip", func(t *testing.T) {
bhm := NewBackendHealthManager(client, "http://localhost:9999", logger)
defer bhm.Shutdown()
before := time.Now()
bhm.updateHealthStatus(true)
after := time.Now()
last := bhm.GetLastHealthCheck()
if last.Before(before) || last.After(after) {
t.Errorf("last health check time %v outside expected range [%v, %v]", last, before, after)
}
})
t.Run("nil receiver safe", func(t *testing.T) {
var nilBHM *BackendHealthManager
nilBHM.updateHealthStatus(true) // must not panic
if !nilBHM.GetLastHealthCheck().IsZero() {
t.Error("expected zero time for nil receiver")
}
})
}
// ---------------------------------------------------------------------------
// graphql.go — trackParsingAllocations
// ---------------------------------------------------------------------------
func TestCoverageMicro_TrackParsingAllocations(t *testing.T) {
t.Run("returned closure runs without panic", func(t *testing.T) {
done := trackParsingAllocations()
// Execute some allocations between start and stop
_ = make([]byte, 1024)
done() // must not panic regardless of cfg.Monitoring state
})
t.Run("closure safe when cfg.Monitoring is nil", func(t *testing.T) {
// Only manipulate cfg.Monitoring if cfg is already initialised
cfgMutex.RLock()
cfgInitialised := cfg != nil
cfgMutex.RUnlock()
if cfgInitialised {
cfgMutex.Lock()
origMonitoring := cfg.Monitoring
cfg.Monitoring = nil
cfgMutex.Unlock()
defer func() {
cfgMutex.Lock()
cfg.Monitoring = origMonitoring
cfgMutex.Unlock()
}()
}
done := trackParsingAllocations()
done() // must not panic regardless of monitoring state
})
}
// ---------------------------------------------------------------------------
// retry_budget.go — UpdateConfig
// ---------------------------------------------------------------------------
func TestCoverageMicro_RetryBudget_UpdateConfig(t *testing.T) {
t.Run("config fields applied", func(t *testing.T) {
initial := RetryBudgetConfig{TokensPerSecond: 5.0, MaxTokens: 50, Enabled: true}
rb := NewRetryBudget(initial, nil)
defer rb.Shutdown()
newCfg := RetryBudgetConfig{TokensPerSecond: 20.0, MaxTokens: 200, Enabled: false}
rb.UpdateConfig(newCfg)
if rb.tokensPerSecond != 20.0 {
t.Errorf("tokensPerSecond: want 20.0, got %v", rb.tokensPerSecond)
}
if rb.maxTokens != 200 {
t.Errorf("maxTokens: want 200, got %v", rb.maxTokens)
}
if rb.enabled {
t.Error("expected enabled=false after UpdateConfig")
}
// currentTokens should equal maxTokens after reset
if rb.currentTokens.Load() != 200 {
t.Errorf("currentTokens: want 200, got %v", rb.currentTokens.Load())
}
})
}
// ---------------------------------------------------------------------------
// rps_tracker.go
// ---------------------------------------------------------------------------
func TestCoverageMicro_RPSTracker(t *testing.T) {
t.Run("NewRPSTracker returns non-nil", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tracker := NewRPSTracker(ctx)
if tracker == nil {
t.Fatal("expected non-nil RPSTracker")
}
tracker.Shutdown()
})
t.Run("RecordRequest increments counter", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tracker := NewRPSTracker(ctx)
defer tracker.Shutdown()
for range 10 {
tracker.RecordRequest()
}
if tracker.lastCount.Load() != 10 {
t.Errorf("expected 10, got %d", tracker.lastCount.Load())
}
})
t.Run("GetCurrentRPS returns zero before first sample", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tracker := NewRPSTracker(ctx)
defer tracker.Shutdown()
rps := tracker.GetCurrentRPS()
if rps < 0 {
t.Errorf("RPS should not be negative, got %v", rps)
}
})
t.Run("sample calculates non-zero RPS after requests", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tracker := NewRPSTracker(ctx)
defer tracker.Shutdown()
// Record requests, then manually advance the sample time to simulate 1s elapsed
for range 50 {
tracker.RecordRequest()
}
// Set lastSampleTime to 1 second ago so elapsed > 0
tracker.lastSampleTime.Store(time.Now().Add(-1 * time.Second).UnixNano())
tracker.sample()
rps := tracker.GetCurrentRPS()
if rps <= 0 {
t.Errorf("expected RPS > 0 after sample with requests, got %v", rps)
}
})
t.Run("Shutdown stops gracefully", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tracker := NewRPSTracker(ctx)
// Should not block
done := make(chan struct{})
go func() {
tracker.Shutdown()
close(done)
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Error("Shutdown blocked for > 2s")
}
})
}
// ---------------------------------------------------------------------------
// metrics_aggregator.go — GetInstanceID, IsClusterMode (no Redis), GetInstanceHostname
// ---------------------------------------------------------------------------
func TestCoverageMicro_MetricsAggregatorGetters(t *testing.T) {
t.Run("GetInstanceID returns stored ID", func(t *testing.T) {
ma := &MetricsAggregator{instanceID: "test-instance-abc"}
if got := ma.GetInstanceID(); got != "test-instance-abc" {
t.Errorf("want test-instance-abc, got %q", got)
}
})
t.Run("GetInstanceHostname returns non-empty string", func(t *testing.T) {
host := GetInstanceHostname()
if host == "" {
t.Error("GetInstanceHostname returned empty string")
}
// Must not contain a dot (domain suffix stripped)
if strings.Contains(host, ".") {
t.Errorf("hostname should have domain stripped, got %q", host)
}
})
}
// ---------------------------------------------------------------------------
// websocket.go — IsWebSocketRequest
// ---------------------------------------------------------------------------
func TestCoverageMicro_IsWebSocketRequest(t *testing.T) {
tests := []struct {
name string
setHeaders func(*fasthttp.RequestHeader)
want bool
}{
{
name: "Upgrade websocket header set",
setHeaders: func(h *fasthttp.RequestHeader) {
h.Set("Upgrade", "websocket")
h.Set("Connection", "Upgrade")
},
want: true,
},
{
name: "no upgrade headers",
setHeaders: func(h *fasthttp.RequestHeader) {},
want: false,
},
{
name: "Connection Upgrade only",
setHeaders: func(h *fasthttp.RequestHeader) {
h.Set("Connection", "Upgrade")
},
want: true,
},
}
app := fiber.New()
app.Get("/ws-test", func(c fiber.Ctx) error {
result := IsWebSocketRequest(c)
if result {
return c.SendStatus(101)
}
return c.SendStatus(200)
})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/ws-test", nil)
tt.setHeaders(&fasthttp.RequestHeader{})
// Set headers on net/http request which fiber will read
switch tt.name {
case "Upgrade websocket header set":
req.Header.Set("Upgrade", "websocket")
req.Header.Set("Connection", "Upgrade")
case "Connection Upgrade only":
req.Header.Set("Connection", "Upgrade")
}
resp, err := app.Test(req, fiber.TestConfig{Timeout: 0})
if err != nil {
t.Fatalf("app.Test error: %v", err)
}
_ = resp.Body.Close()
wantCode := 200
if tt.want {
wantCode = 101
}
if resp.StatusCode != wantCode {
t.Errorf("status: want %d, got %d", wantCode, resp.StatusCode)
}
})
}
}
// ---------------------------------------------------------------------------
// admin_dashboard.go — getMapKeys
// ---------------------------------------------------------------------------
func TestCoverageMicro_GetMapKeys(t *testing.T) {
t.Run("nil map returns empty slice", func(t *testing.T) {
keys := getMapKeys(nil)
if len(keys) != 0 {
t.Errorf("expected empty slice for nil map, got %v", keys)
}
})
t.Run("empty map returns empty slice", func(t *testing.T) {
keys := getMapKeys(map[string]any{})
if len(keys) != 0 {
t.Errorf("expected empty slice, got %v", keys)
}
})
t.Run("populated map returns all keys", func(t *testing.T) {
m := map[string]any{"alpha": 1, "beta": 2, "gamma": 3}
keys := getMapKeys(m)
if len(keys) != 3 {
t.Fatalf("expected 3 keys, got %d: %v", len(keys), keys)
}
sort.Strings(keys)
want := []string{"alpha", "beta", "gamma"}
for i, k := range keys {
if k != want[i] {
t.Errorf("key[%d]: want %q, got %q", i, want[i], k)
}
}
})
}
// ---------------------------------------------------------------------------
// proxy.go — setupTracing (tracing disabled path)
// ---------------------------------------------------------------------------
func TestCoverageMicro_SetupTracing_Disabled(t *testing.T) {
t.Run("tracing disabled returns background context", func(t *testing.T) {
// Ensure cfg is initialised before reading it
cfgMutex.RLock()
needsInit := cfg == nil
cfgMutex.RUnlock()
if needsInit {
parseConfig()
}
// Ensure tracing is disabled
cfgMutex.Lock()
origEnable := cfg.Tracing.Enable
cfg.Tracing.Enable = false
cfgMutex.Unlock()
defer func() {
cfgMutex.Lock()
cfg.Tracing.Enable = origEnable
cfgMutex.Unlock()
}()
app := fiber.New()
var capturedCtx context.Context
app.Get("/trace-test", func(c fiber.Ctx) error {
capturedCtx = setupTracing(c)
return c.SendStatus(200)
})
req := httptest.NewRequest("GET", "/trace-test", nil)
resp, err := app.Test(req, fiber.TestConfig{Timeout: 0})
if err != nil {
t.Fatalf("app.Test error: %v", err)
}
_ = resp.Body.Close()
if capturedCtx == nil {
t.Fatal("setupTracing returned nil context")
}
// Background context has no deadline
if _, hasDeadline := capturedCtx.Deadline(); hasDeadline {
t.Error("expected no deadline on returned context")
}
})
}