mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 11:11:30 -04:00
Refactor TestFormatAndClearCountryStats.
This commit is contained in:
parent
cc0a33faea
commit
bd04cd7752
1 changed files with 19 additions and 16 deletions
|
@ -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,
|
{"IT", 50},
|
||||||
"RU": 150,
|
{"FR", 200},
|
||||||
"CA": 1,
|
{"TZ", 100},
|
||||||
"BE": 1,
|
{"CN", 250},
|
||||||
"PH": 1,
|
{"RU", 150},
|
||||||
|
{"CA", 1},
|
||||||
|
{"BE", 1},
|
||||||
|
{"PH", 1},
|
||||||
} {
|
} {
|
||||||
stats.LoadOrStore(cc, new(uint64))
|
stats.Store(record.cc, &record.count)
|
||||||
val, _ := stats.Load(cc)
|
|
||||||
ptr := val.(*uint64)
|
|
||||||
atomic.AddUint64(ptr, 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.
|
// 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") })
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue