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) +}