...

Source file src/code.rocketnine.space/tslocum/gophast/pkg/manager/download_test.go

Documentation: code.rocketnine.space/tslocum/gophast/pkg/manager

     1  package manager
     2  
     3  /* TODO Fix download interruption test
     4  const MaxInterrupts = 2
     5  
     6  type TestDownloadData struct {
     7  	DownloadSize   int64
     8  	MinSplitSize   int64
     9  	MaxConnections int64
    10  }
    11  
    12  var testDownloadData = []TestDownloadData{
    13  	{DownloadSize: 0, MinSplitSize: 0, MaxConnections: 1},
    14  	{DownloadSize: 0, MinSplitSize: 0, MaxConnections: 2},
    15  	{DownloadSize: 4096, MinSplitSize: 0, MaxConnections: 1},
    16  	{DownloadSize: 4096, MinSplitSize: 0, MaxConnections: 2},
    17  	{DownloadSize: 4194304, MinSplitSize: 0, MaxConnections: 1},
    18  	{DownloadSize: 4194304, MinSplitSize: 0, MaxConnections: 4}}
    19  
    20  var (
    21  	interrupt  bool
    22  	interrupts int
    23  )
    24  
    25  func TestDownload(t *testing.T) {
    26  	tempHostDir, err := ioutil.TempDir("", "gophast")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer os.RemoveAll(tempHostDir)
    31  
    32  	tempSaveDir, err := ioutil.TempDir("", "gophast")
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	defer os.RemoveAll(tempSaveDir)
    37  
    38  	SetDownloadDir(tempSaveDir)
    39  
    40  	tempFile, err := ioutil.TempFile(tempHostDir, "gophast")
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	defer tempFile.Close()
    45  
    46  	fileName := path.Base(tempFile.Name())
    47  
    48  	go func() {
    49  		http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    50  			if interrupt && interrupts < MaxInterrupts && len(r.Header.Get("Range")) > 6 {
    51  				interrupts++
    52  
    53  				sentRange := strings.SplitN(r.Header.Get("Range")[6:], "-", 2)
    54  				if len(sentRange) == 2 {
    55  					rangeStart, err := strconv.ParseInt(sentRange[0], 10, 64)
    56  					if err != nil {
    57  						t.Fatal(err)
    58  					}
    59  					rangeEnd, err := strconv.ParseInt(sentRange[1], 10, 64)
    60  					if err != nil {
    61  						t.Fatal(err)
    62  					}
    63  
    64  					r.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", rangeStart, rangeEnd-((rangeEnd-rangeStart)/2)))
    65  				}
    66  			}
    67  
    68  			http.FileServer(http.Dir(tempHostDir)).ServeHTTP(w, r)
    69  		})
    70  
    71  		t.Fatal(http.ListenAndServe("localhost:9001", nil))
    72  	}()
    73  	time.Sleep(100 * time.Millisecond)
    74  
    75  	var (
    76  		i int
    77  
    78  		hostedData []byte
    79  		savedData  []byte
    80  		status     int
    81  
    82  		downloadedFile     *os.File
    83  		downloadedFileInfo os.FileInfo
    84  	)
    85  	for {
    86  		i++
    87  		if i == 1 {
    88  			interrupt = false
    89  		} else if i == 2 {
    90  			interrupt = true
    91  		} else {
    92  			break
    93  		}
    94  
    95  		for _, testData := range testDownloadData {
    96  			interrupts = 0
    97  
    98  			SetMaxConnections(testData.MaxConnections)
    99  			SetMinSplitSize(testData.MinSplitSize)
   100  
   101  			// Write test file
   102  			hostedData = make([]byte, testData.DownloadSize)
   103  			_, err = rand.Read(hostedData)
   104  			if err != nil {
   105  				t.Fatal(err)
   106  			}
   107  
   108  			err = tempFile.Truncate(testData.DownloadSize)
   109  			if err != nil {
   110  				t.Fatal(err)
   111  			}
   112  
   113  			_, err = tempFile.Write(hostedData)
   114  			if err != nil {
   115  				t.Fatal(err)
   116  			}
   117  
   118  			_, err = tempFile.Seek(0, io.SeekStart)
   119  			if err != nil {
   120  				t.Fatal(err)
   121  			}
   122  
   123  			// Download
   124  			id, err := AddDownload(&NewDownloadInfo{URL: "http://localhost:9001/" + fileName, PostData: nil})
   125  			if err == nil {
   126  				if id <= 0 {
   127  					t.Fatal("Failed to initiate new download")
   128  				}
   129  
   130  				status, err = WaitDownload(id)
   131  			}
   132  			if err != nil {
   133  				t.Fatal(err)
   134  			} else if status <= 0 {
   135  				t.Fatal(errors.New(fmt.Sprintf("failed to download file: status %d", status)))
   136  			}
   137  
   138  			// Verify
   139  			downloadedFile, err = os.OpenFile(path.Join(tempSaveDir, fileName), os.O_RDONLY, 0644)
   140  			if err != nil {
   141  				t.Fatal(err)
   142  			}
   143  
   144  			downloadedFileInfo, err = downloadedFile.Stat()
   145  			if err != nil {
   146  				t.Fatal(err)
   147  			}
   148  
   149  			if downloadedFileInfo.Size() != testData.DownloadSize {
   150  				t.Fatalf("failed to download file: sizes do not match: expected %d, got %d", testData.DownloadSize, downloadedFileInfo.Size())
   151  			}
   152  
   153  			savedData, err = ioutil.ReadAll(downloadedFile)
   154  			if err != nil {
   155  				t.Fatal(err)
   156  			}
   157  
   158  			if len(hostedData) != len(savedData) {
   159  				t.Fatalf("failed to download file: data does not match: expected %d, got %d", len(hostedData), len(savedData))
   160  			}
   161  
   162  			err = downloadedFile.Close()
   163  			if err != nil {
   164  				t.Fatal(err)
   165  			}
   166  
   167  			err = os.Remove(path.Join(tempSaveDir, fileName))
   168  			if err != nil {
   169  				t.Fatal(err)
   170  			}
   171  
   172  			err = RemoveDownload(id)
   173  			if err != nil {
   174  				t.Fatal(err)
   175  			}
   176  		}
   177  	}
   178  
   179  	WaitAll()
   180  
   181  	err = Shutdown()
   182  	if err != nil {
   183  		t.Fatal(err)
   184  	}
   185  }
   186  */
   187  

View as plain text