swarm/network: Rework Health test to strict

Pending add test for saturation
And add test for as many as possible up to saturation
This commit is contained in:
lash 2018-12-17 23:30:02 +01:00
parent 89e7259149
commit bdcca4cdf4
2 changed files with 83 additions and 31 deletions

View file

@ -471,7 +471,6 @@ func depthForPot(p *pot.Pot, minProxBinSize int, pivotAddr []byte) (depth int) {
p.EachNeighbour(pivotAddr, Pof, f) p.EachNeighbour(pivotAddr, Pof, f)
p.EachBin(pivotAddr, Pof, 0, func(po int, _ int, f func(func(pot.Val, int) bool) bool) bool { p.EachBin(pivotAddr, Pof, 0, func(po int, _ int, f func(func(pot.Val, int) bool) bool) bool {
log.Trace("eachbin", "addr", pivotAddr, "po", po)
if po == depth { if po == depth {
if maxDepth == depth { if maxDepth == depth {
return false return false
@ -639,7 +638,6 @@ func NewPeerPotMap(minProxBinSize int, addrs [][]byte) map[string]*PeerPot {
if po == 256 { if po == 256 {
return true return true
} }
// append any neighbors found // append any neighbors found
// a neighbor is any peer in or deeper than the depth // a neighbor is any peer in or deeper than the depth
if po >= depth { if po >= depth {

View file

@ -162,43 +162,96 @@ func TestNeighbourhoodDepth(t *testing.T) {
testNum++ testNum++
} }
func TestHealth(t *testing.T) { func TestHealthStrict(t *testing.T) {
t.Skip("foo")
k := newTestKademlia("00000000") // base address is all zeros
assertHealth(t, k, false) // no peers
Register(k, "00001000") // unhealthy (and lonely)
k := newTestKademlia("11111111")
if err := assertHealth(t, k, false, false); err != nil {
t.Fatal(err)
}
// know one peer but not connected
// unhealthy
Register(k, "11100000")
log.Trace(k.String()) log.Trace(k.String())
assertHealth(t, k, false) if err := assertHealth(t, k, false, false); err != nil {
On(k, "00001000") t.Fatal(err)
assertHealth(t, k, true) }
Register(k, "00000100")
// know one peer and connected
// healthy
On(k, "11100000")
if err := assertHealth(t, k, true, false); err != nil {
t.Fatal(err)
}
// know two peers, only one connected
// unhealthy
Register(k, "11111100")
log.Trace(k.String()) log.Trace(k.String())
assertHealth(t, k, false) if err := assertHealth(t, k, false, false); err != nil {
On(k, "00000100") t.Fatal(err)
assertHealth(t, k, true) }
Register(k, "10000000")
// know two peers and connected to both
// healthy
On(k, "11111100")
if err := assertHealth(t, k, true, false); err != nil {
t.Fatal(err)
}
// know three peers, connected to the two deepest
// healthy
Register(k, "00000000")
log.Trace(k.String()) log.Trace(k.String())
assertHealth(t, k, false) if err := assertHealth(t, k, true, false); err != nil {
On(k, "10000000") t.Fatal(err)
assertHealth(t, k, true) }
Register(k, "00100000")
// know three peers, connected to all three
// healthy
On(k, "00000000")
if err := assertHealth(t, k, true, false); err != nil {
t.Fatal(err)
}
// add fourth peer deeper than current depth
// unhealthy
Register(k, "11110000")
log.Trace(k.String()) log.Trace(k.String())
assertHealth(t, k, false) if err := assertHealth(t, k, false, false); err != nil {
On(k, "00100000") t.Fatal(err)
assertHealth(t, k, true) }
Register(k, "01000000")
// connected to three deepest peers
// healthy
On(k, "11110000")
if err := assertHealth(t, k, true, false); err != nil {
t.Fatal(err)
}
// add additional peer in same bin as deepest peer
// unhealthy
Register(k, "11111101")
log.Trace(k.String()) log.Trace(k.String())
assertHealth(t, k, false) if err := assertHealth(t, k, false, false); err != nil {
On(k, "01000000") t.Fatal(err)
assertHealth(t, k, true) }
// four deepest of five peers connected
// healthy
On(k, "11111101")
if err := assertHealth(t, k, true, false); err != nil {
t.Fatal(err)
}
} }
func assertHealth(t *testing.T, k *Kademlia, expectHealthy bool) { func assertHealth(t *testing.T, k *Kademlia, expectHealthy bool, expectSaturation bool) error {
kid := common.Bytes2Hex(k.BaseAddr()) kid := common.Bytes2Hex(k.BaseAddr())
kads := []*Kademlia{k} addrs := [][]byte{k.BaseAddr()}
var addrs [][]byte
k.EachAddr(nil, 255, func(addr *BzzAddr, po int, _ bool) bool { k.EachAddr(nil, 255, func(addr *BzzAddr, po int, _ bool) bool {
kads = append(kads, NewKademlia(addr.Address(), newTestKademliaParams()))
addrs = append(addrs, addr.Address()) addrs = append(addrs, addr.Address())
return true return true
}) })
@ -212,8 +265,9 @@ func assertHealth(t *testing.T, k *Kademlia, expectHealthy bool) {
// - we are connected to all known neighbors // - we are connected to all known neighbors
health := healthParams.KnowNN && healthParams.GotNN && healthParams.CountKnowNN > 0 health := healthParams.KnowNN && healthParams.GotNN && healthParams.CountKnowNN > 0
if expectHealthy != health { if expectHealthy != health {
t.Fatalf("expected kademlia health %v, is %v\n%v", expectHealthy, health, k.String()) return fmt.Errorf("expected kademlia health %v, is %v\n%v", expectHealthy, health, k.String())
} }
return nil
} }
func testSuggestPeer(k *Kademlia, expAddr string, expPo int, expWant bool) error { func testSuggestPeer(k *Kademlia, expAddr string, expPo int, expWant bool) error {