Skip to content

Commit

Permalink
Failing test case
Browse files Browse the repository at this point in the history
If chunkSize doesn't divide contentSize evenly, and chunkSize < 0.5*contentSize,
we try to download a chunk bigger than chunkSize.  This should not be possible.
  • Loading branch information
philandstuff committed Mar 5, 2024
1 parent efe1ac0 commit 31bc26d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/download/buffer_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newTestServer(t *testing.T, content []byte) *httptest.Server {
// TODO: Implement the test
// func TestGetFileSizeFromContentRange(t *testing.T) {}
func TestFileToBufferChunkCountExceedsMaxChunks(t *testing.T) {
contentSize := int64(humanize.KByte)
contentSize := int64(humanize.KiByte)
content := generateTestContent(contentSize)
server := newTestServer(t, content)
defer server.Close()
Expand Down Expand Up @@ -98,9 +98,9 @@ func TestFileToBufferChunkCountExceedsMaxChunks(t *testing.T) {
maxConcurrency: 2,
},
{
// humanize.KByte = 1024, remainder will result in 1024/10 = 102 chunks, max-chunks is set to 25
// humanize.KByte = 1024, remainder will result in 1024/10 = 102 chunks, concurrency is set to 25
// resulting in a chunkSize of 41
name: "many chunks, low maxChunks",
name: "many chunks, low maxConcurrency",
chunkSize: 10,
maxConcurrency: 25,
},
Expand Down
5 changes: 5 additions & 0 deletions pkg/download/buffered_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func (b *bufferedReader) done() {

func (b *bufferedReader) downloadBody(resp *http.Response) error {
expectedBytes := resp.ContentLength

if expectedBytes > int64(b.buf.Cap()) {
b.err = fmt.Errorf("Tried to download 0x%x bytes to a 0x%x-sized buffer", expectedBytes, b.buf.Cap())
return b.err
}
n, err := b.buf.ReadFrom(resp.Body)
if err != nil && err != io.EOF {
b.err = fmt.Errorf("error reading response for %s: %w", resp.Request.URL.String(), err)
Expand Down

0 comments on commit 31bc26d

Please sign in to comment.