]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Ensure ConnStats spew nicely
authorMatt Joiner <anacrolix@gmail.com>
Tue, 12 Jun 2018 12:40:04 +0000 (22:40 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 12 Jun 2018 12:40:04 +0000 (22:40 +1000)
conn_stats.go
misc_test.go

index 76455355e7469e05f4eadea00f9c845e521dce1f..67188acb0e1fd10b6c441d0b80b3be8cd411278f 100644 (file)
@@ -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 {
index 32e60fa956319a9b1b414a9f411e8ff63e0758d0..33b1784abddd86a20f6afc793ac013b2f841d771 100644 (file)
@@ -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)
+}