package imgcache import ( "errors" "testing" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil" ) // TestNewCache_RegistersItsCountersOnItsRegistry checks that NewCache // registers the cache's counters on the registry it is given, under the // names README.md gives them. func TestNewCache_RegistersItsCountersOnItsRegistry(t *testing.T) { t.Parallel() registry := prometheus.NewRegistry() _, err := NewCache(evictionTestDB(t), Config{StateDir: t.TempDir(), MaxBytes: 1 << 30}, registry) if err != nil { t.Fatalf("NewCache() error = %v", err) } names := []string{ "pixa_cache_hits_total", "pixa_cache_misses_total", } count, err := testutil.GatherAndCount(registry, names...) if err != nil { t.Fatalf("testutil.GatherAndCount() error = %v", err) } if count != len(names) { t.Errorf("%d of the counters %v are on the registry, want %d", count, names, len(names)) } } // TestNewCache_FailsWhenItsCountersCannotBeRegistered makes a second cache // on the registry of a first. Its counters have the same names, so the // registry refuses them, and NewCache must return that error. func TestNewCache_FailsWhenItsCountersCannotBeRegistered(t *testing.T) { t.Parallel() registry := prometheus.NewRegistry() _, err := NewCache(evictionTestDB(t), Config{StateDir: t.TempDir(), MaxBytes: 1 << 30}, registry) if err != nil { t.Fatalf("NewCache() of the first cache error = %v", err) } _, err = NewCache(evictionTestDB(t), Config{StateDir: t.TempDir(), MaxBytes: 1 << 30}, registry) t.Logf("NewCache() of the second cache error = %v", err) var alreadyRegistered prometheus.AlreadyRegisteredError if !errors.As(err, &alreadyRegistered) { t.Errorf("NewCache() of the second cache error = %v, want %T", err, alreadyRegistered) } }