Skip to content

Add tests for streaming chunks.#52253

Closed
ioquatix wants to merge 2 commits into
rails:mainfrom
ioquatix:streaming-tests
Closed

Add tests for streaming chunks.#52253
ioquatix wants to merge 2 commits into
rails:mainfrom
ioquatix:streaming-tests

Conversation

@ioquatix

@ioquatix ioquatix commented Jul 2, 2024

Copy link
Copy Markdown
Contributor

Motivation / Background

The tests for streaming responses, as modified in #52094 are insufficient to determine whether the response included multiple chunks. We should ensure that this is the case, as well as ensuring that the response body conforms to an "Enumerable Body" as defined by the Rack specification when stream: true is requested.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@ioquatix

ioquatix commented Jul 2, 2024

Copy link
Copy Markdown
Contributor Author

I was investigating the no layout test failure, and found that for some reason, streaming is disabled when there was no layout. So, in that case, stream was not actually being respected and it was just returning an array of chunks. I changed this behaviour so that it would correctly stream the response.

@ioquatix ioquatix force-pushed the streaming-tests branch 4 times, most recently from 886e089 to fa71302 Compare July 2, 2024 05:03
@ioquatix ioquatix marked this pull request as ready for review July 2, 2024 05:42
# that knows how to render the template.
def render_template(view, template, layout_name = nil, locals = {}) # :nodoc:
return [super.body] unless layout_name && template.supports_streaming?
return [super.body] unless template.supports_streaming?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This change (layout_name) was introduced in 29078ff but I'm not sure why. It prevents proper streaming for non-layout controller actions. The test appears to assume streaming should work, and so I've removed this condition.

@ioquatix ioquatix force-pushed the streaming-tests branch 3 times, most recently from e4b4d01 to 2900df4 Compare August 12, 2024 23:37

class StreamingTest < Rack::TestCase
test "rendering with streaming enabled at the class level" do
env = Rack::MockRequest.env_for("/render_streaming/basic/hello_world")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why can't use keep using get anymore?

@ioquatix ioquatix Aug 15, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I considered how I could simplify the tests.

Unfortunately, Rails is using it's own bespoke implementation for TestResponse:

module ActionDispatch
  class TestResponse < Response
    def self.from_response(response)
      new response.status, response.headers, response.body
    end

This assigns using Response#body=:

module ActionDispatch
  class Response
    # Allows you to manually set or override the response body.
    def body=(body)
      if body.respond_to?(:to_path)
        @stream = body
      else
        synchronize do
          @stream = build_buffer self, munge_body_object(body)
        end
      end
    end

Unfortunately, at this time, there is no way to bypass build_buffer self, munge_body_object(body). It's necessary to see the unbuffered "chunks" in order to verify streaming. Once this PR, and the other linked PR is merged, I'll try to rework these tests with an improved approach.

In other words, I do agree we should simplify the tests, but I don't want this PR to introduce significant changes to the implementation in order to do so.

@ioquatix

ioquatix commented Sep 2, 2024

Copy link
Copy Markdown
Contributor Author

@rafaelfranca any chance you can merge this? Thanks!

If you want to delegate this effort, feel free to give me the commit bit so I can work on this independently.

@ioquatix

ioquatix commented Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

I've re-based on main.

@ioquatix

Copy link
Copy Markdown
Contributor Author

@rafaelfranca it would be great to merge this before Rails 8 is released.

@ioquatix

ioquatix commented Nov 1, 2024

Copy link
Copy Markdown
Contributor Author

I'm going to rebase this all into one PR as it's easier for me to deal with.

@ioquatix ioquatix closed this Nov 1, 2024
@ioquatix ioquatix deleted the streaming-tests branch November 1, 2024 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants