p2p/simulations, swarm/network: Custom services in snapshot (#17991)

* p2p/simulations: Add custom services to simnodes + remove sim down conn objs

* p2p/simulation, swarm/network: Add selective services to discovery sim

* p2p/simulations, swarm/network: Remove useless comments

* p2p/simulations, swarm/network: Clean up mess from rebase

* p2p/simulation: Add sleep to prevent connect flakiness in http test

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

* p2p/simulations, swarm/network/simulations: address pr comments

* reinstated dummy service

* fixed http snapshot test
This commit is contained in:
lash
2018-11-12 14:57:17 +01:00
committed by Viktor Trón
parent a0876f7433
commit 201a0bf181
3 changed files with 118 additions and 12 deletions

View File

@ -18,6 +18,7 @@ package simulations
import (
"context"
"flag"
"fmt"
"math/rand"
"net/http/httptest"
@ -28,13 +29,26 @@ import (
"time"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
"github.com/ethereum/go-ethereum/rpc"
colorable "github.com/mattn/go-colorable"
)
var (
loglevel = flag.Int("loglevel", 2, "verbosity of logs")
)
func init() {
flag.Parse()
log.PrintOrigins(true)
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
}
// testService implements the node.Service interface and provides protocols
// and APIs which are useful for testing nodes in a simulation network
type testService struct {
@ -584,9 +598,26 @@ func TestHTTPNodeRPC(t *testing.T) {
// TestHTTPSnapshot tests creating and loading network snapshots
func TestHTTPSnapshot(t *testing.T) {
// start the server
_, s := testHTTPServer(t)
network, s := testHTTPServer(t)
defer s.Close()
var eventsDone = make(chan struct{})
count := 1
eventsDoneChan := make(chan *Event)
eventSub := network.Events().Subscribe(eventsDoneChan)
go func() {
defer eventSub.Unsubscribe()
for event := range eventsDoneChan {
if event.Type == EventTypeConn && !event.Control {
count--
if count == 0 {
eventsDone <- struct{}{}
return
}
}
}
}()
// create a two-node network
client := NewClient(s.URL)
nodeCount := 2
@ -620,7 +651,7 @@ func TestHTTPSnapshot(t *testing.T) {
}
states[i] = state
}
<-eventsDone
// create a snapshot
snap, err := client.CreateSnapshot()
if err != nil {
@ -634,9 +665,23 @@ func TestHTTPSnapshot(t *testing.T) {
}
// create another network
_, s = testHTTPServer(t)
network2, s := testHTTPServer(t)
defer s.Close()
client = NewClient(s.URL)
count = 1
eventSub = network2.Events().Subscribe(eventsDoneChan)
go func() {
defer eventSub.Unsubscribe()
for event := range eventsDoneChan {
if event.Type == EventTypeConn && !event.Control {
count--
if count == 0 {
eventsDone <- struct{}{}
return
}
}
}
}()
// subscribe to events so we can check them later
events := make(chan *Event, 100)
@ -651,6 +696,7 @@ func TestHTTPSnapshot(t *testing.T) {
if err := client.LoadSnapshot(snap); err != nil {
t.Fatalf("error loading snapshot: %s", err)
}
<-eventsDone
// check the nodes and connection exists
net, err := client.GetNetwork()
@ -676,6 +722,9 @@ func TestHTTPSnapshot(t *testing.T) {
if conn.Other.String() != nodes[1].ID {
t.Fatalf("expected connection to have other=%q, got other=%q", nodes[1].ID, conn.Other)
}
if !conn.Up {
t.Fatal("should be up")
}
// check the node states were restored
for i, node := range nodes {