improve code coverage a bit

This commit is contained in:
Jeromy 2016-04-20 10:20:46 -07:00
parent 2606cf5154
commit 5b171ba4bf
2 changed files with 51 additions and 0 deletions

View File

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

View File

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