Implement Stringer for tcpip.StatCounter

This enables formatting tcpip.Stats readably with %+v.

PiperOrigin-RevId: 228379088
Change-Id: I6a9876454a22f151ee752cf94589b4188729458f
This commit is contained in:
Bert Muthalaly 2019-01-08 12:34:08 -08:00 committed by Shentubot
parent 5ce542ecc7
commit 3f45878b73
2 changed files with 26 additions and 0 deletions

View File

@ -568,6 +568,10 @@ func (s *StatCounter) IncrementBy(v uint64) {
atomic.AddUint64(&s.count, v) atomic.AddUint64(&s.count, v)
} }
func (s *StatCounter) String() string {
return strconv.FormatUint(s.Value(), 10)
}
// IPStats collects IP-specific stats (both v4 and v6). // IPStats collects IP-specific stats (both v4 and v6).
type IPStats struct { type IPStats struct {
// PacketsReceived is the total number of IP packets received from the link // PacketsReceived is the total number of IP packets received from the link

View File

@ -15,7 +15,9 @@
package tcpip package tcpip
import ( import (
"fmt"
"net" "net"
"strings"
"testing" "testing"
) )
@ -193,3 +195,23 @@ func TestAddressString(t *testing.T) {
} }
} }
} }
func TestStatsString(t *testing.T) {
got := fmt.Sprintf("%+v", Stats{}.FillIn())
matchers := []string{
// Print root-level stats correctly.
"UnknownProtocolRcvdPackets:0",
// Print protocol-specific stats correctly.
"TCP:{ActiveConnectionOpenings:0",
}
for _, m := range matchers {
if !strings.Contains(got, m) {
t.Errorf("string.Contains(got, %q) = false", m)
}
}
if t.Failed() {
t.Logf(`got = fmt.Sprintf("%%+v", Stats{}.FillIn()) = %q`, got)
}
}