1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Prevent race condition when resetting time stubs

If the current thread is preempted after the stub has been removed but
before the original method has been restored, then the other thread will
get a `NoMethodError` when it tries to call the method.

Using `silence_redefinition_of_method` instead of `undef_method` ensures
that either the stub or the original method is always in place.
This commit is contained in:
Eugene Kenny 2017-12-12 06:31:45 +00:00
parent c5462d1f27
commit 6122d2bfdf

View file

@ -1,5 +1,6 @@
# frozen_string_literal: true
require "active_support/core_ext/module/redefine_method"
require "active_support/core_ext/string/strip" # for strip_heredoc
require "active_support/core_ext/time/calculations"
require "concurrent/map"
@ -43,7 +44,7 @@ module ActiveSupport
def unstub_object(stub)
singleton_class = stub.object.singleton_class
singleton_class.send :undef_method, stub.method_name
singleton_class.send :silence_redefinition_of_method, stub.method_name
singleton_class.send :alias_method, stub.method_name, stub.original_method
singleton_class.send :undef_method, stub.original_method
end