From 5cb73d6a377b286cac712de2d49f3e9d5167b0a1 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 12 Jun 2018 22:40:04 +1000 Subject: [PATCH] Ensure ConnStats spew nicely --- conn_stats.go | 7 +++++++ misc_test.go | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/conn_stats.go b/conn_stats.go index 76455355..67188acb 100644 --- a/conn_stats.go +++ b/conn_stats.go @@ -1,6 +1,7 @@ package torrent import ( + "fmt" "io" "reflect" "sync/atomic" @@ -48,6 +49,8 @@ type Count struct { n int64 } +var _ fmt.Stringer = (*Count)(nil) + func (me *Count) Add(n int64) { atomic.AddInt64(&me.n, n) } @@ -56,6 +59,10 @@ func (me *Count) Int64() int64 { return atomic.LoadInt64(&me.n) } +func (me *Count) String() string { + return fmt.Sprintf("%v", me.Int64()) +} + func (cs *ConnStats) wroteMsg(msg *pp.Message) { // TODO: Track messages and not just chunks. switch msg.Type { diff --git a/misc_test.go b/misc_test.go index 32e60fa9..33b1784a 100644 --- a/misc_test.go +++ b/misc_test.go @@ -1,10 +1,13 @@ package torrent import ( + "reflect" + "strings" "testing" "github.com/anacrolix/missinggo/bitmap" "github.com/anacrolix/missinggo/iter" + "github.com/davecgh/go-spew/spew" "github.com/stretchr/testify/assert" ) @@ -29,3 +32,10 @@ func TestIterBitmapsDistinct(t *testing.T) { assert.Equal(t, []interface{}{0, 3, 2}, iter.ToSlice(iterBitmapsDistinct(&skipCopy, first, second))) assert.Equal(t, []int{1}, skip.ToSortedSlice()) } + +func TestSpewConnStats(t *testing.T) { + s := spew.Sdump(ConnStats{}) + t.Logf("\n%s\n", s) + lines := strings.Count(s, "\n") + assert.EqualValues(t, 2+reflect.ValueOf(ConnStats{}).NumField(), lines) +}