From a198ac7a0843ace9b44219cb324a420be7b79588 Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 18 Mar 2019 16:12:35 +0400 Subject: [PATCH] swarm/network/simulation: test cases refactored --- swarm/network/simulation/kademlia.go | 23 +++++++++++++++- swarm/network/simulation/kademlia_test.go | 2 +- .../network/stream/snapshot_retrieval_test.go | 21 ++++++++++----- swarm/network/stream/snapshot_sync_test.go | 9 +++++-- swarm/pss/prox_test.go | 27 +++---------------- 5 files changed, 49 insertions(+), 33 deletions(-) diff --git a/swarm/network/simulation/kademlia.go b/swarm/network/simulation/kademlia.go index 4b880aa0ce..f828aba60b 100644 --- a/swarm/network/simulation/kademlia.go +++ b/swarm/network/simulation/kademlia.go @@ -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 +} diff --git a/swarm/network/simulation/kademlia_test.go b/swarm/network/simulation/kademlia_test.go index 9cbc39da5b..0ac1e78039 100644 --- a/swarm/network/simulation/kademlia_test.go +++ b/swarm/network/simulation/kademlia_test.go @@ -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) } diff --git a/swarm/network/stream/snapshot_retrieval_test.go b/swarm/network/stream/snapshot_retrieval_test.go index 2fdf8e9e37..4431242566 100644 --- a/swarm/network/stream/snapshot_retrieval_test.go +++ b/swarm/network/stream/snapshot_retrieval_test.go @@ -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 } diff --git a/swarm/network/stream/snapshot_sync_test.go b/swarm/network/stream/snapshot_sync_test.go index 9737ec0a54..5f6e88e185 100644 --- a/swarm/network/stream/snapshot_sync_test.go +++ b/swarm/network/stream/snapshot_sync_test.go @@ -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) } diff --git a/swarm/pss/prox_test.go b/swarm/pss/prox_test.go index 1c8538d50b..452fcc92d3 100644 --- a/swarm/pss/prox_test.go +++ b/swarm/pss/prox_test.go @@ -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)