require "active_record" | |
require "minitest/autorun" | |
class Person < ActiveRecord::Base | |
establish_connection adapter: "sqlite3", database: "foobar.db" | |
end | |
class ConnHandlerSwapTest < Minitest::Test | |
def setup | |
@readonly = ActiveRecord::ConnectionAdapters::ConnectionHandler.new | |
end | |
def swap_connection_handler(&block) | |
old_handler, ActiveRecord::Base.connection_handler = ActiveRecord::Base.connection_handler, @readonly | |
yield | |
ensure | |
ActiveRecord::Base.connection_handler = old_handler | |
end | |
def test_conn_swap | |
readonly_id = 0 | |
swap_connection_handler do | |
readonly_id = ActiveRecord::Base.connection_handler.object_id | |
end | |
refute_equal readonly_id, Person.connection_handler.object_id | |
assert_equal readonly_id, @readonly.object_id | |
end | |
def test_conn_swap_in_fiber | |
readonly_id = 0 | |
enum = Enumerator.new do |r| | |
r << ActiveRecord::Base.connection_handler.object_id | |
end | |
swap_connection_handler do | |
readonly_id = enum.next | |
end | |
assert_equal readonly_id, @readonly.object_id | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
arthurnn commentedAug 2, 2017
Something like this would fix the test: