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

Added tests for getDownloadSize #19

Merged
merged 1 commit into from
Nov 17, 2022
Merged

Conversation

devils2ndself
Copy link
Contributor

Closes #18

I have added 3 unit tests for getDownloadSize: one using ContentLength, one using Content-Range header, and one as a failure.

Copy link
Owner

@cuducos cuducos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @devils2ndself — many many thanks for joining us in such an early stage : ) This make me so happy 💜

Also, thank you for the quality PR! I added minor inline comments if you wanna change them. Also, if you're changing anything, feel free to rebase and squash your PRs (otherwise, I'll do it on merge here).

Let me know if you need more time, or if we merge that one as it is and follow up with new changes in new PRs.

main_test.go Outdated Show resolved Hide resolved
main_test.go Outdated Show resolved Hide resolved
main_test.go Outdated
Comment on lines 263 to 261
d.client = &http.Client{Timeout: d.TimeoutPerChunk}
got, err := d.getDownloadSize(context.Background(), "test")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this test more realistic (or add yet a fourth test case)?

Maybe a response with content-length set to 0 and no content-range header…?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good catch. However, not having Content-Range and ContentLength as 0 breaks the function, returning 18446744073709551615 and no error. Would you like me to add a sentinel to the function when no Content is presented?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch… another bug!

Yes, if you can find a workaround, that would be great. Re-reading it, it looks like this logic is deceiving:

chunk/main.go

Line 150 in a27be85

if resp.ContentLength <= 0 && resp.Header.Get("Content-Range") != "" {

Maybe we just check resp.ContentLength <= 0 and deal with the other header inside of the if block to make it clearer and less error-prone.

Otherwise, feel free to leave the new test with the assertion commented with a // TODO: fix (or similar).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so I encountered another bug... setting a sentinel for checking if both ContentLength and Content-Range are null breaks another test which expects an error in a different place... Is it okay if I remove the sentinel and leave this one for another issue?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay if I remove the sentinel and leave this one for another issue?

Sure think! Just leave the test you wrote commenting the t.Errorf(…) with a // TODO: … so we don't forget about it : )

Many many thanks 💜

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus, it might make more sense to return 0 instead of an error if ContentLegnth is 0 and there is no Content-Range header, and then checking if download size is 0. I will leave that for you to decide

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might make more sense to return 0 instead of an error if ContentLegnth is 0

I don't think so. The only use case we have for getting the size is to know how many content-range requests to spin. If we don't know that, the whole process is broken.

In the future (stretch goal), we can capture cases like that and set a regular download (not using a content-range header), but for that, any error on getDownloadSize would do it.

 * added tests for getDownloadSize

 * addressed requested changes

 * client assignment on DefaultDownloader() and NoContent test

 * added a TODO for future no-content error-handling
@cuducos
Copy link
Owner

cuducos commented Nov 17, 2022

Beautifully done ✨ Thank you so much.
Can I merge it, or is there anything else you wanna add to this PR?

@devils2ndself
Copy link
Contributor Author

just pushed a TODO for future contributers, but I think that's it for the issue

@cuducos cuducos merged commit 4e96cd2 into cuducos:main Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Write tests for getDownloadSize
2 participants