Files
gohoarder/pkg/scanner/health_test.go
T
lukaszraczylo e6d8ed0f2e fix(scanner): reuse a recent scanner health result
Each /health request ran the version command of every scanner. Under rescan load one round took about 1.4 seconds, which exceeds the client probes that use /health to choose the cache. The result is now reused for 30 seconds. A probe that ends with a cancelled context is not stored.
2026-11-23 19:47:16 +00:00

69 lines
1.8 KiB
Go

package scanner
import (
"context"
"errors"
"sync/atomic"
"testing"
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
)
type healthProbe struct {
err error
calls atomic.Int32
}
func (s *healthProbe) Name() string { return "counting" }
func (s *healthProbe) Scan(context.Context, string, string, string, string) (*metadata.ScanResult, error) {
return nil, nil
}
func (s *healthProbe) Health(context.Context) error {
s.calls.Add(1)
return s.err
}
// Health runs each scanner's version command; under scan load that took over a second, so
// repeated probes must reuse a recent result.
func TestHealthReusesRecentResult(t *testing.T) {
tests := []struct {
err error
name string
}{
{name: "healthy", err: nil},
{name: "unhealthy", err: errors.New("grype missing")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &healthProbe{err: tt.err}
m := &Manager{enabled: true, scanners: []Scanner{s}}
for range 3 {
if err := m.Health(context.Background()); (err != nil) != (tt.err != nil) {
t.Fatalf("Health() error = %v, want error %v", err, tt.err != nil)
}
}
if got := s.calls.Load(); got != 1 {
t.Fatalf("scanner health ran %d times, want 1", got)
}
})
}
}
// A probe whose context was cancelled must not pin its failure for the cache period.
func TestHealthDoesNotCacheCancelledProbe(t *testing.T) {
s := &healthProbe{err: context.Canceled}
m := &Manager{enabled: true, scanners: []Scanner{s}}
ctx, cancel := context.WithCancel(context.Background())
cancel()
_ = m.Health(ctx)
s.err = nil
if err := m.Health(context.Background()); err != nil {
t.Fatalf("Health() after cancelled probe = %v, want nil", err)
}
if got := s.calls.Load(); got != 2 {
t.Fatalf("scanner health ran %d times, want 2", got)
}
}