diff --git a/broker/metrics_test.go b/broker/metrics_test.go index 148bf4a..2507299 100644 --- a/broker/metrics_test.go +++ b/broker/metrics_test.go @@ -2,31 +2,34 @@ package main import ( "sync" - "sync/atomic" "testing" . "github.com/smartystreets/goconvey/convey" ) func TestFormatAndClearCountryStats(t *testing.T) { - Convey("for country stats order", t, func() { + Convey("given a mapping of country stats", t, func() { stats := new(sync.Map) - for cc, count := range map[string]uint64{ - "IT": 50, - "FR": 200, - "TZ": 100, - "CN": 250, - "RU": 150, - "CA": 1, - "BE": 1, - "PH": 1, + for _, record := range []struct { + cc string + count uint64 + }{ + {"IT", 50}, + {"FR", 200}, + {"TZ", 100}, + {"CN", 250}, + {"RU", 150}, + {"CA", 1}, + {"BE", 1}, + {"PH", 1}, } { - stats.LoadOrStore(cc, new(uint64)) - val, _ := stats.Load(cc) - ptr := val.(*uint64) - atomic.AddUint64(ptr, count) + stats.Store(record.cc, &record.count) } - So(formatAndClearCountryStats(stats, false), ShouldEqual, "CN=250,FR=200,RU=150,TZ=100,IT=50,BE=1,CA=1,PH=1") + + Convey("the order should be correct with binned=false", func() { + So(formatAndClearCountryStats(stats, false), ShouldEqual, "CN=250,FR=200,RU=150,TZ=100,IT=50,BE=1,CA=1,PH=1") + }) + // The map should be cleared on return. stats.Range(func(_, _ any) bool { panic("map was not cleared") }) })