mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
fixed http snapshot test
This commit is contained in:
parent
5ec709a338
commit
a07d55baa6
2 changed files with 52 additions and 138 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -18,124 +18,14 @@ package simulations
|
|||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
var loglevel = flag.Int("loglevel", 2, "verbosity of logs")
|
||||
|
||||
func init() {
|
||||
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
|
||||
}
|
||||
|
||||
//TestSnapshotExplicit tests that nodes connect and disconnect properly
|
||||
//and that the exposed services are as perscribed
|
||||
func TestSnapshotExplicit(t *testing.T) {
|
||||
adapter := adapters.NewSimAdapter(adapters.Services{
|
||||
"dummy": newDummyService,
|
||||
"dummy2": newDummy2Service,
|
||||
})
|
||||
network := NewNetwork(adapter, &NetworkConfig{
|
||||
DefaultService: "dummy",
|
||||
})
|
||||
defer network.Shutdown()
|
||||
nodeCount := 3
|
||||
ids := make([]enode.ID, nodeCount)
|
||||
for i := 0; i < nodeCount; i++ {
|
||||
conf := adapters.RandomNodeConfig()
|
||||
conf.Services = []string{"dummy", "dummy2"}
|
||||
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()
|
||||
}
|
||||
var eventsDone = make(chan struct{})
|
||||
count := 2
|
||||
events := make(chan *Event)
|
||||
sub := network.Events().Subscribe(events)
|
||||
go func() {
|
||||
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()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(snap.Conns) > 1 {
|
||||
t.Fatalf("expected one connect object")
|
||||
}
|
||||
for _, svc := range snap.Nodes[0].Node.Config.Services {
|
||||
if svc != "dummy" && svc != "dummy2" {
|
||||
t.Fatalf("unexpected service %s", svc)
|
||||
}
|
||||
}
|
||||
|
||||
snap, err = network.SnapshotWithServices([]string{"bzz"}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, svc := range snap.Nodes[0].Node.Config.Services {
|
||||
if svc != "dummy" && svc != "dummy2" && svc != "bzz" {
|
||||
t.Fatalf("unexpected service %s", svc)
|
||||
}
|
||||
}
|
||||
|
||||
snap, err = network.SnapshotWithServices([]string{"bzz"}, []string{"dummy2"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, svc := range snap.Nodes[0].Node.Config.Services {
|
||||
if svc != "dummy" && svc != "bzz" {
|
||||
t.Fatalf("unexpected service %s", svc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
@ -268,28 +158,3 @@ func triggerChecks(ctx context.Context, ids []enode.ID, trigger chan enode.ID, i
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
type dummyService struct {
|
||||
}
|
||||
type dummy2Service struct {
|
||||
dummyService
|
||||
}
|
||||
|
||||
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{}
|
||||
}
|
||||
func (p *dummyService) Protocols() []p2p.Protocol {
|
||||
return []p2p.Protocol{}
|
||||
}
|
||||
func (p *dummyService) Start(server *p2p.Server) error {
|
||||
return nil
|
||||
}
|
||||
func (p *dummyService) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue