diff --git a/test/lib/helper.rb b/test/lib/helper.rb index fe11727d..e52a1e19 100644 --- a/test/lib/helper.rb +++ b/test/lib/helper.rb @@ -224,11 +224,24 @@ def assert_local_raise(expected, message = nil) else assert_raise(expected, &block) end - stack = caller - assert_equal stack, error.backtrace&.last(stack.size) + assert_local_backtrace error error end + # Asserts that +error+ was raised in the same thread as +caller+ _and_ was + # called from the same level as +caller+. The caller's own frame is ignored, + # as are all extra frames in +error+, but the remaining frames much match. + # + # NOTE: `stack = caller(2)` is different from `$!.backtrace[2..]` in JRuby. + # Rather than use `caller`, this raises a local exception to use its backtrace + # for the comparison. + def assert_local_backtrace(error) + local_stack = raise "generating local backtrace" rescue $!.backtrace[2..] + error_stack = error.backtrace&.last(local_stack.size) + assert_equal local_stack, error_stack + error_stack + end + # Combines +assert_local_raise+ with an assertion that the exception's cause # is in the receiver thread. # diff --git a/test/net/imap/test_imap_tls.rb b/test/net/imap/test_imap_tls.rb index 052c114e..0fbceed8 100644 --- a/test/net/imap/test_imap_tls.rb +++ b/test/net/imap/test_imap_tls.rb @@ -110,7 +110,7 @@ def test_starttls_unknown_ca imap end assert_kind_of(OpenSSL::SSL::SSLError, ex) - assert_equal (stack = caller), ex.backtrace&.last(stack.size) + assert_local_backtrace ex assert_equal false, imap.tls_verified? assert_equal({}, imap.ssl_ctx_params) assert_equal(nil, imap.ssl_ctx.ca_file)