Skip to content

Commit 7f5e7f5

Browse files
Thomas StrombergThomas Stromberg
authored andcommitted
lint
1 parent fbf1e99 commit 7f5e7f5

File tree

4 files changed

+51
-52
lines changed

4 files changed

+51
-52
lines changed

cache_persist_test.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func (m *mockPersister[K, V]) Delete(ctx context.Context, key K) error {
8686
return nil
8787
}
8888

89+
//nolint:gocritic // Channel returns are clearer without named results
8990
func (m *mockPersister[K, V]) LoadRecent(ctx context.Context, limit int) (<-chan Entry[K, V], <-chan error) {
9091
entryCh := make(chan Entry[K, V], 10)
9192
errCh := make(chan error, 1)
@@ -166,7 +167,7 @@ func TestCache_WithPersistence(t *testing.T) {
166167
if err != nil {
167168
t.Fatalf("New: %v", err)
168169
}
169-
defer func() { _ = cache.Close() }()
170+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
170171

171172
// Set should persist
172173
if err := cache.Set(ctx, "key1", 42, 0); err != nil {
@@ -202,13 +203,13 @@ func TestCache_GetFromPersistence(t *testing.T) {
202203
persister := newMockPersister[string, int]()
203204

204205
// Pre-populate persistence
205-
_ = persister.Store(ctx, "key1", 42, time.Time{})
206+
_ = persister.Store(ctx, "key1", 42, time.Time{}) //nolint:errcheck // Test fixture
206207

207208
cache, err := New[string, int](ctx, WithPersistence(persister))
208209
if err != nil {
209210
t.Fatalf("New: %v", err)
210211
}
211-
defer func() { _ = cache.Close() }()
212+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
212213

213214
// Get should load from persistence
214215
val, found, err := cache.Get(ctx, "key1")
@@ -228,13 +229,13 @@ func TestCache_GetFromPersistenceExpired(t *testing.T) {
228229
persister := newMockPersister[string, int]()
229230

230231
// Pre-populate with expired entry
231-
_ = persister.Store(ctx, "key1", 42, time.Now().Add(-1*time.Hour))
232+
_ = persister.Store(ctx, "key1", 42, time.Now().Add(-1*time.Hour)) //nolint:errcheck // Test fixture
232233

233234
cache, err := New[string, int](ctx, WithPersistence(persister))
234235
if err != nil {
235236
t.Fatalf("New: %v", err)
236237
}
237-
defer func() { _ = cache.Close() }()
238+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
238239

239240
// Get should return not found for expired entry
240241
_, found, err := cache.Get(ctx, "key1")
@@ -252,7 +253,7 @@ func TestCache_WithWarmup(t *testing.T) {
252253

253254
// Pre-populate persistence with 10 items
254255
for i := range 10 {
255-
_ = persister.Store(ctx, fmt.Sprintf("key%d", i), i, time.Time{})
256+
_ = persister.Store(ctx, fmt.Sprintf("key%d", i), i, time.Time{}) //nolint:errcheck // Test fixture
256257
}
257258

258259
// Create cache with warmup limit of 5
@@ -262,7 +263,7 @@ func TestCache_WithWarmup(t *testing.T) {
262263
if err != nil {
263264
t.Fatalf("New: %v", err)
264265
}
265-
defer func() { _ = cache.Close() }()
266+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
266267

267268
// Give warmup goroutine time to complete
268269
time.Sleep(100 * time.Millisecond)
@@ -282,9 +283,9 @@ func TestCache_WithCleanupOnStartup(t *testing.T) {
282283

283284
// Pre-populate with expired entries
284285
past := time.Now().Add(-2 * time.Hour)
285-
_ = persister.Store(ctx, "expired1", 1, past)
286-
_ = persister.Store(ctx, "expired2", 2, past)
287-
_ = persister.Store(ctx, "valid", 3, time.Time{})
286+
_ = persister.Store(ctx, "expired1", 1, past) //nolint:errcheck // Test fixture
287+
_ = persister.Store(ctx, "expired2", 2, past) //nolint:errcheck // Test fixture
288+
_ = persister.Store(ctx, "valid", 3, time.Time{}) //nolint:errcheck // Test fixture
288289

289290
// Create cache with cleanup
290291
cache, err := New[string, int](ctx,
@@ -293,7 +294,7 @@ func TestCache_WithCleanupOnStartup(t *testing.T) {
293294
if err != nil {
294295
t.Fatalf("New: %v", err)
295296
}
296-
defer func() { _ = cache.Close() }()
297+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
297298

298299
// Expired entries should be cleaned up
299300
_, _, found, err := persister.Load(ctx, "expired1")
@@ -322,7 +323,7 @@ func TestCache_SetAsyncWithPersistence(t *testing.T) {
322323
if err != nil {
323324
t.Fatalf("New: %v", err)
324325
}
325-
defer func() { _ = cache.Close() }()
326+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
326327

327328
// SetAsync should not block but value should be available immediately
328329
if err := cache.SetAsync(ctx, "key1", 42, 0); err != nil {
@@ -378,7 +379,7 @@ func TestCache_PersistenceErrors(t *testing.T) {
378379
if err != nil {
379380
t.Fatalf("New: %v", err)
380381
}
381-
defer func() { _ = cache.Close() }()
382+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
382383

383384
// Set returns error when persistence fails (by design)
384385
// Value is still in memory, but error is returned to caller
@@ -434,7 +435,7 @@ func TestCache_Delete_Errors(t *testing.T) {
434435
if err != nil {
435436
t.Fatalf("New: %v", err)
436437
}
437-
defer func() { _ = cache.Close() }()
438+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
438439

439440
// Store a value (with failSet = false)
440441
persister.failSet = false
@@ -483,7 +484,7 @@ func TestCache_Get_InvalidKey(t *testing.T) {
483484
if err != nil {
484485
t.Fatalf("New: %v", err)
485486
}
486-
defer func() { _ = cache.Close() }()
487+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
487488

488489
// Get with empty key (invalid)
489490
_, found, err := cache.Get(ctx, "")
@@ -503,10 +504,10 @@ func TestCache_Get_PersistenceLoadError(t *testing.T) {
503504
if err != nil {
504505
t.Fatalf("New: %v", err)
505506
}
506-
defer func() { _ = cache.Close() }()
507+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
507508

508509
// Pre-populate persistence (not in memory)
509-
_ = persister.Store(ctx, "key1", 42, time.Time{})
510+
_ = persister.Store(ctx, "key1", 42, time.Time{}) //nolint:errcheck // Test fixture
510511

511512
// Make persistence Load fail
512513
persister.failGet = true
@@ -548,7 +549,7 @@ func TestCache_GhostQueue(t *testing.T) {
548549
if err != nil {
549550
t.Fatalf("New: %v", err)
550551
}
551-
defer func() { _ = cache.Close() }()
552+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
552553

553554
// Fill small queue (10% of 10 = 1)
554555
// Insert items to trigger ghost queue
@@ -560,7 +561,7 @@ func TestCache_GhostQueue(t *testing.T) {
560561

561562
// Access some items to create hot items
562563
for i := range 5 {
563-
_, _, _ = cache.Get(ctx, fmt.Sprintf("key%d", i))
564+
_, _, _ = cache.Get(ctx, fmt.Sprintf("key%d", i)) //nolint:errcheck // Exercising code path
564565
}
565566

566567
// Insert more to trigger evictions from small queue to ghost queue
@@ -582,7 +583,7 @@ func TestCache_MainQueueEviction(t *testing.T) {
582583
if err != nil {
583584
t.Fatalf("New: %v", err)
584585
}
585-
defer func() { _ = cache.Close() }()
586+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
586587

587588
// Insert and access items to get them into Main queue
588589
for i := range 15 {
@@ -591,7 +592,7 @@ func TestCache_MainQueueEviction(t *testing.T) {
591592
t.Fatalf("Set: %v", err)
592593
}
593594
// Access to promote to Main
594-
_, _, _ = cache.Get(ctx, key)
595+
_, _, _ = cache.Get(ctx, key) //nolint:errcheck // Exercising code path
595596
}
596597

597598
// Insert more items to trigger eviction from Main queue
@@ -600,7 +601,7 @@ func TestCache_MainQueueEviction(t *testing.T) {
600601
if err := cache.Set(ctx, key, i+15, 0); err != nil {
601602
t.Fatalf("Set: %v", err)
602603
}
603-
_, _, _ = cache.Get(ctx, key)
604+
_, _, _ = cache.Get(ctx, key) //nolint:errcheck // Exercising code path
604605
}
605606

606607
// Verify cache is at capacity
@@ -616,13 +617,13 @@ func TestCache_CleanupExpiredEntries(t *testing.T) {
616617
if err != nil {
617618
t.Fatalf("New: %v", err)
618619
}
619-
defer func() { _ = cache.Close() }()
620+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
620621

621622
// Store items with expiry
622623
past := time.Now().Add(-1 * time.Hour)
623-
_ = cache.Set(ctx, "expired1", 1, -1*time.Hour) // Already expired
624-
_ = cache.Set(ctx, "expired2", 2, -1*time.Hour)
625-
_ = cache.Set(ctx, "valid", 3, 1*time.Hour)
624+
_ = cache.Set(ctx, "expired1", 1, -1*time.Hour) //nolint:errcheck // Test fixture
625+
_ = cache.Set(ctx, "expired2", 2, -1*time.Hour) //nolint:errcheck // Test fixture
626+
_ = cache.Set(ctx, "valid", 3, 1*time.Hour) //nolint:errcheck // Test fixture
626627

627628
// Manually set expiry to past for testing
628629
if err := cache.Set(ctx, "test-expired", 99, 0); err != nil {

cache_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ func TestCache_WithOptions(t *testing.T) {
481481
if cache.opts.MemorySize != 500 {
482482
t.Errorf("memory size = %d; want 500", cache.opts.MemorySize)
483483
}
484-
_ = cache.Close()
484+
_ = cache.Close() //nolint:errcheck // Test cleanup
485485

486486
// Test WithDefaultTTL
487487
cache, err = New[string, int](ctx, WithDefaultTTL(5*time.Minute))
@@ -491,7 +491,7 @@ func TestCache_WithOptions(t *testing.T) {
491491
if cache.opts.DefaultTTL != 5*time.Minute {
492492
t.Errorf("default TTL = %v; want 5m", cache.opts.DefaultTTL)
493493
}
494-
_ = cache.Close()
494+
_ = cache.Close() //nolint:errcheck // Test cleanup
495495

496496
// Test WithWarmup
497497
cache, err = New[string, int](ctx, WithWarmup(100))
@@ -501,7 +501,7 @@ func TestCache_WithOptions(t *testing.T) {
501501
if cache.opts.WarmupLimit != 100 {
502502
t.Errorf("warmup limit = %d; want 100", cache.opts.WarmupLimit)
503503
}
504-
_ = cache.Close()
504+
_ = cache.Close() //nolint:errcheck // Test cleanup
505505

506506
// Test WithCleanup
507507
cache, err = New[string, int](ctx, WithCleanup(1*time.Hour))
@@ -514,7 +514,7 @@ func TestCache_WithOptions(t *testing.T) {
514514
if cache.opts.CleanupMaxAge != 1*time.Hour {
515515
t.Errorf("cleanup max age = %v; want 1h", cache.opts.CleanupMaxAge)
516516
}
517-
_ = cache.Close()
517+
_ = cache.Close() //nolint:errcheck // Test cleanup
518518
}
519519

520520
func TestCache_DeleteNonExistent(t *testing.T) {
@@ -523,7 +523,7 @@ func TestCache_DeleteNonExistent(t *testing.T) {
523523
if err != nil {
524524
t.Fatalf("New: %v", err)
525525
}
526-
defer func() { _ = cache.Close() }()
526+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
527527

528528
// Delete non-existent key should not error
529529
cache.Delete(ctx, "does-not-exist")
@@ -548,19 +548,19 @@ func TestCache_EvictFromMain(t *testing.T) {
548548
if err != nil {
549549
t.Fatalf("New: %v", err)
550550
}
551-
defer func() { _ = cache.Close() }()
551+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
552552

553553
// Fill small queue and promote items to main by accessing them twice
554554
for i := range 15 {
555-
_ = cache.Set(ctx, i, i, 0)
555+
_ = cache.Set(ctx, i, i, 0) //nolint:errcheck // Test fixture
556556
// Access immediately to promote to main
557-
_, _, _ = cache.Get(ctx, i)
557+
_, _, _ = cache.Get(ctx, i) //nolint:errcheck // Exercising code path
558558
}
559559

560560
// Add more items to force eviction from main queue
561561
for i := range 10 {
562-
_ = cache.Set(ctx, i+100, i+100, 0)
563-
_, _, _ = cache.Get(ctx, i+100)
562+
_ = cache.Set(ctx, i+100, i+100, 0) //nolint:errcheck // Test fixture
563+
_, _, _ = cache.Get(ctx, i+100) //nolint:errcheck // Exercising code path
564564
}
565565

566566
// Cache should not exceed capacity
@@ -575,7 +575,7 @@ func TestCache_GetExpired(t *testing.T) {
575575
if err != nil {
576576
t.Fatalf("New: %v", err)
577577
}
578-
defer func() { _ = cache.Close() }()
578+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
579579

580580
// Set with very short TTL
581581
if err := cache.Set(ctx, "key1", 42, 1*time.Millisecond); err != nil {
@@ -601,7 +601,7 @@ func TestCache_SetUpdateExisting(t *testing.T) {
601601
if err != nil {
602602
t.Fatalf("New: %v", err)
603603
}
604-
defer func() { _ = cache.Close() }()
604+
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
605605

606606
// Set initial value
607607
if err := cache.Set(ctx, "key1", 42, 0); err != nil {

persist/cloudrun/cloudrun_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ func TestNew_LocalFallback(t *testing.T) {
1313

1414
// Ensure K_SERVICE is not set
1515
oldVal := os.Getenv("K_SERVICE")
16-
_ = os.Unsetenv("K_SERVICE")
16+
_ = os.Unsetenv("K_SERVICE") //nolint:errcheck // Test setup
1717
defer func() {
1818
if oldVal != "" {
19-
_ = os.Setenv("K_SERVICE", oldVal)
19+
_ = os.Setenv("K_SERVICE", oldVal) //nolint:errcheck,usetesting // Test cleanup
2020
}
2121
}()
2222

@@ -42,12 +42,12 @@ func TestNew_CloudRunWithoutDatastore(t *testing.T) {
4242

4343
// Set K_SERVICE to simulate Cloud Run
4444
oldVal := os.Getenv("K_SERVICE")
45-
_ = os.Setenv("K_SERVICE", "test-service")
45+
_ = os.Setenv("K_SERVICE", "test-service") //nolint:errcheck,usetesting // Test setup
4646
defer func() {
4747
if oldVal != "" {
48-
_ = os.Setenv("K_SERVICE", oldVal)
48+
_ = os.Setenv("K_SERVICE", oldVal) //nolint:errcheck,usetesting // Test cleanup
4949
} else {
50-
_ = os.Unsetenv("K_SERVICE")
50+
_ = os.Unsetenv("K_SERVICE") //nolint:errcheck // Test setup
5151
}
5252
}()
5353

@@ -108,10 +108,10 @@ func TestNew_DetectsCloudRun(t *testing.T) {
108108

109109
// Unset K_SERVICE to test non-Cloud Run path
110110
oldVal := os.Getenv("K_SERVICE")
111-
_ = os.Unsetenv("K_SERVICE")
111+
_ = os.Unsetenv("K_SERVICE") //nolint:errcheck // Test setup
112112
defer func() {
113113
if oldVal != "" {
114-
_ = os.Setenv("K_SERVICE", oldVal)
114+
_ = os.Setenv("K_SERVICE", oldVal) //nolint:errcheck,usetesting // Test cleanup
115115
}
116116
}()
117117

@@ -221,12 +221,12 @@ func TestNew_CloudRunFallbackWithDelete(t *testing.T) {
221221

222222
// Set K_SERVICE to simulate Cloud Run
223223
oldVal := os.Getenv("K_SERVICE")
224-
_ = os.Setenv("K_SERVICE", "test-service-delete")
224+
_ = os.Setenv("K_SERVICE", "test-service-delete") //nolint:errcheck,usetesting // Test setup
225225
defer func() {
226226
if oldVal != "" {
227-
_ = os.Setenv("K_SERVICE", oldVal)
227+
_ = os.Setenv("K_SERVICE", oldVal) //nolint:errcheck,usetesting // Test cleanup
228228
} else {
229-
_ = os.Unsetenv("K_SERVICE")
229+
_ = os.Unsetenv("K_SERVICE") //nolint:errcheck // Test setup
230230
}
231231
}()
232232

persist/localfs/integration_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func TestFilePersist_ValidateKey(t *testing.T) {
381381
{"key at max length", string(validMaxKey), false},
382382
{"key too long", string(make([]byte, 128)), true},
383383
{"key with space", "my key", true},
384-
{"key with unicode", "key-日本語", true},
384+
{"key with unicode", "key-日本語", true}, //nolint:gosmopolitan // Testing unicode handling
385385
{"key with slash", "key/123", true},
386386
}
387387

@@ -722,9 +722,7 @@ func TestFilePersist_New_UseDefaultCacheDir(t *testing.T) {
722722
t.Logf("Close error: %v", err)
723723
}
724724
// Clean up the test directory from OS cache dir
725-
if err := os.RemoveAll(fp.(*persister[string, int]).Dir); err != nil {
726-
t.Logf("RemoveAll error: %v", err)
727-
}
725+
_ = os.RemoveAll(fp.(*persister[string, int]).Dir) //nolint:errcheck // Test cleanup
728726
}()
729727

730728
ctx := context.Background()

0 commit comments

Comments
 (0)