gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/renter/download_test.go (about)

     1  package renter
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"gitlab.com/SiaPrime/SiaPrime/types"
     8  )
     9  
    10  // TestClearDownloads tests all the edge cases of the ClearDownloadHistory Method
    11  func TestClearDownloads(t *testing.T) {
    12  	if testing.Short() {
    13  		t.SkipNow()
    14  	}
    15  	rt, err := newRenterTester(t.Name())
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	defer rt.Close()
    20  
    21  	// Test clearing empty download history
    22  	if err := rt.renter.ClearDownloadHistory(time.Time{}, time.Time{}); err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	// Check Clearing individual download from history
    27  	// doesn't exist - before
    28  	length, err := clearDownloadHistory(rt, 1, 1)
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	if len(rt.renter.downloadHistory) != length {
    33  		t.Fatal("Download should not have been cleared")
    34  	}
    35  	// doesn't exist - after
    36  	length, err = clearDownloadHistory(rt, 10, 10)
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	if len(rt.renter.downloadHistory) != length {
    41  		t.Fatal("Download should not have been cleared")
    42  	}
    43  	// doesn't exist - within range
    44  	length, err = clearDownloadHistory(rt, 5, 5)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	if len(rt.renter.downloadHistory) != length {
    49  		t.Fatal("Download should not have been cleared")
    50  	}
    51  	// Remove Last Download
    52  	length, err = clearDownloadHistory(rt, 9, 9)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	if len(rt.renter.downloadHistory) != length-1 {
    57  		t.Fatal("Download should have been cleared")
    58  	}
    59  	if rt.renter.downloadHistory[length-2].staticStartTime.Unix() == 9 {
    60  		t.Fatal("Download not removed")
    61  	}
    62  	// Remove First Download
    63  	length, err = clearDownloadHistory(rt, 2, 2)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	if len(rt.renter.downloadHistory) != length-1 {
    68  		t.Fatal("Download should have been cleared")
    69  	}
    70  	if rt.renter.downloadHistory[0].staticStartTime.Unix() == 2 {
    71  		t.Fatal("Download not removed")
    72  	}
    73  	// Remove download from middle of history
    74  	length, err = clearDownloadHistory(rt, 6, 6)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	if len(rt.renter.downloadHistory) != length-1 {
    79  		t.Fatal("Download should have been cleared")
    80  	}
    81  	for _, d := range rt.renter.downloadHistory {
    82  		if d.staticStartTime.Unix() == 6 {
    83  			t.Fatal("Download not removed")
    84  		}
    85  	}
    86  
    87  	// Check Clearing range
    88  	// both exist - first and last
    89  	_, err = clearDownloadHistory(rt, 2, 9)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	if len(rt.renter.downloadHistory) != 0 {
    94  		t.Fatal("Download history should have been cleared")
    95  	}
    96  	// both exist - within range
    97  	_, err = clearDownloadHistory(rt, 3, 8)
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	if !checkDownloadHistory(rt.renter.downloadHistory, []int64{2, 9}) {
   102  		t.Fatal("Download history not cleared as expected")
   103  	}
   104  	// exist - within range and doesn't exist - before
   105  	_, err = clearDownloadHistory(rt, 1, 4)
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  	if !checkDownloadHistory(rt.renter.downloadHistory, []int64{6, 8, 9}) {
   110  		t.Fatal("Download history not cleared as expected")
   111  	}
   112  	// exist - within range and doesn't exist - after
   113  	_, err = clearDownloadHistory(rt, 6, 10)
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	if !checkDownloadHistory(rt.renter.downloadHistory, []int64{2, 3, 4}) {
   118  		t.Fatal("Download history not cleared as expected")
   119  	}
   120  	// neither exist - within range and before
   121  	_, err = clearDownloadHistory(rt, 1, 5)
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  	if !checkDownloadHistory(rt.renter.downloadHistory, []int64{6, 8, 9}) {
   126  		t.Fatal("Download history not cleared as expected")
   127  	}
   128  	// neither exist - within range and after
   129  	_, err = clearDownloadHistory(rt, 5, 10)
   130  	if err != nil {
   131  		t.Fatal(err)
   132  	}
   133  	if !checkDownloadHistory(rt.renter.downloadHistory, []int64{2, 3, 4}) {
   134  		t.Fatal("Download history not cleared as expected")
   135  	}
   136  	// neither exist - outside
   137  	_, err = clearDownloadHistory(rt, 1, 10)
   138  	if err != nil {
   139  		t.Fatal(err)
   140  	}
   141  	if len(rt.renter.downloadHistory) != 0 {
   142  		t.Fatal("Download history should have been cleared")
   143  	}
   144  	// neither exist - inside
   145  	_, err = clearDownloadHistory(rt, 5, 7)
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  	if !checkDownloadHistory(rt.renter.downloadHistory, []int64{2, 3, 4, 8, 9}) {
   150  		t.Fatal("Download history not cleared as expected")
   151  	}
   152  
   153  	// Check Clear Before
   154  	// exists - within range
   155  	_, err = clearDownloadHistory(rt, 0, 6)
   156  	if err != nil {
   157  		t.Fatal(err)
   158  	}
   159  	if !checkDownloadHistory(rt.renter.downloadHistory, []int64{8, 9}) {
   160  		t.Fatal("Download history not cleared as expected")
   161  	}
   162  	// exists - last
   163  	_, err = clearDownloadHistory(rt, 0, 9)
   164  	if err != nil {
   165  		t.Fatal(err)
   166  	}
   167  	if len(rt.renter.downloadHistory) != 0 {
   168  		t.Fatal("Download history should have been cleared")
   169  	}
   170  	// doesn't exist - within range
   171  	_, err = clearDownloadHistory(rt, 0, 7)
   172  	if err != nil {
   173  		t.Fatal(err)
   174  	}
   175  	if !checkDownloadHistory(rt.renter.downloadHistory, []int64{8, 9}) {
   176  		t.Fatal("Download history not cleared as expected")
   177  	}
   178  	// doesn't exist - before
   179  	length, err = clearDownloadHistory(rt, 0, 1)
   180  	if err != nil {
   181  		t.Fatal(err)
   182  	}
   183  	if len(rt.renter.downloadHistory) != length {
   184  		t.Fatal("No downloads should not have been cleared")
   185  	}
   186  	// doesn't exist - after
   187  	_, err = clearDownloadHistory(rt, 0, 10)
   188  	if err != nil {
   189  		t.Fatal(err)
   190  	}
   191  	if len(rt.renter.downloadHistory) != 0 {
   192  		t.Fatal("Download history should have been cleared")
   193  	}
   194  
   195  	// Check Clear After
   196  	// exists - within range
   197  	_, err = clearDownloadHistory(rt, 6, 0)
   198  	if err != nil {
   199  		t.Fatal(err)
   200  	}
   201  	if !checkDownloadHistory(rt.renter.downloadHistory, []int64{2, 3, 4}) {
   202  		t.Fatal("Download history not cleared as expected")
   203  	}
   204  	// exist - first
   205  	_, err = clearDownloadHistory(rt, 2, 0)
   206  	if err != nil {
   207  		t.Fatal(err)
   208  	}
   209  	if len(rt.renter.downloadHistory) != 0 {
   210  		t.Fatal("Download history should have been cleared")
   211  	}
   212  	// doesn't exist - within range
   213  	_, err = clearDownloadHistory(rt, 5, 0)
   214  	if err != nil {
   215  		t.Fatal(err)
   216  	}
   217  	if !checkDownloadHistory(rt.renter.downloadHistory, []int64{2, 3, 4}) {
   218  		t.Fatal("Download history not cleared as expected")
   219  	}
   220  	// doesn't exist - after
   221  	length, err = clearDownloadHistory(rt, 10, 0)
   222  	if err != nil {
   223  		t.Fatal(err)
   224  	}
   225  	if len(rt.renter.downloadHistory) != length {
   226  		t.Fatal("No downloads should not have been cleared")
   227  	}
   228  	// doesn't exist - before
   229  	_, err = clearDownloadHistory(rt, 1, 0)
   230  	if err != nil {
   231  		t.Fatal(err)
   232  	}
   233  	if len(rt.renter.downloadHistory) != 0 {
   234  		t.Fatal("Download history should have been cleared")
   235  	}
   236  
   237  	// Check clearing entire download history
   238  	_, err = clearDownloadHistory(rt, 0, 0)
   239  	if err != nil {
   240  		t.Fatal(err)
   241  	}
   242  	if len(rt.renter.downloadHistory) != 0 {
   243  		t.Fatal("Download History not cleared")
   244  	}
   245  }
   246  
   247  // clearDownloadHistory is a helper function for TestClearDownloads, it builds and resets the download
   248  // history of the renter and then calls ClearDownloadHistory and returns the length
   249  // of the original download history
   250  func clearDownloadHistory(rt *renterTester, after, before int) (int, error) {
   251  	// Build/Reset download History
   252  	// Skipping 5 and 7 so there are clear times missing that can
   253  	// be referenced
   254  	rt.renter.downloadHistoryMu.Lock()
   255  	downloads := []*download{}
   256  	for i := 2; i < 10; i++ {
   257  		if i != 5 && i != 7 {
   258  			d := &download{
   259  				staticStartTime: time.Unix(int64(i), 0),
   260  			}
   261  			downloads = append(downloads, d)
   262  		}
   263  	}
   264  	rt.renter.downloadHistory = downloads
   265  	length := len(rt.renter.downloadHistory)
   266  	rt.renter.downloadHistoryMu.Unlock()
   267  
   268  	// clear download history
   269  	var afterTime time.Time
   270  	beforeTime := types.EndOfTime
   271  	if before != 0 {
   272  		beforeTime = time.Unix(int64(before), 0)
   273  	}
   274  	if after != 0 {
   275  		afterTime = time.Unix(int64(after), 0)
   276  	}
   277  	if err := rt.renter.ClearDownloadHistory(afterTime, beforeTime); err != nil {
   278  		return 0, err
   279  	}
   280  	return length, nil
   281  }
   282  
   283  // checkDownloadHistory is a helper function for TestClearDownloads
   284  // it compares the renter's download history against what is expected
   285  // after ClearDownloadHistory is called
   286  func checkDownloadHistory(downloads []*download, check []int64) bool {
   287  	if downloads == nil && check == nil {
   288  		return true
   289  	}
   290  	if downloads == nil || check == nil {
   291  		return false
   292  	}
   293  	if len(downloads) != len(check) {
   294  		return false
   295  	}
   296  	for i := range downloads {
   297  		if downloads[i].staticStartTime.Unix() != check[i] {
   298  			return false
   299  		}
   300  	}
   301  	return true
   302  }