Skip to content

Commit

Permalink
Add testclient to check bug
Browse files Browse the repository at this point in the history
  • Loading branch information
corny committed Sep 4, 2023
1 parent 14b47a0 commit 488006d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,13 @@ func (c *Client) downloadChunked(ctx context.Context, req *http.Request, w *io.P
currentChunk := atomic.Uint32{}
for i := 0; i < maxRoutines; i++ {
go func() {
i := int(currentChunk.Add(1)) - 1
if i > len(chunks) {
chunkNumber := int(currentChunk.Add(1))
if chunkNumber > len(chunks) {
// no more chunks
return
}

chunk := &chunks[i]
chunk := &chunks[chunkNumber-1]
err := c.downloadChunk(req.Clone(cancelCtx), chunk)
close(chunk.data)

Expand Down
11 changes: 10 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ func TestGetVideoWithManifestURL(t *testing.T) {
func TestGetStream(t *testing.T) {
assert, require := assert.New(t), require.New(t)

expectedSize := 988479

// Create testclient to enforce re-using of routines
testClient := Client{
Debug: true,
MaxRoutines: 10,
ChunkSize: int64(expectedSize) / 11,
}

// Download should not last longer than a minute.
// Otherwise we assume Youtube is throtteling us.
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
Expand All @@ -141,7 +150,7 @@ func TestGetStream(t *testing.T) {

reader, size, err := testClient.GetStreamContext(ctx, video, &video.Formats[0])
require.NoError(err)
assert.EqualValues(988479, size)
assert.EqualValues(expectedSize, size)

data, err := io.ReadAll(reader)
require.NoError(err)
Expand Down

0 comments on commit 488006d

Please sign in to comment.