gitlab.com/jokerrs1/Sia@v1.3.2/siatest/renter/renter_test.go (about)

     1  package renter
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  
     7  	"github.com/NebulousLabs/Sia/modules"
     8  	"github.com/NebulousLabs/Sia/siatest"
     9  	"github.com/NebulousLabs/fastrand"
    10  	"github.com/NebulousLabs/ratelimit"
    11  )
    12  
    13  // TestRenter executes a number of subtests using the same TestGroup to
    14  // save time on initialization
    15  func TestRenter(t *testing.T) {
    16  	if testing.Short() {
    17  		t.SkipNow()
    18  	}
    19  
    20  	// Create a group for the subtests
    21  	groupParams := siatest.GroupParams{
    22  		Hosts:   5,
    23  		Renters: 1,
    24  		Miners:  1,
    25  	}
    26  	tg, err := siatest.NewGroupFromTemplate(groupParams)
    27  	if err != nil {
    28  		t.Fatal("Failed to create group: ", err)
    29  	}
    30  	defer func() {
    31  		if err := tg.Close(); err != nil {
    32  			t.Fatal(err)
    33  		}
    34  	}()
    35  
    36  	// Specify subtests to run
    37  	subTests := []struct {
    38  		name string
    39  		test func(*testing.T, *siatest.TestGroup)
    40  	}{
    41  		{"UploadDownload", testUploadDownload},
    42  		{"DownloadMultipleLargeSectors", testDownloadMultipleLargeSectors},
    43  	}
    44  	// Run subtests
    45  	for _, subtest := range subTests {
    46  		t.Run(subtest.name, func(t *testing.T) {
    47  			subtest.test(t, tg)
    48  		})
    49  	}
    50  }
    51  
    52  // testUploadDownload is a subtest that uses an existing TestGroup to test if
    53  // uploading and downloading a file works
    54  func testUploadDownload(t *testing.T, tg *siatest.TestGroup) {
    55  	// Grab the first of the group's renters
    56  	renter := tg.Renters()[0]
    57  	// Upload file, creating a piece for each host in the group
    58  	dataPieces := uint64(1)
    59  	parityPieces := uint64(len(tg.Hosts())) - dataPieces
    60  	remoteFile, err := renter.UploadNewFileBlocking(100+siatest.Fuzz(), dataPieces, parityPieces)
    61  	if err != nil {
    62  		t.Fatal("Failed to upload a file for testing: ", err)
    63  	}
    64  	// Download the file synchronously directly into memory
    65  	_, err = renter.DownloadByStream(remoteFile)
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	// Download the file synchronously to a file on disk
    70  	_, err = renter.DownloadToDisk(remoteFile, false)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	// Download the file asynchronously and wait for the download to finish.
    75  	localFile, err := renter.DownloadToDisk(remoteFile, true)
    76  	if err != nil {
    77  		t.Error(err)
    78  	}
    79  	if err := renter.WaitForDownload(localFile, remoteFile); err != nil {
    80  		t.Error(err)
    81  	}
    82  }
    83  
    84  // testDownloadMultipleLargeSectors downloads multiple large files (>5 Sectors)
    85  // in parallel and makes sure that the downloads are blocking each other.
    86  func testDownloadMultipleLargeSectors(t *testing.T, tg *siatest.TestGroup) {
    87  	// parallelDownloads is the number of downloads that are run in parallel.
    88  	parallelDownloads := 10
    89  	// fileSize is the size of the downloaded file.
    90  	fileSize := int(10*modules.SectorSize) + siatest.Fuzz()
    91  	// set download limits and reset them after test.
    92  	ratelimit.SetLimits(int64(fileSize)*2, 0, modules.SectorSize)
    93  	defer ratelimit.SetLimits(0, 0, 0)
    94  	// uniqueRemoteFiles is the number of files that will be uploaded to the
    95  	// network. Downloads will choose the remote file to download randomly.
    96  	uniqueRemoteFiles := 5
    97  	// Grab the first of the group's renters
    98  	renter := tg.Renters()[0]
    99  
   100  	// Upload files
   101  	dataPieces := uint64(len(tg.Hosts())) - 1
   102  	parityPieces := uint64(1)
   103  	remoteFiles := make([]*siatest.RemoteFile, 0, uniqueRemoteFiles)
   104  	for i := 0; i < uniqueRemoteFiles; i++ {
   105  		remoteFile, err := renter.UploadNewFileBlocking(fileSize, dataPieces, parityPieces)
   106  		if err != nil {
   107  			t.Fatal("Failed to upload a file for testing: ", err)
   108  		}
   109  		remoteFiles = append(remoteFiles, remoteFile)
   110  	}
   111  
   112  	// Randomly download using download to file and download to stream methods.
   113  	wg := new(sync.WaitGroup)
   114  	for i := 0; i < parallelDownloads; i++ {
   115  		wg.Add(1)
   116  		go func() {
   117  			var err error
   118  			var rf = remoteFiles[fastrand.Intn(len(remoteFiles))]
   119  			if fastrand.Intn(2) == 0 {
   120  				_, err = renter.DownloadByStream(rf)
   121  			} else {
   122  				_, err = renter.DownloadToDisk(rf, false)
   123  			}
   124  			if err != nil {
   125  				t.Error("Download failed:", err)
   126  			}
   127  			wg.Done()
   128  		}()
   129  	}
   130  	wg.Wait()
   131  }