mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-18 02:52:36 +03:00
* chore(lint): ban context.Background in tests, flag unused nolints Add a forbidigo rule that rejects context.Background and context.TODO in _test.go files. The lint message points to the right fix: the spec's ctx from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and benchmarks. AGENTS.md already asks for this, but nothing enforced it, so new tests kept adding Background calls. All 465 existing calls are replaced, with one ctx per top-level Describe set at the top of its BeforeEach. The migration forbidigo rules keep their old scope through an exclusion matched on their message text. Also enable nolintlint with require-specific and allow-unused: false. It found 41 //nolint directives that suppressed nothing, or named no linter, or had a leading space that made them inert. They are removed or fixed. The sweep also fixed a middleware test that wrote a user with a nil ctx before ctx was set. * test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx The ROLLBACK cleanups in the library, plugin and initial setup tests must run even if the spec context is already cancelled, so they use context.WithoutCancel. The same goes for the context handed to the process-wide scanner singleton in the GetInstance test. Also document that the forbidigo exclusions are scoped by message text. * test: pass the caller's ctx to tests.ClearDB ClearDB was the last context.Background call in shared test code. It now takes a ctx, and every caller passes its spec ctx. The forbidigo rule for tests also covers the tests/ helper package now, so new helpers there cannot add Background calls either.
171 lines
3.8 KiB
Go
171 lines
3.8 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
)
|
|
|
|
type benchItem struct {
|
|
key string
|
|
}
|
|
|
|
func (b *benchItem) Key() string { return b.key }
|
|
|
|
// setupBenchCache creates a file cache in a temp directory. Returns the cache and cleanup function.
|
|
func setupBenchCache(b *testing.B, cacheSize string, getReader ReadFunc) (*fileCache, func()) {
|
|
b.Helper()
|
|
tmpDir, err := os.MkdirTemp("", "bench-cache-*")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
b.Cleanup(conf.SnapshotConfig())
|
|
conf.Update(func(c *conf.Options) { c.CacheFolder = conf.NewDir(tmpDir) })
|
|
|
|
fc := NewFileCache("bench", cacheSize, "bench", 0, getReader).(*fileCache)
|
|
|
|
// Wait for cache to be ready
|
|
for !fc.ready.Load() {
|
|
runtime.Gosched() // Yield to allow background init goroutine to run
|
|
}
|
|
|
|
teardown := func() {
|
|
os.RemoveAll(tmpDir)
|
|
}
|
|
return fc, teardown
|
|
}
|
|
|
|
func BenchmarkCacheWrite(b *testing.B) {
|
|
// Simulate writing 50KB images (typical 300px JPEG)
|
|
imageData := strings.Repeat("x", 50*1024)
|
|
|
|
fc, cleanup := setupBenchCache(b, "100MB", func(ctx context.Context, item Item) (io.Reader, error) {
|
|
return strings.NewReader(imageData), nil
|
|
})
|
|
defer cleanup()
|
|
|
|
b.SetBytes(int64(len(imageData)))
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
key := fmt.Sprintf("write-bench-%d", i)
|
|
s, err := fc.Get(b.Context(), &benchItem{key: key})
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
_, _ = io.ReadAll(s)
|
|
s.Close()
|
|
}
|
|
}
|
|
|
|
func BenchmarkCacheRead(b *testing.B) {
|
|
imageData := strings.Repeat("x", 50*1024)
|
|
|
|
fc, cleanup := setupBenchCache(b, "100MB", func(ctx context.Context, item Item) (io.Reader, error) {
|
|
return strings.NewReader(imageData), nil
|
|
})
|
|
defer cleanup()
|
|
|
|
// Pre-populate cache
|
|
item := &benchItem{key: "read-bench"}
|
|
s, err := fc.Get(b.Context(), item)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
_, _ = io.ReadAll(s)
|
|
s.Close()
|
|
|
|
b.SetBytes(int64(len(imageData)))
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
s, err := fc.Get(b.Context(), item)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
_, _ = io.ReadAll(s)
|
|
s.Close()
|
|
}
|
|
}
|
|
|
|
func BenchmarkConcurrentCacheRead(b *testing.B) {
|
|
imageData := strings.Repeat("x", 50*1024)
|
|
|
|
fc, cleanup := setupBenchCache(b, "100MB", func(ctx context.Context, item Item) (io.Reader, error) {
|
|
return strings.NewReader(imageData), nil
|
|
})
|
|
defer cleanup()
|
|
|
|
// Pre-populate cache
|
|
item := &benchItem{key: "concurrent-read"}
|
|
s, _ := fc.Get(b.Context(), item)
|
|
_, _ = io.ReadAll(s)
|
|
s.Close()
|
|
|
|
concurrencyLevels := []int{1, 10, 50}
|
|
for _, n := range concurrencyLevels {
|
|
b.Run(fmt.Sprintf("goroutines_%d", n), func(b *testing.B) {
|
|
b.SetBytes(int64(len(imageData)))
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
var wg sync.WaitGroup
|
|
wg.Add(n)
|
|
for range n {
|
|
go func() {
|
|
defer wg.Done()
|
|
s, err := fc.Get(b.Context(), item)
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
_, _ = io.ReadAll(s)
|
|
s.Close()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkConcurrentCacheMiss(b *testing.B) {
|
|
imageData := strings.Repeat("x", 50*1024)
|
|
|
|
concurrencyLevels := []int{1, 10, 50}
|
|
for _, n := range concurrencyLevels {
|
|
b.Run(fmt.Sprintf("goroutines_%d", n), func(b *testing.B) {
|
|
fc, cleanup := setupBenchCache(b, "100MB", func(ctx context.Context, item Item) (io.Reader, error) {
|
|
return strings.NewReader(imageData), nil
|
|
})
|
|
defer cleanup()
|
|
|
|
b.SetBytes(int64(len(imageData)))
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
var wg sync.WaitGroup
|
|
wg.Add(n)
|
|
// All goroutines request the SAME key (not yet cached)
|
|
item := &benchItem{key: fmt.Sprintf("miss-%d", i)}
|
|
for range n {
|
|
go func() {
|
|
defer wg.Done()
|
|
s, err := fc.Get(b.Context(), item)
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
_, _ = io.ReadAll(s)
|
|
s.Close()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|
|
})
|
|
}
|
|
}
|