Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reports already downloaded bytes as downloaded #47

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading