-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test that reproduces lazy cache invalidation bug
- Loading branch information
Showing
7 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module LazyModel | ||
class A < ActiveRecord::Base | ||
self.table_name = "lazy_as" | ||
include IdentityCache | ||
has_many :bs, class_name: "::LazyModel::B" | ||
cache_has_many :bs, embed: true | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module LazyModel | ||
class B < ActiveRecord::Base | ||
self.table_name = "lazy_bs" | ||
include IdentityCache | ||
belongs_to :a, class_name: "::LazyModel::A", inverse_of: :bs | ||
has_one :c, class_name: "::LazyModel::C", inverse_of: :b | ||
cache_has_one :c | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module LazyModel | ||
class C < ActiveRecord::Base | ||
self.table_name = "lazy_cs" | ||
include IdentityCache | ||
belongs_to :b, class_name: "::LazyModel::B", inverse_of: :c | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require "test_helper" | ||
|
||
class LazyHookTest < IdentityCache::TestCase | ||
def test_expires_unloaded_lazy_parent_models | ||
# Populate cache | ||
a = LazyModel::A.create!(name: "Initial A") | ||
b = a.bs.create!(name: "Initial B") | ||
c = b.create_c!(name: "Initial C") | ||
|
||
ids = { | ||
a: a.id, | ||
b: b.id, | ||
c: c.id, | ||
} | ||
|
||
# Warm cache | ||
LazyModel::A.fetch(ids[:a]).fetch_bs.first.fetch_c | ||
|
||
# Clear lazyloaded code to simulate code that isn't eager loaded | ||
reset_loaded_code | ||
LazyModel::C.find(ids[:c]).update!(name: "Updated C") | ||
|
||
refute(lazy_model_loaded?(:A)) | ||
refute(lazy_model_loaded?(:B)) | ||
|
||
queries = count_queries do | ||
c = LazyModel::A.fetch(ids[:a]).fetch_bs.first.fetch_c | ||
assert_equal("Updated C", c.name) | ||
end | ||
|
||
assert_equal(0, queries) | ||
end | ||
|
||
private | ||
|
||
def reset_loaded_code | ||
teardown_models | ||
setup_models | ||
end | ||
|
||
def lazy_model_loaded?(name) | ||
!LazyModel.autoload?(name) | ||
end | ||
end |