mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Move formatAndClearCountryStats test into a new metrics_test.go.
This is more unit test–y. We don't need a full broker instantiation for testing this function, unlike other tests in snowflake-broker_test.go.
This commit is contained in:
parent
ec39237e69
commit
cc0a33faea
2 changed files with 33 additions and 22 deletions
33
broker/metrics_test.go
Normal file
33
broker/metrics_test.go
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFormatAndClearCountryStats(t *testing.T) {
|
||||||
|
Convey("for country stats order", 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,
|
||||||
|
} {
|
||||||
|
stats.LoadOrStore(cc, new(uint64))
|
||||||
|
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")
|
||||||
|
// The map should be cleared on return.
|
||||||
|
stats.Range(func(_, _ any) bool { panic("map was not cleared") })
|
||||||
|
})
|
||||||
|
}
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -1030,26 +1029,5 @@ snowflake-ips-nat-unknown 0
|
||||||
ctx.metrics.printMetrics()
|
ctx.metrics.printMetrics()
|
||||||
So(buf.String(), ShouldContainSubstring, "client-denied-count 8\nclient-restricted-denied-count 8\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0")
|
So(buf.String(), ShouldContainSubstring, "client-denied-count 8\nclient-restricted-denied-count 8\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0")
|
||||||
})
|
})
|
||||||
Convey("for country stats order", 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,
|
|
||||||
} {
|
|
||||||
stats.LoadOrStore(cc, new(uint64))
|
|
||||||
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")
|
|
||||||
// The map should be cleared on return.
|
|
||||||
stats.Range(func(_, _ any) bool { panic("map was not cleared") })
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue