package imgcache import ( "testing" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil" ) // counterValues holds the values of the counters in counters.go. type counterValues struct { hitCount int64 missCount int64 upstreamFetchCount int64 upstreamFetchBytes int64 transformCount int64 } // readCounters reads the counters in counters.go. They count for the whole // process, and every test that serves an image adds to them, so a test that // checks how much they rose does not call t.Parallel. Go runs the tests that // do not call it one at a time, and the tests that do only after them. func readCounters() counterValues { return counterValues{ hitCount: int64(testutil.ToFloat64(cacheHits)), missCount: int64(testutil.ToFloat64(cacheMisses)), upstreamFetchCount: int64(testutil.ToFloat64(upstreamFetches)), upstreamFetchBytes: int64(testutil.ToFloat64(upstreamFetchBytes)), transformCount: int64(testutil.ToFloat64(transforms)), } } // minus returns how much each counter rose from before to v. func (v counterValues) minus(before counterValues) counterValues { return counterValues{ hitCount: v.hitCount - before.hitCount, missCount: v.missCount - before.missCount, upstreamFetchCount: v.upstreamFetchCount - before.upstreamFetchCount, upstreamFetchBytes: v.upstreamFetchBytes - before.upstreamFetchBytes, transformCount: v.transformCount - before.transformCount, } } // TestCounters_AreOnTheRegistryMetricsServes checks that the counters in // counters.go are on Prometheus' default registry, which /metrics serves, // under the names README.md gives them. func TestCounters_AreOnTheRegistryMetricsServes(t *testing.T) { t.Parallel() names := []string{ "pixa_cache_hits_total", "pixa_cache_misses_total", "pixa_upstream_fetches_total", "pixa_upstream_fetch_bytes_total", "pixa_transforms_total", } count, err := testutil.GatherAndCount(prometheus.DefaultGatherer, 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 default registry, want %d", count, names, len(names)) } }