From 5b171ba4bfad7511ac740d1af9368941f344c0e2 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 20 Apr 2016 10:20:46 -0700 Subject: [PATCH] improve code coverage a bit --- addr_manager_test.go | 22 ++++++++++++++++++++++ metrics_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/addr_manager_test.go b/addr_manager_test.go index 0de0c6a..52f621c 100644 --- a/addr_manager_test.go +++ b/addr_manager_test.go @@ -88,6 +88,10 @@ func TestAddresses(t *testing.T) { m.ClearAddrs(id5) m.AddAddrs(id5, []ma.Multiaddr{ma51, ma52, ma53, ma54, ma55}, ttl) // clearing + if len(m.Peers()) != 5 { + t.Fatal("should have exactly two peers in the address book") + } + // test the Addresses return value testHas(t, []ma.Multiaddr{ma11}, m.Addrs(id1)) testHas(t, []ma.Multiaddr{ma21, ma22}, m.Addrs(id2)) @@ -114,6 +118,10 @@ func TestAddressesExpire(t *testing.T) { m.AddAddr(id2, ma24, time.Hour) m.AddAddr(id2, ma25, time.Hour) + if len(m.Peers()) != 2 { + t.Fatal("should have exactly two peers in the address book") + } + testHas(t, []ma.Multiaddr{ma11, ma12, ma13}, m.Addrs(id1)) testHas(t, []ma.Multiaddr{ma24, ma25}, m.Addrs(id2)) @@ -178,3 +186,17 @@ func TestClearWorks(t *testing.T) { testHas(t, nil, m.Addrs(id1)) testHas(t, nil, m.Addrs(id2)) } + +func TestSetNegativeTTLClears(t *testing.T) { + id1 := IDS(t, "QmcNstKuwBBoVTpSCSDrwzjgrRcaYXK833Psuz2EMHwyQN") + ma11 := MA(t, "/ip4/1.2.3.1/tcp/1111") + + m := AddrManager{} + m.SetAddr(id1, ma11, time.Hour) + + testHas(t, []ma.Multiaddr{ma11}, m.Addrs(id1)) + + m.SetAddr(id1, ma11, -1) + + testHas(t, nil, m.Addrs(id1)) +} diff --git a/metrics_test.go b/metrics_test.go index 4d9bf25..bd10a1a 100644 --- a/metrics_test.go +++ b/metrics_test.go @@ -2,6 +2,7 @@ package peer_test import ( "fmt" + "math" "math/rand" "testing" "time" @@ -38,3 +39,31 @@ func TestLatencyEWMAFun(t *testing.T) { } } } + +func TestLatencyEWMA(t *testing.T) { + m := peer.NewMetrics() + id, err := testutil.RandPeerID() + if err != nil { + t.Fatal(err) + } + + exp := 100.0 + mu := exp + sig := 10.0 + next := func() time.Duration { + mu = (rand.NormFloat64() * sig) + mu + return time.Duration(mu) + } + + for i := 0; i < 10; i++ { + select { + case <-time.After(200 * time.Millisecond): + m.RecordLatency(id, next()) + } + } + + lat := m.LatencyEWMA(id) + if math.Abs(exp-float64(lat)) > sig { + t.Fatal("latency outside of expected range: ", exp, lat, sig) + } +}