Skip to content

Commit

Permalink
Merge pull request #47 from cuducos/fix-report-already-downloaded-bytes
Browse files Browse the repository at this point in the history
Reports already downloaded bytes as downloaded
  • Loading branch information
cuducos committed Aug 26, 2023
2 parents 9bb80b8 + bcd66f6 commit e62f47a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ func (d *Downloader) prepareAndStartDownload(ctx context.Context, url string, ch
return
}
if !pending {
s.DownloadedFileBytes = atomic.AddInt64(&downloadedBytes, c.size())
ch <- s
continue
}
urlDownload.Add(1)
Expand Down
33 changes: 33 additions & 0 deletions downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,39 @@ func TestDownload_Retry(t *testing.T) {
}
}

func TestDownload_ReportPreviouslyDownloadedBytes(t *testing.T) {
tmp := t.TempDir()
firstChunk := func(url string) DownloadStatus {
d := Downloader{
OutputDir: tmp,
MaxRetries: 1,
ConcurrencyPerServer: 1,
ChunkSize: 3,
}
ch := d.Download(url)
<-ch // discard the first status (just the file size)
return <-ch
}

// first download attempt (with server up)
url, first := func() (string, DownloadStatus) {
s := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "42")
},
))
defer s.Close()
got := firstChunk(s.URL)
return s.URL, got
}()

// second download attempt (with server down)
second := firstChunk(url)
if first.DownloadedFileBytes != second.DownloadedFileBytes {
t.Errorf("expected the same number of downloaded bytes, got %d and %d", first.DownloadedFileBytes, second.DownloadedFileBytes)
}
}

func TestDownloadWithContext_ErrorUserTimeout(t *testing.T) {
userTimeout := 250 * time.Millisecond // please note that the user timeout is less than the timeout per chunk.
timeout := 10 * userTimeout
Expand Down

0 comments on commit e62f47a

Please sign in to comment.