1package stats_test
2
3import (
4	"testing"
5
6	"github.com/facebookgo/ensure"
7	"github.com/facebookgo/stats"
8)
9
10// Ensure calling End works even when a BumpTimeHook isn't provided.
11func TestHookClientBumpTime(t *testing.T) {
12	(&stats.HookClient{}).BumpTime("foo").End()
13}
14
15func TestPrefixClient(t *testing.T) {
16	const (
17		prefix1      = "prefix1"
18		prefix2      = "prefix2"
19		avgKey       = "avg"
20		avgVal       = float64(1)
21		sumKey       = "sum"
22		sumVal       = float64(2)
23		histogramKey = "histogram"
24		histogramVal = float64(3)
25		timeKey      = "time"
26	)
27
28	var keys []string
29	hc := &stats.HookClient{
30		BumpAvgHook: func(key string, val float64) {
31			keys = append(keys, key)
32			ensure.DeepEqual(t, val, avgVal)
33		},
34		BumpSumHook: func(key string, val float64) {
35			keys = append(keys, key)
36			ensure.DeepEqual(t, val, sumVal)
37		},
38		BumpHistogramHook: func(key string, val float64) {
39			keys = append(keys, key)
40			ensure.DeepEqual(t, val, histogramVal)
41		},
42		BumpTimeHook: func(key string) interface {
43			End()
44		} {
45			return multiEnderTest{
46				EndHook: func() {
47					keys = append(keys, key)
48				},
49			}
50		},
51	}
52
53	pc := stats.PrefixClient([]string{prefix1, prefix2}, hc)
54	pc.BumpAvg(avgKey, avgVal)
55	pc.BumpSum(sumKey, sumVal)
56	pc.BumpHistogram(histogramKey, histogramVal)
57	pc.BumpTime(timeKey).End()
58
59	ensure.SameElements(t, keys, []string{
60		prefix1 + avgKey,
61		prefix1 + sumKey,
62		prefix1 + histogramKey,
63		prefix1 + timeKey,
64		prefix2 + avgKey,
65		prefix2 + sumKey,
66		prefix2 + histogramKey,
67		prefix2 + timeKey,
68	})
69}
70
71type multiEnderTest struct {
72	EndHook func()
73}
74
75func (e multiEnderTest) End() {
76	e.EndHook()
77}
78