p2p/simulations: added concurrent goroutines to prevent sleeps on simulation connect/disconnect

This commit is contained in:
Elad Nachmias 2018-11-07 13:39:02 +05:30
parent 724581134f
commit e59205c9c3
2 changed files with 59 additions and 21 deletions

View file

@ -605,7 +605,6 @@ func TestHTTPSnapshot(t *testing.T) {
if err := client.ConnectNode(nodes[0].ID, nodes[1].ID); err != nil { if err := client.ConnectNode(nodes[0].ID, nodes[1].ID); err != nil {
t.Fatalf("error connecting nodes: %s", err) t.Fatalf("error connecting nodes: %s", err)
} }
time.Sleep(time.Second)
// store some state in the test services // store some state in the test services
states := make([]string, nodeCount) states := make([]string, nodeCount)

View file

@ -38,18 +38,18 @@ func init() {
func TestSnapshotExplicit(t *testing.T) { func TestSnapshotExplicit(t *testing.T) {
// create simulation network with 20 testService nodes // create simulation network with 20 testService nodes
adapter := adapters.NewSimAdapter(adapters.Services{ adapter := adapters.NewSimAdapter(adapters.Services{
"test": newTestService, "dummy": newDummyService,
"placebo": newPlaceboService, "dummy2": newDummy2Service,
}) })
network := NewNetwork(adapter, &NetworkConfig{ network := NewNetwork(adapter, &NetworkConfig{
DefaultService: "test", DefaultService: "dummy",
}) })
defer network.Shutdown() defer network.Shutdown()
nodeCount := 3 nodeCount := 3
ids := make([]enode.ID, nodeCount) ids := make([]enode.ID, nodeCount)
for i := 0; i < nodeCount; i++ { for i := 0; i < nodeCount; i++ {
conf := adapters.RandomNodeConfig() conf := adapters.RandomNodeConfig()
conf.Services = []string{"test", "placebo"} conf.Services = []string{"dummy", "dummy2"}
node, err := network.NewNodeWithConfig(conf) node, err := network.NewNodeWithConfig(conf)
if err != nil { if err != nil {
t.Fatalf("error creating node: %s", err) t.Fatalf("error creating node: %s", err)
@ -59,12 +59,44 @@ func TestSnapshotExplicit(t *testing.T) {
} }
ids[i] = node.ID() ids[i] = node.ID()
} }
var eventsDone = make(chan struct{})
network.Connect(ids[0], ids[1]) count := 2
network.Connect(ids[0], ids[2]) events := make(chan *Event)
time.Sleep(time.Second) sub := network.Events().Subscribe(events)
network.Disconnect(ids[0], ids[2]) go func() {
time.Sleep(time.Second) for event := range events {
if event.Type == EventTypeConn && !event.Control {
count--
if count == 0 {
eventsDone <- struct{}{}
return
}
}
}
}()
err := network.Connect(ids[0], ids[1])
if err != nil {
t.Fatal(err)
}
err = network.Connect(ids[0], ids[2])
if err != nil {
t.Fatal(err)
}
<-eventsDone
go func() {
defer sub.Unsubscribe()
for event := range events {
if event.Type == EventTypeConn && !event.Control {
eventsDone <- struct{}{}
return
}
}
}()
err = network.Disconnect(ids[0], ids[2])
if err != nil {
t.Fatal(err)
}
<-eventsDone
snap, err := network.Snapshot() snap, err := network.Snapshot()
if err != nil { if err != nil {
@ -74,7 +106,7 @@ func TestSnapshotExplicit(t *testing.T) {
t.Fatalf("expected one connect object") t.Fatalf("expected one connect object")
} }
for _, svc := range snap.Nodes[0].Node.Config.Services { for _, svc := range snap.Nodes[0].Node.Config.Services {
if svc != "test" && svc != "placebo" { if svc != "dummy" && svc != "dummy2" {
t.Fatalf("unexpected service %s", svc) t.Fatalf("unexpected service %s", svc)
} }
} }
@ -84,7 +116,7 @@ func TestSnapshotExplicit(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
for _, svc := range snap.Nodes[0].Node.Config.Services { for _, svc := range snap.Nodes[0].Node.Config.Services {
if svc != "test" && svc != "placebo" && svc != "bzz" { if svc != "dummy" && svc != "dummy2" && svc != "bzz" {
t.Fatalf("unexpected service %s", svc) t.Fatalf("unexpected service %s", svc)
} }
} }
@ -94,7 +126,7 @@ func TestSnapshotExplicit(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
for _, svc := range snap.Nodes[0].Node.Config.Services { for _, svc := range snap.Nodes[0].Node.Config.Services {
if svc != "placebo" && svc != "bzz" { if svc != "dummy" && svc != "dummy2" && svc != "bzz" {
t.Fatalf("unexpected service %s", svc) t.Fatalf("unexpected service %s", svc)
} }
} }
@ -214,26 +246,33 @@ func TestNetworkSimulation(t *testing.T) {
} }
} }
type placeboService struct { type dummyService struct {
} }
func newPlaceboService(ctx *adapters.ServiceContext) (node.Service, error) { type dummy2Service struct {
return &placeboService{}, nil dummyService
} }
func (p *placeboService) APIs() []rpc.API { func newDummyService(ctx *adapters.ServiceContext) (node.Service, error) {
return &dummyService{}, nil
}
func newDummy2Service(ctx *adapters.ServiceContext) (node.Service, error) {
return &dummy2Service{}, nil
}
func (p *dummyService) APIs() []rpc.API {
return []rpc.API{} return []rpc.API{}
} }
func (p *placeboService) Protocols() []p2p.Protocol { func (p *dummyService) Protocols() []p2p.Protocol {
return []p2p.Protocol{} return []p2p.Protocol{}
} }
func (p *placeboService) Start(server *p2p.Server) error { func (p *dummyService) Start(server *p2p.Server) error {
return nil return nil
} }
func (p *placeboService) Stop() error { func (p *dummyService) Stop() error {
return nil return nil
} }