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