From 89245fbdae4df56f0d6364d3d91afd9ac58edd1c Mon Sep 17 00:00:00 2001 From: lash Date: Fri, 30 Nov 2018 16:01:08 +0100 Subject: [PATCH] p2p/simulation: WIP minimal snapshot test --- p2p/simulations/network_test.go | 73 +++++++++++++++++++++ p2p/simulations/test.go | 2 +- swarm/network/simulation/simulation_test.go | 35 +++------- 3 files changed, 83 insertions(+), 27 deletions(-) diff --git a/p2p/simulations/network_test.go b/p2p/simulations/network_test.go index f349352651..accdda5b7d 100644 --- a/p2p/simulations/network_test.go +++ b/p2p/simulations/network_test.go @@ -22,10 +22,83 @@ import ( "testing" "time" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/simulations/adapters" ) +func TestSnapshot(t *testing.T) { + protoCMap := make(map[enode.ID]map[enode.ID]chan struct{}) + adapter := adapters.NewSimAdapter(adapters.Services{ + "noopwoop": func(ctx *adapters.ServiceContext) (node.Service, error) { + svc := NewNoopService() + protoCMap[ctx.Config.ID] = svc.C + return svc, nil + }, + }) + network := NewNetwork(adapter, &NetworkConfig{ + DefaultService: "noopwoop", + }) + defer network.Shutdown() + + nodeCount := 20 + ids := make([]enode.ID, nodeCount) + for i := 0; i < nodeCount; i++ { + conf := adapters.RandomNodeConfig() + node, err := network.NewNodeWithConfig(conf) + if err != nil { + t.Fatalf("error creating node: %s", err) + } + if err := network.Start(node.ID()); err != nil { + t.Fatalf("error starting node: %s", err) + } + ids[i] = node.ID() + } + + go func() { + for i, id := range ids { + peerID := ids[(i+1)%len(ids)] + if err := network.Connect(id, peerID); err != nil { + t.Fatal(err) + } + } + }() + + evC := make(chan *Event) + sub := network.Events().Subscribe(evC) + defer sub.Unsubscribe() + + ctx, cancel := context.WithTimeout(context.TODO(), time.Second) + defer cancel() + + connEventCount := nodeCount +OUTER: + for { + select { + case <-ctx.Done(): + t.Fatal(ctx.Err()) + case ev := <-evC: + if ev.Type == EventTypeConn && !ev.Control { + connEventCount-- + log.Debug("ev", "count", connEventCount) + if connEventCount == 0 { + break OUTER + } + } + } + } + + for nodid, peers := range protoCMap { + for peerid, peerC := range peers { + log.Debug("getting ", "node", nodid, "peer", peerid) + <-peerC + } + } + + t.Logf("ok") +} + // TestNetworkSimulation creates a multi-node simulation network with each node // connected in a ring topology, checks that all nodes successfully handshake // with each other and that a snapshot fully represents the desired topology diff --git a/p2p/simulations/test.go b/p2p/simulations/test.go index beeb414e41..451c06080b 100644 --- a/p2p/simulations/test.go +++ b/p2p/simulations/test.go @@ -23,7 +23,7 @@ func NewNoopService(ackC map[enode.ID]chan struct{}) *NoopService { func (t *NoopService) Protocols() []p2p.Protocol { return []p2p.Protocol{ - { + p2p.Protocol{ Name: "noop", Version: 666, Length: 0, diff --git a/swarm/network/simulation/simulation_test.go b/swarm/network/simulation/simulation_test.go index ca8599d7c0..4667a2abc3 100644 --- a/swarm/network/simulation/simulation_test.go +++ b/swarm/network/simulation/simulation_test.go @@ -26,9 +26,8 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/node" - "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/p2p/simulations" "github.com/ethereum/go-ethereum/p2p/simulations/adapters" - "github.com/ethereum/go-ethereum/rpc" colorable "github.com/mattn/go-colorable" ) @@ -182,39 +181,23 @@ func noopServiceFunc(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, f return newNoopService(), nil, nil } -// noopService is the service that does not do anything -// but implements node.Service interface. -type noopService struct{} - func newNoopService() node.Service { return &noopService{} } -func (t *noopService) Protocols() []p2p.Protocol { - return []p2p.Protocol{} -} - -func (t *noopService) APIs() []rpc.API { - return []rpc.API{} -} - -func (t *noopService) Start(server *p2p.Server) error { - return nil -} - -func (t *noopService) Stop() error { - return nil -} - -// a helper function for most basic noop service -// of a different type then noopService to test +// a helper function for most basic Noop service +// of a different type then NoopService to test // multiple services on one node. func noopService2Func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { return new(noopService2), nil, nil } -// noopService2 is the service that does not do anything +// NoopService2 is the service that does not do anything // but implements node.Service interface. type noopService2 struct { - noopService + simulations.NoopService +} + +type noopService struct { + simulations.NoopService }