swarm/network/simulation: test cases refactored

This commit is contained in:
Vlad 2019-03-18 16:12:35 +04:00
parent acebccc3bf
commit a198ac7a08
5 changed files with 49 additions and 33 deletions

View file

@ -20,6 +20,9 @@ import (
"context"
"encoding/binary"
"encoding/hex"
"encoding/json"
"io/ioutil"
"os"
"time"
"github.com/ethereum/go-ethereum/common"
@ -103,7 +106,7 @@ func (s *Simulation) kademlias() (ks map[enode.ID]*network.Kademlia) {
// in the snapshot are registered in the kademlia.
// It differs from WaitTillHealthy, which waits only until all the kademlias are
// healthy (it might happen even before all the connections are established).
func (s *Simulation) WaitTillSnapshotRecreated(ctx context.Context, snap simulations.Snapshot) error {
func (s *Simulation) WaitTillSnapshotRecreated(ctx context.Context, snap *simulations.Snapshot) error {
expected := getSnapshotConnections(snap.Conns)
ticker := time.NewTicker(150 * time.Millisecond)
defer ticker.Stop()
@ -201,3 +204,21 @@ func removeDuplicatesAndSingletons(arr []uint64) []uint64 {
return arr
}
func ReadSnapshot(filename string) (*simulations.Snapshot, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
jsonbyte, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
var snap simulations.Snapshot
err = json.Unmarshal(jsonbyte, &snap)
if err != nil {
return nil, err
}
return &snap, nil
}

View file

@ -182,7 +182,7 @@ func TestWaitTillSnapshotRecreated(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = controlSim.WaitTillSnapshotRecreated(ctx, *snap)
err = controlSim.WaitTillSnapshotRecreated(ctx, snap)
if err != nil {
t.Fatal(err)
}

View file

@ -22,8 +22,6 @@ import (
"testing"
"time"
"github.com/ethereum/go-ethereum/swarm/testutil"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
@ -31,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/swarm/network/simulation"
"github.com/ethereum/go-ethereum/swarm/state"
"github.com/ethereum/go-ethereum/swarm/storage"
"github.com/ethereum/go-ethereum/swarm/testutil"
)
//constants for random file generation
@ -155,7 +154,8 @@ func runFileRetrievalTest(nodeCount int) error {
//array where the generated chunk hashes will be stored
conf.hashes = make([]storage.Address, 0)
err := sim.UploadSnapshot(fmt.Sprintf("testing/snapshot_%d.json", nodeCount))
filename := fmt.Sprintf("testing/snapshot_%d.json", nodeCount)
err := sim.UploadSnapshot(filename)
if err != nil {
return err
}
@ -188,7 +188,11 @@ func runFileRetrievalTest(nodeCount int) error {
if err != nil {
return err
}
if _, err := sim.WaitTillHealthy(ctx); err != nil {
snap, err := simulation.ReadSnapshot(filename)
if err != nil {
return err
}
if err := sim.WaitTillSnapshotRecreated(ctx, snap); err != nil {
return err
}
@ -253,7 +257,8 @@ func runRetrievalTest(t *testing.T, chunkCount int, nodeCount int) error {
//array where the generated chunk hashes will be stored
conf.hashes = make([]storage.Address, 0)
err := sim.UploadSnapshot(fmt.Sprintf("testing/snapshot_%d.json", nodeCount))
filename := fmt.Sprintf("testing/snapshot_%d.json", nodeCount)
err := sim.UploadSnapshot(filename)
if err != nil {
return err
}
@ -283,7 +288,11 @@ func runRetrievalTest(t *testing.T, chunkCount int, nodeCount int) error {
if err != nil {
return err
}
if _, err := sim.WaitTillHealthy(ctx); err != nil {
snap, err := simulation.ReadSnapshot(filename)
if err != nil {
t.Fatalf("failed to read snapshot: %s", err)
}
if err := sim.WaitTillSnapshotRecreated(ctx, snap); err != nil {
return err
}

View file

@ -147,7 +147,8 @@ func testSyncingViaGlobalSync(t *testing.T, chunkCount int, nodeCount int) {
//array where the generated chunk hashes will be stored
conf.hashes = make([]storage.Address, 0)
err := sim.UploadSnapshot(fmt.Sprintf("testing/snapshot_%d.json", nodeCount))
filename := fmt.Sprintf("testing/snapshot_%d.json", nodeCount)
err := sim.UploadSnapshot(filename)
if err != nil {
t.Fatal(err)
}
@ -155,7 +156,11 @@ func testSyncingViaGlobalSync(t *testing.T, chunkCount int, nodeCount int) {
ctx, cancelSimRun := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancelSimRun()
if _, err := sim.WaitTillHealthy(ctx); err != nil {
snap, err := simulation.ReadSnapshot(filename)
if err != nil {
t.Fatalf("failed to read snapshot: %s", err)
}
if err := sim.WaitTillSnapshotRecreated(ctx, snap); err != nil {
t.Fatal(err)
}

View file

@ -3,11 +3,8 @@ package pss
import (
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"sync"
@ -20,7 +17,6 @@ import (
"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"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/swarm/network"
@ -105,24 +101,6 @@ func getCmdParams(t *testing.T) (int, int) {
return int(msgCount), int(nodeCount)
}
func readSnapshot(t *testing.T, nodeCount int) simulations.Snapshot {
f, err := os.Open(fmt.Sprintf("testdata/snapshot_%d.json", nodeCount))
if err != nil {
t.Fatal(err)
}
defer f.Close()
jsonbyte, err := ioutil.ReadAll(f)
if err != nil {
t.Fatal(err)
}
var snap simulations.Snapshot
err = json.Unmarshal(jsonbyte, &snap)
if err != nil {
t.Fatal(err)
}
return snap
}
func newTestData() *testData {
return &testData{
kademlias: make(map[enode.ID]*network.Kademlia),
@ -241,7 +219,10 @@ func testProxNetwork(t *testing.T) {
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*120)
defer cancel()
snap := readSnapshot(t, nodeCount)
snap, err := simulation.ReadSnapshot(fmt.Sprintf("testdata/snapshot_%d.json", nodeCount))
if err != nil {
t.Fatalf("failed to read snapshot: %s", err)
}
err = tstdata.sim.WaitTillSnapshotRecreated(ctx, snap)
if err != nil {
t.Fatalf("failed to recreate snapshot: %s", err)