Skip to content

Commit

Permalink
Refactor timeout_if_not_debugging
Browse files Browse the repository at this point in the history
Refactor `timeout_if_not_debugging` into `spec_helper` and use it in the
compound matcher spec to limit the run time when testing long chains of
compound matchers.
  • Loading branch information
genehsu committed Nov 18, 2021
1 parent f72dfbc commit 74fe79e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
32 changes: 20 additions & 12 deletions spec/rspec/matchers/built_in/compound_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -882,31 +882,39 @@ def expect_block

context "with a failing first matcher" do
it "generates a failure description quickly" do
compound = failing_matcher
15.times { compound = compound.and(passing_matcher) }
expect { expect([actual]).to compound }.to fail_including("expected [#{actual}] to include #{not_expected}")
timeout_if_not_debugging(0.1) do
compound = failing_matcher
15.times { compound = compound.and(passing_matcher) }
expect { expect([actual]).to compound }.to fail_including("expected [#{actual}] to include #{not_expected}")
end
end
end

context "with a failing last matcher" do
it "generates a failure description quickly" do
compound = failing_matcher
15.times { compound = passing_matcher.and(compound) }
expect { expect([actual]).to compound }.to fail_including("expected [#{actual}] to include #{not_expected}")
timeout_if_not_debugging(0.1) do
compound = failing_matcher
15.times { compound = passing_matcher.and(compound) }
expect { expect([actual]).to compound }.to fail_including("expected [#{actual}] to include #{not_expected}")
end
end
end

context "with all failing matchers" do
it "generates a failure description quickly with and" do
compound = failing_matcher
15.times { compound = compound.and(failing_matcher) }
expect { expect([actual]).to compound }.to fail_including("expected [#{actual}] to include #{not_expected}")
timeout_if_not_debugging(0.1) do
compound = failing_matcher
15.times { compound = compound.and(failing_matcher) }
expect { expect([actual]).to compound }.to fail_including("expected [#{actual}] to include #{not_expected}")
end
end

it "generates a failure description quickly with or" do
compound = failing_matcher
15.times { compound = compound.or(failing_matcher) }
expect { expect([actual]).to compound }.to fail_including("expected [#{actual}] to include #{not_expected}")
timeout_if_not_debugging(0.1) do
compound = failing_matcher
15.times { compound = compound.or(failing_matcher) }
expect { expect([actual]).to compound }.to fail_including("expected [#{actual}] to include #{not_expected}")
end
end
end
end
Expand Down
8 changes: 0 additions & 8 deletions spec/rspec/matchers/built_in/contain_exactly_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,6 @@ def array.send; :sent; end
MESSAGE
end

def timeout_if_not_debugging(time)
in_sub_process_if_possible do
require 'timeout'
return yield if defined?(::Debugger)
Timeout.timeout(time) { yield }
end
end

it 'fails a match of 11 items with duplicates in a reasonable amount of time' do
timeout_if_not_debugging(0.1) do
expected = [0, 1, 1, 3, 3, 3, 4, 4, 8, 8, 9 ]
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def dedent(string)
string.gsub(/^\s+\|/, '').chomp
end

def timeout_if_not_debugging(time)
in_sub_process_if_possible do
require 'timeout'
return yield if defined?(::Debugger)
Timeout.timeout(time) { yield }
end
end

# We have to use Hash#inspect in examples that have multi-entry
# hashes because the #inspect output on 1.8.7 is non-deterministic
# due to the fact that hashes are not ordered. So we can't simply
Expand Down

0 comments on commit 74fe79e

Please sign in to comment.