diff options
author | Eileen Uchitelle <eileencodes@gmail.com> | 2019-02-01 10:16:34 -0500 |
---|---|---|
committer | Eileen Uchitelle <eileencodes@gmail.com> | 2019-02-01 11:34:50 -0500 |
commit | 31f205234acb5f4b084eb726cf903c55d59677fa (patch) | |
tree | 2f94b169a30755e64227073cbeef0060d63d6193 /activerecord/test | |
parent | 79bc9e81c3d47be6336223be39cb3bcaeddc0a39 (diff) | |
download | rails-31f205234acb5f4b084eb726cf903c55d59677fa.tar.gz rails-31f205234acb5f4b084eb726cf903c55d59677fa.tar.bz2 rails-31f205234acb5f4b084eb726cf903c55d59677fa.zip |
Refactor options for middleware
Right now we only have one option that's supported, the delay. However I
can see us supporting other options in the future.
This PR refactors the options to get passed into the resolver so whether
you're using middleware or using the config options you can pass options
to the resolver. This will also make it easy to add new options in the
future.
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) |