From 15ba8259db95b7c3b28c161cf99f3353892eb484 Mon Sep 17 00:00:00 2001 From: Vlad Date: Wed, 13 Mar 2019 00:18:30 +0400 Subject: [PATCH] swarm/pss: tests added --- swarm/network/simulation/kademlia_test.go | 78 +++++++++++++++++++++++ swarm/pss/prox_test.go | 2 +- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/swarm/network/simulation/kademlia_test.go b/swarm/network/simulation/kademlia_test.go index 4cfcecd8ef..4d9bab3c0f 100644 --- a/swarm/network/simulation/kademlia_test.go +++ b/swarm/network/simulation/kademlia_test.go @@ -144,3 +144,81 @@ func createSimServiceMap(discovery bool) map[string]ServiceFunc { }, } } + +func TestWaitTillSnapshotRecreated(t *testing.T) { + var err error + sim := New(createSimServiceMap(true)) + _, err = sim.AddNodesAndConnectRing(16) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + defer cancel() + _, err = sim.WaitTillHealthy(ctx) + if err != nil { + t.Fatal(err) + } + + originalConnections := sim.getActualConnections() + snap, err := sim.Net.Snapshot() + sim.Close() + if err != nil { + t.Fatal(err) + } + + controlSim := New(createSimServiceMap(false)) + defer controlSim.Close() + err = controlSim.Net.Load(snap) + if err != nil { + t.Fatal(err) + } + err = controlSim.WaitTillSnapshotRecreated(ctx, *snap) + if err != nil { + t.Fatal(err) + } + controlConnections := controlSim.getActualConnections() + + for _, c := range originalConnections { + if !exist(controlConnections, c) { + t.Fatal("connection was not recreated") + } + } +} + +func exist(arr []uint64, val uint64) bool { + for _, c := range arr { + if c == val { + return true + } + } + return false +} + +func TestRemoveDuplicatesAndSingletons(t *testing.T) { + singletons := []uint64{0x3c127c6f6cb026b0, 0x0f45190d72e71fc5, 0xb0184c02449e0bb6, 0xa85c7b84239c54d3, 0xe3b0c44298fc1c14, 0x9afbf4c8996fb924, 0x27ae41e4649b934c, 0xa495991b7852b855} + doubles := []uint64{0x1b879f878de7fc7a, 0xc6791470521bdab4, 0xdd34b0ee39bbccc6, 0x4d904fbf0f31da10, 0x6403c2560432c8f8, 0x18954e33cf3ad847, 0x90db00e98dc7a8a6, 0x92886b0dfcc1809b} + var arr []uint64 + arr = append(arr, doubles...) + arr = append(arr, singletons...) + arr = append(arr, doubles...) + arr = removeDuplicatesAndSingletons(arr) + + for _, i := range singletons { + if exist(arr, i) { + t.Fatalf("singleton not removed: %d", i) + } + } + + for _, i := range doubles { + if !exist(arr, i) { + t.Fatalf("wrong value removed: %d", i) + } + } + + for j := 0; j < len(doubles); j++ { + v := doubles[j] + singletons[j] + if exist(arr, v) { + t.Fatalf("non-existing value found, index: %d", j) + } + } +} diff --git a/swarm/pss/prox_test.go b/swarm/pss/prox_test.go index e87d1aa6ee..74b8a28df7 100644 --- a/swarm/pss/prox_test.go +++ b/swarm/pss/prox_test.go @@ -218,7 +218,7 @@ func testProxNetwork(t *testing.T) { if err != nil { t.Fatal(err) } - ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) // todo: increase before commit + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() snap := readSnapshot(t, nodeCount) err = tstdata.sim.WaitTillSnapshotRecreated(ctx, snap)