swarm/network: expose simulation RandomUpNode to use in stream tests

This commit is contained in:
Janos Guljas 2018-07-20 13:25:16 +02:00 committed by Fabio Barone
parent b7c333faa6
commit bc42faee0a
6 changed files with 23 additions and 31 deletions

View file

@ -54,7 +54,7 @@ func (s *Simulation) ConnectToLastNode(id discover.NodeID) (err error) {
// ConnectToRandomNode connects the node with provieded NodeID // ConnectToRandomNode connects the node with provieded NodeID
// to a random node that is up. // to a random node that is up.
func (s *Simulation) ConnectToRandomNode(id discover.NodeID) (err error) { func (s *Simulation) ConnectToRandomNode(id discover.NodeID) (err error) {
n := s.randomUpNode(id) n := s.RandomUpNode(id)
if n == nil { if n == nil {
return ErrNodeNotFound return ErrNodeNotFound
} }

View file

@ -294,7 +294,7 @@ func (s *Simulation) StopNode(id discover.NodeID) (err error) {
// StopRandomNode stops a random node. // StopRandomNode stops a random node.
func (s *Simulation) StopRandomNode() (id discover.NodeID, err error) { func (s *Simulation) StopRandomNode() (id discover.NodeID, err error) {
n := s.randomUpNode() n := s.RandomUpNode()
if n == nil { if n == nil {
return id, ErrNodeNotFound return id, ErrNodeNotFound
} }
@ -324,18 +324,18 @@ func init() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
} }
// randomUpNode returns a random SimNode that is up. // RandomUpNode returns a random SimNode that is up.
// Arguments are NodeIDs for nodes that should not be returned. // Arguments are NodeIDs for nodes that should not be returned.
func (s *Simulation) randomUpNode(exclude ...discover.NodeID) *adapters.SimNode { func (s *Simulation) RandomUpNode(exclude ...discover.NodeID) *adapters.SimNode {
return s.randomNode(s.UpNodeIDs(), exclude...) return s.randomNode(s.UpNodeIDs(), exclude...)
} }
// randomUpNode returns a random SimNode that is not up. // randomDownNode returns a random SimNode that is not up.
func (s *Simulation) randomDownNode(exclude ...discover.NodeID) *adapters.SimNode { func (s *Simulation) randomDownNode(exclude ...discover.NodeID) *adapters.SimNode {
return s.randomNode(s.DownNodeIDs(), exclude...) return s.randomNode(s.DownNodeIDs(), exclude...)
} }
// randomUpNode returns a random SimNode from the slice of NodeIDs. // randomNode returns a random SimNode from the slice of NodeIDs.
func (s *Simulation) randomNode(ids []discover.NodeID, exclude ...discover.NodeID) *adapters.SimNode { func (s *Simulation) randomNode(ids []discover.NodeID, exclude ...discover.NodeID) *adapters.SimNode {
for _, e := range exclude { for _, e := range exclude {
var i int var i int

View file

@ -39,7 +39,7 @@ func (s *Simulation) Service(name string, id discover.NodeID) node.Service {
// RandomService returns a single Service by name on a // RandomService returns a single Service by name on a
// randomly chosen node that is up. // randomly chosen node that is up.
func (s *Simulation) RandomService(name string) node.Service { func (s *Simulation) RandomService(name string) node.Service {
n := s.randomUpNode() n := s.RandomUpNode()
if n == nil { if n == nil {
return nil return nil
} }

View file

@ -384,6 +384,10 @@ func uploadFilesToNodes(sim *simulation.Simulation) ([]storage.Address, []string
return rootAddrs, rfiles, nil return rootAddrs, rfiles, nil
} }
func init() {
rand.Seed(time.Now().UnixNano())
}
//generate a random file (string) //generate a random file (string)
func generateRandomFile() (string, error) { func generateRandomFile() (string, error) {
//generate a random file size between minFileSize and maxFileSize //generate a random file size between minFileSize and maxFileSize

View file

@ -18,7 +18,6 @@ package stream
import ( import (
"context" "context"
"fmt" "fmt"
"math/rand"
"os" "os"
"sync" "sync"
"testing" "testing"
@ -343,17 +342,15 @@ func runRetrievalTest(chunkCount int, nodeCount int) error {
//uploadFinished := make(chan struct{}) //uploadFinished := make(chan struct{})
//channel to trigger new node checks //channel to trigger new node checks
//select one index at random... //get the the node at that index
idx := rand.Intn(len(nodeIDs))
//...and get the the node at that index
//this is the node selected for upload //this is the node selected for upload
node := nodeIDs[idx] node := sim.RandomUpNode()
item, ok := sim.NodeItem(node, bucketKeyStore) item, ok := sim.NodeItem(node.ID, bucketKeyStore)
if !ok { if !ok {
return fmt.Errorf("No localstore") return fmt.Errorf("No localstore")
} }
lstore := item.(*storage.LocalStore) lstore := item.(*storage.LocalStore)
conf.hashes, err = uploadFileToSingleNodeStore(node, chunkCount, lstore) conf.hashes, err = uploadFileToSingleNodeStore(node.ID, chunkCount, lstore)
if err != nil { if err != nil {
return err return err
} }

View file

@ -20,7 +20,6 @@ import (
crand "crypto/rand" crand "crypto/rand"
"fmt" "fmt"
"io" "io"
"math/rand"
"os" "os"
"sync" "sync"
"testing" "testing"
@ -56,10 +55,6 @@ type synctestConfig struct {
addrToIdMap map[string]discover.NodeID addrToIdMap map[string]discover.NodeID
} }
func init() {
rand.Seed(time.Now().Unix())
}
//This test is a syncing test for nodes. //This test is a syncing test for nodes.
//One node is randomly selected to be the pivot node. //One node is randomly selected to be the pivot node.
//A configurable number of chunks and nodes can be //A configurable number of chunks and nodes can be
@ -191,17 +186,15 @@ func testSyncing(t *testing.T, chunkCount int, nodeCount int) {
conf.addrToIdMap[string(a)] = n conf.addrToIdMap[string(a)] = n
} }
//select one index at random... //get the the node at that index
idx := rand.Intn(len(nodeIDs))
//...and get the the node at that index
//this is the node selected for upload //this is the node selected for upload
node := nodeIDs[idx] node := sim.RandomUpNode()
item, ok := sim.NodeItem(node, bucketKeyStore) item, ok := sim.NodeItem(node.ID, bucketKeyStore)
if !ok { if !ok {
return fmt.Errorf("No localstore") return fmt.Errorf("No localstore")
} }
lstore := item.(*storage.LocalStore) lstore := item.(*storage.LocalStore)
hashes, err := uploadFileToSingleNodeStore(node, chunkCount, lstore) hashes, err := uploadFileToSingleNodeStore(node.ID, chunkCount, lstore)
if err != nil { if err != nil {
return err return err
} }
@ -403,17 +396,15 @@ func runSyncTest(chunkCount int, nodeCount int) error {
} }
} }
}() }()
//select one index at random... //get the the node at that index
idx := rand.Intn(len(nodeIDs))
//...and get the the node at that index
//this is the node selected for upload //this is the node selected for upload
node := nodeIDs[idx] node := sim.RandomUpNode()
item, ok := sim.NodeItem(node, bucketKeyStore) item, ok := sim.NodeItem(node.ID, bucketKeyStore)
if !ok { if !ok {
return fmt.Errorf("No localstore") return fmt.Errorf("No localstore")
} }
lstore := item.(*storage.LocalStore) lstore := item.(*storage.LocalStore)
hashes, err := uploadFileToSingleNodeStore(node, chunkCount, lstore) hashes, err := uploadFileToSingleNodeStore(node.ID, chunkCount, lstore)
if err != nil { if err != nil {
return err return err
} }