Created
August 2, 2017 19:34
-
-
Save arthurnn/9dc9a71cbbddac98a9dee2c79dfd7d05 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Something like this would fix the test: