Skip to content
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

P20-829: Fix Unit.clear_cache #57812

Merged
merged 2 commits into from Apr 9, 2024
Merged

Conversation

artem-vavilov
Copy link
Contributor

@artem-vavilov artem-vavilov commented Apr 4, 2024

  1. Fixed Unit.clear_cache: since @@script_level_cache wasn't being cleared, the data set during the first test remained unchanged in subsequent tests.
    def self.script_level_cache
    return nil unless should_cache?
    @@script_level_cache ||= {}.tap do |cache|
    script_cache.each_value do |unit|
    cache.merge!(unit.script_levels.index_by(&:id))
    end
    end
    end
  2. Re-introduced the seed_deprecated_unit_fixtures optimization that was reverted here: P20-829: Undo optimization of deprecated unit fixtures seeding #57772

Links

@artem-vavilov artem-vavilov requested a review from Hamms April 4, 2024 21:35
@artem-vavilov artem-vavilov changed the title P20-829: Re-introduce seed_deprecated_unit_fixtures optimization to enhance Dashboard test env performance P20-829: Re-introduce seed_deprecated_unit_fixtures optimization to improve Dashboard test env loading Apr 4, 2024
@artem-vavilov artem-vavilov changed the title P20-829: Re-introduce seed_deprecated_unit_fixtures optimization to improve Dashboard test env loading P20-829: Fix Unit.clear_cache Apr 9, 2024
@artem-vavilov artem-vavilov force-pushed the P20-829/prepare-units-on-demand branch from 67253cd to 751e852 Compare April 9, 2024 03:55
@@ -1723,6 +1723,7 @@ def self.clear_cache
raise "only call this in a test!" unless Rails.env.test?
@@unit_cache = nil
@@unit_family_cache = nil
@@script_level_cache = nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since @@script_level_cache wasn't being cleared, the data set during the first test remained unchanged in subsequent tests:

def self.script_level_cache
return nil unless should_cache?
@@script_level_cache ||= {}.tap do |cache|
script_cache.each_value do |unit|
cache.merge!(unit.script_levels.index_by(&:id))
end
end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It worked previously because we seeded deprecated unit fixtures before enabling Unit caching with Unit.stubs(:should_cache?).returns(true):

class CalloutsTest < ActionDispatch::IntegrationTest
setup do
Unit.stubs(:should_cache?).returns true
Rails.application.config.stubs(:levelbuilder_mode).returns false
@script = create :script
@lesson_group = create :lesson_group, script: @script
@lesson = create :lesson, script: @script, lesson_group: @lesson_group
@maze_data = {game_id: 25, user_id: 1, name: '__bob4', level_num: 'custom', skin: 'birds', short_instructions: 'sdfdfs'}
@level = Maze.create(@maze_data)
@level.callout_json = '[{"localization_key": "run", "element_id": "#runButton"}]'
@level.save!
@script_level = create(:script_level, levels: [@level], lesson: @lesson, script: @script)
@level_path = "/levels/#{@level.id}"
@script_level_path = "/s/#{@script.name}/lessons/1/levels/1"
Unit.script_cache.delete @script.name
Unit.script_cache.delete @script.id.to_s

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error can be reproduced by running the command: cd dashboard && RAILS_ENV=test RACK_ENV=test bundle exec rails test --seed 37263 test/integration/callouts_test.rb test/models/unit_test.rb

Copy link
Contributor

@Hamms Hamms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic find! Obscure interactions between two disparate unit tests can be real tough to track down; I'm so thrilled that we now know what the underlying issue was. Thank you for the investigation!

This does raise the question of why clear_cache was previously not clearing the ScriptLevel cache; glancing through the history of the method, however, it does seem like this was not an intentional exclusion. Looks like the method originally only cleared @@unit_cache (technically, @@script_cache), and the other cache values were added individually over time on an as-needed basis. That then raises a second question for me: are there any other locally cached values that should also be added to clear_cache?

I think that question can be addressed in a follow-up PR, though; this one looks great to me! Thank you for the diligent debugging.

@artem-vavilov artem-vavilov merged commit 8f84adc into staging Apr 9, 2024
2 checks passed
@artem-vavilov artem-vavilov deleted the P20-829/prepare-units-on-demand branch April 9, 2024 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants