Refactor TestFormatAndClearCountryStats.

This commit is contained in:
David Fifield 2025-08-15 18:35:53 +00:00 committed by Cecylia Bocovich
parent cc0a33faea
commit bd04cd7752
No known key found for this signature in database
GPG key ID: 009DE379FD9B7B90

View file

@ -2,31 +2,34 @@ package main
import ( import (
"sync" "sync"
"sync/atomic"
"testing" "testing"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
) )
func TestFormatAndClearCountryStats(t *testing.T) { 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) stats := new(sync.Map)
for cc, count := range map[string]uint64{ for _, record := range []struct {
"IT": 50, cc string
"FR": 200, count uint64
"TZ": 100,
"CN": 250,
"RU": 150,
"CA": 1,
"BE": 1,
"PH": 1,
}{ }{
stats.LoadOrStore(cc, new(uint64)) {"IT", 50},
val, _ := stats.Load(cc) {"FR", 200},
ptr := val.(*uint64) {"TZ", 100},
atomic.AddUint64(ptr, count) {"CN", 250},
{"RU", 150},
{"CA", 1},
{"BE", 1},
{"PH", 1},
} {
stats.Store(record.cc, &record.count)
} }
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") 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. // The map should be cleared on return.
stats.Range(func(_, _ any) bool { panic("map was not cleared") }) stats.Range(func(_, _ any) bool { panic("map was not cleared") })
}) })