-
Notifications
You must be signed in to change notification settings - Fork 8
Allow to load associations asynchronously #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1841,3 +1841,35 @@ def test_destroy_linked_models | |
assert_not Author.exists?(author.id) | ||
end | ||
end | ||
|
||
class AsyncBelongsToAssociationsTest < ActiveRecord::TestCase | ||
include WaitForAsyncTestHelper | ||
|
||
fixtures :companies | ||
|
||
self.use_transactional_tests = false | ||
|
||
def test_async_load_belongs_to | ||
client = Client.find(3) | ||
first_firm = companies(:first_firm) | ||
|
||
promise = client.load_async(:firm) | ||
wait_for_async_query | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to wait? Should not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to, it's only for testing purpose to ensure the query is actually executed in the background. |
||
|
||
events = [] | ||
callback = -> (event) do | ||
events << event unless event.payload[:name] == "SCHEMA" | ||
end | ||
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do | ||
client.firm | ||
end | ||
|
||
assert_no_queries do | ||
assert_equal first_firm, client.firm | ||
assert_equal first_firm.name, client.firm.name | ||
end | ||
|
||
assert_equal 1, events.size | ||
assert_equal true, events.first.payload[:async] | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -941,3 +941,35 @@ def test_has_one_with_touch_option_on_nonpersisted_built_associations_doesnt_upd | |
MESSAGE | ||
end | ||
end | ||
|
||
class AsyncHasOneAssociationsTest < ActiveRecord::TestCase | ||
include WaitForAsyncTestHelper | ||
|
||
fixtures :companies, :accounts | ||
|
||
self.use_transactional_tests = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self. With the recent refactor in connection pool / pinned connections, we should actually be able to properly simulate async queries with transactional tests. We just need a lock around the connection. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @casperisfine We are interested in this - would we have to do anything in the tests themselves? |
||
|
||
def test_async_load_has_one | ||
firm = companies(:first_firm) | ||
first_account = Account.find(1) | ||
|
||
promise = firm.load_async(:account) | ||
wait_for_async_query | ||
|
||
events = [] | ||
callback = -> (event) do | ||
events << event unless event.payload[:name] == "SCHEMA" | ||
end | ||
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do | ||
firm.account | ||
end | ||
|
||
assert_no_queries do | ||
assert_equal first_account, firm.account | ||
assert_equal first_account.credit_limit, firm.account.credit_limit | ||
end | ||
|
||
assert_equal 1, events.size | ||
assert_equal true, events.first.payload[:async] | ||
end | ||
end |
Uh oh!
There was an error while loading. Please reload this page.