swarm/network/stream: addressed PR comments

This commit is contained in:
Fabio Barone 2019-05-10 10:24:40 -05:00
parent 25831b3bf2
commit b2f91729c0

View file

@ -41,7 +41,7 @@ const (
maxFileSize = 40 maxFileSize = 40
) )
// This test is a retrieval test for nodes. // TestFileRetrieval is a retrieval test for nodes.
// A configurable number of nodes can be // A configurable number of nodes can be
// provided to the test. // provided to the test.
// Files are uploaded to nodes, other nodes try to retrieve the file // Files are uploaded to nodes, other nodes try to retrieve the file
@ -63,9 +63,7 @@ func TestFileRetrieval(t *testing.T) {
} }
for _, nc := range nodeCount { for _, nc := range nodeCount {
if err := runFileRetrievalTest(nc); err != nil { runFileRetrievalTest(t, nc)
t.Error(err)
}
} }
} }
@ -100,27 +98,22 @@ func TestPureRetrieval(t *testing.T) {
for _, nc := range nodeCount { for _, nc := range nodeCount {
for _, c := range chunkCount { for _, c := range chunkCount {
if err := runPureRetrievalTest(nc, c); err != nil { runPureRetrievalTest(t, nc, c)
t.Error(err)
}
} }
} }
} }
//This test is a retrieval test for nodes. // TestRetrieval tests retrieval of chunks by random 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
//provided to the test, the number of chunks is uploaded // provided to the test, the number of chunks is uploaded
//to the pivot node and other nodes try to retrieve the chunk(s). // to the pivot node and other nodes try to retrieve the chunk(s).
//Number of chunks and nodes can be provided via commandline too. // Number of chunks and nodes can be provided via commandline too.
func TestRetrieval(t *testing.T) { func TestRetrieval(t *testing.T) {
//if nodes/chunks have been provided via commandline, // if nodes/chunks have been provided via commandline,
//run the tests with these values // run the tests with these values
if *nodes != 0 && *chunks != 0 { if *nodes != 0 && *chunks != 0 {
err := runRetrievalTest(t, *chunks, *nodes) runRetrievalTest(t, *chunks, *nodes)
if err != nil {
t.Fatal(err)
}
} else { } else {
nodeCnt := []int{16} nodeCnt := []int{16}
chnkCnt := []int{32} chnkCnt := []int{32}
@ -136,10 +129,7 @@ func TestRetrieval(t *testing.T) {
for _, n := range nodeCnt { for _, n := range nodeCnt {
for _, c := range chnkCnt { for _, c := range chnkCnt {
t.Run(fmt.Sprintf("TestRetrieval_%d_%d", n, c), func(t *testing.T) { t.Run(fmt.Sprintf("TestRetrieval_%d_%d", n, c), func(t *testing.T) {
err := runRetrievalTest(t, c, n) runRetrievalTest(t, c, n)
if err != nil {
t.Fatal(err)
}
}) })
} }
} }
@ -176,7 +166,9 @@ var retrievalSimServiceMap = map[string]simulation.ServiceFunc{
// then starting a simulation, distribute chunks to nodes // then starting a simulation, distribute chunks to nodes
// and start retrieval. // and start retrieval.
// The snapshot should have 'streamer' in its service list. // The snapshot should have 'streamer' in its service list.
func runPureRetrievalTest(nodeCount int, chunkCount int) error { func runPureRetrievalTest(t *testing.T, nodeCount int, chunkCount int) {
t.Helper()
// the pure retrieval test needs a different service map, as we want // the pure retrieval test needs a different service map, as we want
// syncing disabled and we don't need to set the syncUpdateDelay // syncing disabled and we don't need to set the syncUpdateDelay
sim := simulation.New(map[string]simulation.ServiceFunc{ sim := simulation.New(map[string]simulation.ServiceFunc{
@ -187,7 +179,7 @@ func runPureRetrievalTest(nodeCount int, chunkCount int) error {
} }
r := NewRegistry(addr.ID(), delivery, netStore, state.NewInmemoryStore(), &RegistryOptions{ r := NewRegistry(addr.ID(), delivery, netStore, state.NewInmemoryStore(), &RegistryOptions{
Syncing: SyncingDisabled, // disable syncing Syncing: SyncingDisabled,
}, nil) }, nil)
cleanup = func() { cleanup = func() {
@ -217,7 +209,7 @@ func runPureRetrievalTest(nodeCount int, chunkCount int) error {
filename := fmt.Sprintf("testing/snapshot_%d.json", nodeCount) filename := fmt.Sprintf("testing/snapshot_%d.json", nodeCount)
err := sim.UploadSnapshot(ctx, filename) err := sim.UploadSnapshot(ctx, filename)
if err != nil { if err != nil {
return err t.Fatal(err)
} }
log.Info("Starting simulation") log.Info("Starting simulation")
@ -313,12 +305,17 @@ func runPureRetrievalTest(nodeCount int, chunkCount int) error {
log.Info("Simulation terminated") log.Info("Simulation terminated")
return result.Error if result.Error != nil {
t.Fatal(result.Error)
}
} }
// The test loads a snapshot file to construct the swarm network. // runFileRetrievalTest loads a snapshot file to construct the swarm network.
// The snapshot should have 'streamer' in its service list. // The snapshot should have 'streamer' in its service list.
func runFileRetrievalTest(nodeCount int) error { func runFileRetrievalTest(t *testing.T, nodeCount int) {
t.Helper()
sim := simulation.New(retrievalSimServiceMap) sim := simulation.New(retrievalSimServiceMap)
defer sim.Close() defer sim.Close()
@ -338,7 +335,7 @@ func runFileRetrievalTest(nodeCount int) error {
filename := fmt.Sprintf("testing/snapshot_%d.json", nodeCount) filename := fmt.Sprintf("testing/snapshot_%d.json", nodeCount)
err := sim.UploadSnapshot(ctx, filename) err := sim.UploadSnapshot(ctx, filename)
if err != nil { if err != nil {
return err t.Fatal(err)
} }
log.Info("Starting simulation") log.Info("Starting simulation")
@ -396,17 +393,17 @@ func runFileRetrievalTest(nodeCount int) error {
log.Info("Simulation terminated") log.Info("Simulation terminated")
if result.Error != nil { if result.Error != nil {
return result.Error t.Fatal(result.Error)
} }
return nil
} }
// The test generates the given number of chunks. // runRetrievalTest generates the given number of chunks.
// The test loads a snapshot file to construct the swarm network. // The test loads a snapshot file to construct the swarm network.
// The snapshot should have 'streamer' in its service list. // The snapshot should have 'streamer' in its service list.
func runRetrievalTest(t *testing.T, chunkCount int, nodeCount int) error { func runRetrievalTest(t *testing.T, chunkCount int, nodeCount int) {
t.Helper() t.Helper()
sim := simulation.New(retrievalSimServiceMap) sim := simulation.New(retrievalSimServiceMap)
defer sim.Close() defer sim.Close()
@ -424,7 +421,7 @@ func runRetrievalTest(t *testing.T, chunkCount int, nodeCount int) error {
filename := fmt.Sprintf("testing/snapshot_%d.json", nodeCount) filename := fmt.Sprintf("testing/snapshot_%d.json", nodeCount)
err := sim.UploadSnapshot(ctx, filename) err := sim.UploadSnapshot(ctx, filename)
if err != nil { if err != nil {
return err t.Fatal(err)
} }
result := sim.Run(ctx, func(ctx context.Context, sim *simulation.Simulation) error { result := sim.Run(ctx, func(ctx context.Context, sim *simulation.Simulation) error {
@ -482,8 +479,6 @@ func runRetrievalTest(t *testing.T, chunkCount int, nodeCount int) error {
}) })
if result.Error != nil { if result.Error != nil {
return result.Error t.Fatal(result.Error)
} }
return nil
} }