Skip to content

Commit 6d2ef7a

Browse files
committed
✅ Fix JRuby local backtrace test assertions
A bug was fixed in jruby-head, but that may not be in current releases: * JRuby Issue: jruby/jruby#9528 * Fixed by: jruby/jruby#9528 With that issue fixed, these tests don't need to be marked pending! 😄 BUT, JRuby _does_ still have some incongruity between `caller(1)` and `raise rescue $!.backtrace[1..]`. Some ruby block stack frames in `caller` are replaced by java stack frames in `Exception#backtrace`. For example: ```diff --- Kernel#caller +++ Exception#backtrace /home/nick/.local/share/rubies/jruby-dev/lib/ruby/gems/shared/gems/test-unit-3.7.8/lib/test/unit/testcase.rb:632:in 'block in run' - /home/nick/.local/share/rubies/jruby-dev/lib/ruby/gems/shared/gems/test-unit-3.7.8/lib/test/unit/testcase.rb:631:in 'catch' + org/jruby/RubyKernel.java:1604:in 'catch' + org/jruby/RubyKernel.java:1599:in 'catch' /home/nick/.local/share/rubies/jruby-dev/lib/ruby/gems/shared/gems/test-unit-3.7.8/lib/test/unit/testcase.rb:631:in 'run' ``` The workaround is relatively simple: use a locally generated exception to generate the stack frames for comparison.
1 parent df21e99 commit 6d2ef7a

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

test/lib/helper.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,24 @@ def assert_local_raise(expected, message = nil)
224224
else
225225
assert_raise(expected, &block)
226226
end
227-
stack = caller
228-
assert_equal stack, error.backtrace&.last(stack.size)
227+
assert_local_backtrace error
229228
error
230229
end
231230

231+
# Asserts that +error+ was raised in the same thread as +caller+ _and_ was
232+
# called from the same level as +caller+. The caller's own frame is ignored,
233+
# as are all extra frames in +error+, but the remaining frames much match.
234+
#
235+
# NOTE: `stack = caller(2)` is different from `$!.backtrace[2..]` in JRuby.
236+
# Rather than use `caller`, this raises a local exception to use its backtrace
237+
# for the comparison.
238+
def assert_local_backtrace(error)
239+
local_stack = raise "generating local backtrace" rescue $!.backtrace[2..]
240+
error_stack = error.backtrace&.last(local_stack.size)
241+
assert_equal local_stack, error_stack
242+
error_stack
243+
end
244+
232245
# Combines +assert_local_raise+ with an assertion that the exception's cause
233246
# is in the receiver thread.
234247
#

test/net/imap/test_imap_tls.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_starttls_unknown_ca
110110
imap
111111
end
112112
assert_kind_of(OpenSSL::SSL::SSLError, ex)
113-
assert_equal (stack = caller), ex.backtrace&.last(stack.size)
113+
assert_local_backtrace ex
114114
assert_equal false, imap.tls_verified?
115115
assert_equal({}, imap.ssl_ctx_params)
116116
assert_equal(nil, imap.ssl_ctx.ca_file)

0 commit comments

Comments
 (0)