mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
p2p/simulations: Add comments, timeout check on additional events
This commit is contained in:
parent
57522410d9
commit
de133fceeb
1 changed files with 74 additions and 21 deletions
|
|
@ -32,18 +32,25 @@ import (
|
||||||
// Tests that a created snapshot with a minimal service only contains the expected connections
|
// Tests that a created snapshot with a minimal service only contains the expected connections
|
||||||
// and that a network when loaded with this snapshot only contains those same connections
|
// and that a network when loaded with this snapshot only contains those same connections
|
||||||
func TestSnapshot(t *testing.T) {
|
func TestSnapshot(t *testing.T) {
|
||||||
protoCMap := make(map[enode.ID]map[enode.ID]chan struct{})
|
|
||||||
|
// PART I
|
||||||
|
// create snapshot from ring network
|
||||||
|
|
||||||
|
// this is a minimal service, whose protocol will take exactly one message OR close of connection before quitting
|
||||||
|
//protoCMap := make(map[enode.ID]map[enode.ID]chan struct{})
|
||||||
adapter := adapters.NewSimAdapter(adapters.Services{
|
adapter := adapters.NewSimAdapter(adapters.Services{
|
||||||
"noopwoop": func(ctx *adapters.ServiceContext) (node.Service, error) {
|
"noopwoop": func(ctx *adapters.ServiceContext) (node.Service, error) {
|
||||||
svc := NewNoopService(false)
|
// svc := NewNoopService(false)
|
||||||
protoCMap[ctx.Config.ID] = svc.C
|
// protoCMap[ctx.Config.ID] = svc.C
|
||||||
return svc, nil
|
// return svc, nil
|
||||||
|
return NewNoopService(false), nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// create network
|
||||||
network := NewNetwork(adapter, &NetworkConfig{
|
network := NewNetwork(adapter, &NetworkConfig{
|
||||||
DefaultService: "noopwoop",
|
DefaultService: "noopwoop",
|
||||||
})
|
})
|
||||||
|
|
||||||
// \todo consider making a member of network, set to true threadsafe when shutdown
|
// \todo consider making a member of network, set to true threadsafe when shutdown
|
||||||
runningOne := true
|
runningOne := true
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
@ -52,6 +59,7 @@ func TestSnapshot(t *testing.T) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// create and start nodes
|
||||||
nodeCount := 20
|
nodeCount := 20
|
||||||
ids := make([]enode.ID, nodeCount)
|
ids := make([]enode.ID, nodeCount)
|
||||||
for i := 0; i < nodeCount; i++ {
|
for i := 0; i < nodeCount; i++ {
|
||||||
|
|
@ -66,6 +74,13 @@ func TestSnapshot(t *testing.T) {
|
||||||
ids[i] = node.ID()
|
ids[i] = node.ID()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// subscribe to peer events
|
||||||
|
evC := make(chan *Event)
|
||||||
|
sub := network.Events().Subscribe(evC)
|
||||||
|
defer sub.Unsubscribe()
|
||||||
|
|
||||||
|
// connect nodes in a ring
|
||||||
|
// spawn separate thread to avoid deadlock in the event listeners
|
||||||
go func() {
|
go func() {
|
||||||
for i, id := range ids {
|
for i, id := range ids {
|
||||||
peerID := ids[(i+1)%len(ids)]
|
peerID := ids[(i+1)%len(ids)]
|
||||||
|
|
@ -75,13 +90,9 @@ func TestSnapshot(t *testing.T) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
evC := make(chan *Event)
|
// collect connection events up to expected number
|
||||||
sub := network.Events().Subscribe(evC)
|
|
||||||
defer sub.Unsubscribe()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
checkIds := make(map[enode.ID][]enode.ID)
|
checkIds := make(map[enode.ID][]enode.ID)
|
||||||
connEventCount := nodeCount
|
connEventCount := nodeCount
|
||||||
OUTER:
|
OUTER:
|
||||||
|
|
@ -91,6 +102,8 @@ OUTER:
|
||||||
t.Fatal(ctx.Err())
|
t.Fatal(ctx.Err())
|
||||||
case ev := <-evC:
|
case ev := <-evC:
|
||||||
if ev.Type == EventTypeConn && !ev.Control {
|
if ev.Type == EventTypeConn && !ev.Control {
|
||||||
|
|
||||||
|
// fail on any disconnect
|
||||||
if !ev.Conn.Up {
|
if !ev.Conn.Up {
|
||||||
t.Fatalf("unexpected disconnect: %v -> %v", ev.Conn.One, ev.Conn.Other)
|
t.Fatalf("unexpected disconnect: %v -> %v", ev.Conn.One, ev.Conn.Other)
|
||||||
}
|
}
|
||||||
|
|
@ -105,6 +118,7 @@ OUTER:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create snapshot of current network
|
||||||
snap, err := network.Snapshot()
|
snap, err := network.Snapshot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
@ -121,17 +135,19 @@ OUTER:
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for all protocols to signal to close down
|
// wait for all protocols to signal to close down
|
||||||
for nodid, peers := range protoCMap {
|
// for nodid, peers := range protoCMap {
|
||||||
for peerid, peerC := range peers {
|
// for peerid, peerC := range peers {
|
||||||
log.Debug("getting ", "node", nodid, "peer", peerid)
|
// log.Debug("getting ", "node", nodid, "peer", peerid)
|
||||||
<-peerC
|
// <-peerC
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// shut down sim network
|
||||||
runningOne = false
|
runningOne = false
|
||||||
sub.Unsubscribe()
|
sub.Unsubscribe()
|
||||||
network.Shutdown()
|
network.Shutdown()
|
||||||
|
|
||||||
// check that we have all expected connections in snapshot
|
// check that we have all the expected connections in the snapshot
|
||||||
for nodid, nodConns := range checkIds {
|
for nodid, nodConns := range checkIds {
|
||||||
for _, nodConn := range nodConns {
|
for _, nodConn := range nodConns {
|
||||||
var match bool
|
var match bool
|
||||||
|
|
@ -153,12 +169,14 @@ OUTER:
|
||||||
|
|
||||||
// PART II
|
// PART II
|
||||||
// load snapshot and verify that exactly same connections are formed
|
// load snapshot and verify that exactly same connections are formed
|
||||||
protoCMap = make(map[enode.ID]map[enode.ID]chan struct{})
|
|
||||||
|
//protoCMap = make(map[enode.ID]map[enode.ID]chan struct{})
|
||||||
adapter = adapters.NewSimAdapter(adapters.Services{
|
adapter = adapters.NewSimAdapter(adapters.Services{
|
||||||
"noopwoop": func(ctx *adapters.ServiceContext) (node.Service, error) {
|
"noopwoop": func(ctx *adapters.ServiceContext) (node.Service, error) {
|
||||||
svc := NewNoopService(false)
|
// svc := NewNoopService(false)
|
||||||
protoCMap[ctx.Config.ID] = svc.C
|
// protoCMap[ctx.Config.ID] = svc.C
|
||||||
return svc, nil
|
// return svc, nil
|
||||||
|
return NewNoopService(false), nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
network = NewNetwork(adapter, &NetworkConfig{
|
network = NewNetwork(adapter, &NetworkConfig{
|
||||||
|
|
@ -168,10 +186,13 @@ OUTER:
|
||||||
network.Shutdown()
|
network.Shutdown()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// subscribe to peer events
|
||||||
evC = make(chan *Event)
|
evC = make(chan *Event)
|
||||||
sub = network.Events().Subscribe(evC)
|
sub = network.Events().Subscribe(evC)
|
||||||
defer sub.Unsubscribe()
|
defer sub.Unsubscribe()
|
||||||
|
|
||||||
|
// load the snapshot
|
||||||
|
// spawn separate thread to avoid deadlock in the event listeners
|
||||||
go func() {
|
go func() {
|
||||||
err = network.Load(snap)
|
err = network.Load(snap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -179,6 +200,7 @@ OUTER:
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// collect connection events up to expected number
|
||||||
ctx, cancel = context.WithTimeout(context.TODO(), time.Second*3)
|
ctx, cancel = context.WithTimeout(context.TODO(), time.Second*3)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
|
@ -190,6 +212,8 @@ OUTER_TWO:
|
||||||
t.Fatal(ctx.Err())
|
t.Fatal(ctx.Err())
|
||||||
case ev := <-evC:
|
case ev := <-evC:
|
||||||
if ev.Type == EventTypeConn && !ev.Control {
|
if ev.Type == EventTypeConn && !ev.Control {
|
||||||
|
|
||||||
|
// fail on any disconnect
|
||||||
if !ev.Conn.Up {
|
if !ev.Conn.Up {
|
||||||
t.Fatalf("unexpected disconnect: %v -> %v", ev.Conn.One, ev.Conn.Other)
|
t.Fatalf("unexpected disconnect: %v -> %v", ev.Conn.One, ev.Conn.Other)
|
||||||
}
|
}
|
||||||
|
|
@ -205,6 +229,35 @@ OUTER_TWO:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check that we have all expected connections in the network
|
||||||
|
for _, snapConn := range snap.Conns {
|
||||||
|
var match bool
|
||||||
|
for nodid, nodConns := range checkIds {
|
||||||
|
for _, nodConn := range nodConns {
|
||||||
|
if snapConn.One == nodid && snapConn.Other == nodConn {
|
||||||
|
match = true
|
||||||
|
break
|
||||||
|
} else if snapConn.Other == nodid && snapConn.One == nodConn {
|
||||||
|
match = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !match {
|
||||||
|
t.Fatalf("network missing conn %v -> %v", snapConn.One, snapConn.Other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify that network didn't generate any other additional connection events after the ones we have collected within a reasonable period of time
|
||||||
|
ctx, cancel = context.WithTimeout(context.TODO(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
case ev := <-evC:
|
||||||
|
if ev.Type == EventTypeConn {
|
||||||
|
t.Fatalf("Superfluous conn found %v -> %v", ev.Conn.One, ev.Conn.Other)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestNetworkSimulation creates a multi-node simulation network with each node
|
// TestNetworkSimulation creates a multi-node simulation network with each node
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue