diff options
author | Eileen M. Uchitelle <eileencodes@users.noreply.github.com> | 2019-02-01 13:53:34 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-01 13:53:34 -0500 |
commit | caf8dbc1591c64ae4fd9253a714903710f46f7ce (patch) | |
tree | 69e9be104ab71e9b1aa1144eede7affc703f616d /activerecord/test | |
parent | bc0cad0690ab2c16fefae7421a2908052ca57f19 (diff) | |
parent | 31f205234acb5f4b084eb726cf903c55d59677fa (diff) | |
download | rails-caf8dbc1591c64ae4fd9253a714903710f46f7ce.tar.gz rails-caf8dbc1591c64ae4fd9253a714903710f46f7ce.tar.bz2 rails-caf8dbc1591c64ae4fd9253a714903710f46f7ce.zip |
Merge pull request #35130 from rails/move-delay-to-options-argument
Refactor options for database selector middleware
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/database_selector_test.rb | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/activerecord/test/cases/database_selector_test.rb b/activerecord/test/cases/database_selector_test.rb index 6142e223ce..4106a6ec46 100644 --- a/activerecord/test/cases/database_selector_test.rb +++ b/activerecord/test/cases/database_selector_test.rb @@ -95,6 +95,54 @@ module ActiveRecord assert @session_store[:last_write] end + def test_read_from_primary_with_options + resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver.new(@session, delay: 5.seconds) + + # Session should start empty + assert_nil @session_store[:last_write] + + called = false + resolver.write do + assert ActiveRecord::Base.connected_to?(role: :writing) + called = true + end + assert called + + # and be populated by the last write time + assert @session_store[:last_write] + + read = false + resolver.read do + assert ActiveRecord::Base.connected_to?(role: :writing) + read = true + end + assert read + end + + def test_read_from_replica_with_no_delay + resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver.new(@session, delay: 0.seconds) + + # Session should start empty + assert_nil @session_store[:last_write] + + called = false + resolver.write do + assert ActiveRecord::Base.connected_to?(role: :writing) + called = true + end + assert called + + # and be populated by the last write time + assert @session_store[:last_write] + + read = false + resolver.read do + assert ActiveRecord::Base.connected_to?(role: :reading) + read = true + end + assert read + end + def test_the_middleware_chooses_writing_role_with_POST_request middleware = ActiveRecord::Middleware::DatabaseSelector.new(lambda { |env| assert ActiveRecord::Base.connected_to?(role: :writing) |