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

Test buffered reader / Ensure EmptyBuffer does not get content in it #178

Closed
wants to merge 2 commits into from

Conversation

tempusfrangit
Copy link
Member

This PR introduces tests for bufferedReader to ensure there are no regressions, additionally, it adds a panic() case where we've attempted to use the downloadBody or a .readFrom() on the bufferedReader when emptyBuffer is the underlying bytes.Buffer.

The panic() is indicating a programming error, additionally it helps ensure the assumption that emptyBuffer will EOF is guaranteed. The only use of the br.buf.ReadFrom is from tests and/or downloadBody.

Ensure it is not possible for downloadBody or other wrappers to
accidently populate data into the emptyBuffer.

This could be solved with an interface, but that interface would likely
need to be quite complex as we rely on the methods on bytes.Buffer.
While we could mirror Cap() Len(), etc, this seems like busy work. It is
easier to broker access to br.buf.ReadFrom through br.readFrom to ensure
we panic in the case of writing to a buffer that has had emptyBuffer
assigned to it.

This ensures we can make the assumption that .Read() will result in EOF
in all scenarios when emptyBuffer has been set on the BufferedReader.
@philandstuff
Copy link
Contributor

Ooof, bufferedReader sure is hard to test, isn't it? This PR made me feel bad enough about its interface that I have raised #180 to change it to have a better interface (purely on io abstractions with nothing http-specific) and also a better implementation (delegating more logic to bufio.Reader). If we do that, we don't have to test for some of the behaviour we're testing for here - there's no longer an emptyBuffer that we might write to, we don't have to implement checks for full buffers, bufio.Reader has a fixed-size buffer so we can't unintentionally grow it, bufio.Reader handles passing errors during fetch out on the Read() interface appropriately, etc.

@tempusfrangit tempusfrangit deleted the test-buffered-reader branch March 7, 2024 17:42
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.

2 participants