Skip to content

Commit

Permalink
Add list of existing stats to assertion not-found failures (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
haltwise authored Sep 21, 2020
1 parent d74c79b commit 503d0e4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
42 changes: 33 additions & 9 deletions mock/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ func (s *Sink) LoadTimer(name string) (float64, bool) {
return 0, false
}

// ListCounters returns a list of existing counter names.
func (s *Sink) ListCounters() []string {
return keys(s.counters())
}

// ListGauges returns a list of existing gauge names.
func (s *Sink) ListGauges() []string {
return keys(s.gauges())
}

// ListTimers returns a list of existing timer names.
func (s *Sink) ListTimers() []string {
return keys(s.timers())
}

// Note, this may return an incoherent snapshot if contents is being concurrently modified
func keys(m *sync.Map) (a []string) {
m.Range(func(key interface{}, _ interface{}) bool {
a = append(a, key.(string))
return true
})
return a
}

// Counters returns all the counters currently stored by the sink.
func (s *Sink) Counters() map[string]uint64 {
m := make(map[string]uint64)
Expand Down Expand Up @@ -224,7 +248,7 @@ func (s *Sink) AssertCounterEquals(tb testing.TB, name string, exp uint64) {
tb.Helper()
u, ok := s.LoadCounter(name)
if !ok {
tb.Errorf("gostats/mock: Counter (%q): not found", name)
tb.Errorf("gostats/mock: Counter (%q): not found in: %q", name, s.ListCounters())
return
}
if u != exp {
Expand All @@ -237,7 +261,7 @@ func (s *Sink) AssertGaugeEquals(tb testing.TB, name string, exp uint64) {
tb.Helper()
u, ok := s.LoadGauge(name)
if !ok {
tb.Errorf("gostats/mock: Gauge (%q): not found", name)
tb.Errorf("gostats/mock: Gauge (%q): not found in: %q", name, s.ListGauges())
return
}
if u != exp {
Expand All @@ -250,7 +274,7 @@ func (s *Sink) AssertTimerEquals(tb testing.TB, name string, exp float64) {
tb.Helper()
f, ok := s.LoadTimer(name)
if !ok {
tb.Errorf("gostats/mock: Timer (%q): not found", name)
tb.Errorf("gostats/mock: Timer (%q): not found in: %q", name, s.ListTimers())
return
}
if f != exp {
Expand All @@ -262,23 +286,23 @@ func (s *Sink) AssertTimerEquals(tb testing.TB, name string, exp float64) {
func (s *Sink) AssertCounterExists(tb testing.TB, name string) {
tb.Helper()
if _, ok := s.LoadCounter(name); !ok {
tb.Errorf("gostats/mock: Counter (%q): not found", name)
tb.Errorf("gostats/mock: Counter (%q): not found in: %q", name, s.ListCounters())
}
}

// AssertGaugeExists asserts that Gauge name exists.
func (s *Sink) AssertGaugeExists(tb testing.TB, name string) {
tb.Helper()
if _, ok := s.LoadGauge(name); !ok {
tb.Errorf("gostats/mock: Gauge (%q): not found", name)
tb.Errorf("gostats/mock: Gauge (%q): not found in: %q", name, s.ListGauges())
}
}

// AssertTimerExists asserts that Timer name exists.
func (s *Sink) AssertTimerExists(tb testing.TB, name string) {
tb.Helper()
if _, ok := s.LoadTimer(name); !ok {
tb.Errorf("gostats/mock: Timer (%q): not found", name)
tb.Errorf("gostats/mock: Timer (%q): not found in: %q", name, s.ListTimers())
}
}

Expand Down Expand Up @@ -311,7 +335,7 @@ func (s *Sink) AssertCounterCallCount(tb testing.TB, name string, exp int) {
tb.Helper()
v, ok := s.counters().Load(name)
if !ok {
tb.Errorf("gostats/mock: Counter (%q): not found", name)
tb.Errorf("gostats/mock: Counter (%q): not found in: %q", name, s.ListCounters())
return
}
p := v.(*entry)
Expand All @@ -327,7 +351,7 @@ func (s *Sink) AssertGaugeCallCount(tb testing.TB, name string, exp int) {
tb.Helper()
v, ok := s.gauges().Load(name)
if !ok {
tb.Errorf("gostats/mock: Gauge (%q): not found", name)
tb.Errorf("gostats/mock: Gauge (%q): not found in: %q", name, s.ListGauges())
return
}
p := v.(*entry)
Expand All @@ -343,7 +367,7 @@ func (s *Sink) AssertTimerCallCount(tb testing.TB, name string, exp int) {
tb.Helper()
v, ok := s.timers().Load(name)
if !ok {
tb.Errorf("gostats/mock: Timer (%q): not found", name)
tb.Errorf("gostats/mock: Timer (%q): not found in: %q", name, s.ListTimers())
return
}
p := v.(*entry)
Expand Down
6 changes: 3 additions & 3 deletions mock/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestSink(t *testing.T) {
func(t testing.TB) { sink.AssertCounterCallCount(t, missing, 9999) },
}
for _, fn := range fns {
AssertErrorMsg(t, fn, "gostats/mock: Counter (%q): not found", missing)
AssertErrorMsg(t, fn, "gostats/mock: Counter (%q): not found in: [\"test-counter\"]", missing)
}

AssertErrorMsg(t, func(t testing.TB) {
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestSink(t *testing.T) {
func(t testing.TB) { sink.AssertGaugeCallCount(t, missing, 9999) },
}
for _, fn := range fns {
AssertErrorMsg(t, fn, "gostats/mock: Gauge (%q): not found", missing)
AssertErrorMsg(t, fn, "gostats/mock: Gauge (%q): not found in: [\"test-gauge\"]", missing)
}

AssertErrorMsg(t, func(t testing.TB) {
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestSink(t *testing.T) {
func(t testing.TB) { sink.AssertTimerCallCount(t, missing, 9999) },
}
for _, fn := range fns {
AssertErrorMsg(t, fn, "gostats/mock: Timer (%q): not found", missing)
AssertErrorMsg(t, fn, "gostats/mock: Timer (%q): not found in: [\"test-timer\"]", missing)
}

AssertErrorMsg(t, func(t testing.TB) {
Expand Down

0 comments on commit 503d0e4

Please sign in to comment.