super_diff: RSpec hanging when matching on http response
We’ve seen an issue where in a rails request spec we have something like this:
it "returns a failure" do
post some_path(**args)
expect(response).to be_created
end
In cases where the spec would fail, it seems to hang forever (I’m not sure if it would eventually complete if I left it, but it hits our CI pipeline timeout limit of 15 minutes).
I’ve managed to trace it to a method within super_diff when its trying to report the failure, but I’ve not found the root cause.
We’re using version super_diff 0.9.0, with rspec 3.11.0 (and rails 7.0.4). (I can provide our Gemfile.lock if that would be useful).
If anyone else comes across this with the same problem, we’ve worked around this by using have_http_status(:created) instead.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 12
- Comments: 16 (2 by maintainers)
On further investigation with a couple production apps, I’ve come to the conclusion that this is not infinite recursion: it’s deep-inspecting
ActionDispatch::Routing::RouteSet, a structure with tons of references to other complex structures. This is nested in:ActionDispatch::TestResponse@request@env['action_dispatch.routes']The Rails code itself aliases
ActionDispatch::Routing::RouteSet#inspectto#to_s, specifically citing the large tree.I added an inspection tree builder for
ActionDispatch::Routing::RouteSetto test this theory.SuperDiff.inspect_object(response, as_lines: false)terminated in a couple seconds instead of hanging indefinitely (though it still rendered a string over 233K characters long!)Adding a custom inspection of
ActionDispatch::Requestobjects would prune the inspection tree even more. Rails also customizes inspection forActionDispatch::Request, showing only the request method, original URL, and originating IP, so I think this would still be pretty reasonable. (I tested this out again and it shortened the output to “only” ~55K characters, which is very close to the length of the direct#inspectoutput on the originalActionDispatch::TestResponseobject.)For anyone else having this problem: I have two solutions. Both solutions work in the user code and don’t need a change to the gem. My company is probably going with option 1.
Solution 1:
Monkey patch the TestResponse. Add this to the bottom of
spec/rails_helper.rb.Solution 2:
Create a new inspection tree builder. Make the following new file at spec/support/super_diff/action_dispatch_test_response_inspection_tree_builder.rb
Then change your SuperDiff configuration to use the new inspection tree builder:
Output
With either option, the output looks something like this:
@mcmire do you think either one of these is suitable to add to the gem?
Thanks for re-flagging. I have a branch open for this; in the meantime, Solution 2 as described in https://github.com/splitwise/super_diff/issues/160#issuecomment-1627712258 should work as a quick fix.
SuperDiff aims to improve most RSpec output – that includes both diffs and object inspection. The error message for the
be_xyzmatcher is something like “expected [object] to return truthy for xyz? but returned false”; the issue here is that the “[object]” string takes forever to generate.Ah, thanks @travatomic, that’s helpful. I’ll take a look at the Rails fix — maybe
inspect_objectcan look for a ActionDispatch::TestResponse and monkeypatch it on the fly so thatto_areturns something else. It would be educational to start by tracing it and finding which method it keeps recursing on, though. As for your recursion counter idea, that would be good in general as well, in case we run into any other situations like this in the future. In any case I’m a bit busy at the moment but will hopefully have time to look at this shortly.The issue seems to be because of circular references in the object in question, so it seems like we could just wrap the
compareinBasewith a circular reference protection?E.g.: