Checking in tests/specs that READs actually go to the replica #184
-
|
First of all, this is a great gem, thank you for sharing it with us! I've added it in an app and it's working great, though I'm trying to validate with some specs that READs actually go to the read replica, and it doesn't seem to make this switch automatically in a Here's a simple spec, please let me know if specific configuration is needed to make the gem work in a it "fetches from the read replica" do
expect { User.count }.to exactly_query_databases({ primary_replica: :reading })
endand I get back If I manually switch to the read replica, then I get back what I'm expecting: it "fetches from the read replica" do
ActiveRecord::Base.connected_to(role: :reading) do
expect { User.count }.to exactly_query_databases({ primary_replica: :reading })
end
endI must be missing something though it's not obvious to me. Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
|
Hello @serggiu. I moved this to the discussions tab as it's more of a question than a bug. The most likely reason you're seeing this behaviour in your test case is you have transactional fixtures in your test config or, if you're using the Anything inside a transaction will always go to the primary (as it makes no sense to open a transaction in a replica). So your test case should work if you don't wrap your test cases in transactions. You could use truncation instead. Although I'm not sure what you're trying to achieve with that test case. It's not usually advised to write tests for third party code, we usually write tests for code we write. We have thorough, comprehensive coverage on the behaviour you're trying to test. The latest gem version has about 97% overall test coverage. I hope that helps clarify things. |
Beta Was this translation helpful? Give feedback.
-
|
This looks promising. I'm using the default for The way I'm checking for the used db connection is by subscribing to the I have added the If I wrap my query like this Any idea why is it connecting first to the |
Beta Was this translation helpful? Give feedback.
-
|
@mateuscruz thank you for your patience, I got the tests working eventually. The initial |
Beta Was this translation helpful? Give feedback.
-
|
@mateuscruz I forgot to ask something that may be important: what happens if the read replica is not accessible, does the proxy silently fallback to the primary db or does not show connection errors? One other thing that confuses me a bit (while testing our update on our irb(main):001> ActiveRecord::Base.connection_db_config
=>
...
adapter: "postgresql"
...I would have expected the Thank you! |
Beta Was this translation helpful? Give feedback.
Hello @serggiu.
I moved this to the discussions tab as it's more of a question than a bug.
The most likely reason you're seeing this behaviour in your test case is you have transactional fixtures in your test config or, if you're using the
database_cleanergem, your test database is being cleaned with the:transactionstrategy.Anything inside a transaction will always go to the primary (as it makes no sense to open a transaction in a replica).
So your test case should work if you don't wrap your test cases in transactions. You could use truncation instead.
Although I'm not sure what you're trying to achieve with that test case. It's not usually advised to write tests for third party code…