From 8650f5c5aa7dbf1bda5d93887f4e88727991d58e Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Tue, 31 Dec 2019 14:14:23 -0500 Subject: [PATCH] Create new BustCacheWorker and start sending jobs to it (#5302) [deploy] --- app/models/article.rb | 2 +- app/workers/articles/bust_cache_worker.rb | 13 ++++++ spec/rails_helper.rb | 1 + .../articles/bust_cache_worker_spec.rb | 46 +++++++++++++++++++ .../enqueues_on_correct_queue.rb | 11 +++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 app/workers/articles/bust_cache_worker.rb create mode 100644 spec/workers/articles/bust_cache_worker_spec.rb create mode 100644 spec/workers/shared_examples/enqueues_on_correct_queue.rb diff --git a/app/models/article.rb b/app/models/article.rb index bac8faf3b..3c4fe8709 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -647,6 +647,6 @@ class Article < ApplicationRecord end def async_bust - Articles::BustCacheJob.perform_later(id) + Articles::BustCacheWorker.perform_async(id) end end diff --git a/app/workers/articles/bust_cache_worker.rb b/app/workers/articles/bust_cache_worker.rb new file mode 100644 index 000000000..e1876e996 --- /dev/null +++ b/app/workers/articles/bust_cache_worker.rb @@ -0,0 +1,13 @@ +module Articles + class BustCacheWorker + include Sidekiq::Worker + + sidekiq_options queue: :high_priority, retry: 10 + + def perform(article_id, cache_buster = "CacheBuster") + article = Article.find_by(id: article_id) + + cache_buster.constantize.bust_article(article) if article + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index f1f046be6..9bbb49cb4 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -33,6 +33,7 @@ Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |f| require f } Dir[Rails.root.join("spec/system/shared_examples/**/*.rb")].sort.each { |f| require f } Dir[Rails.root.join("spec/models/shared_examples/**/*.rb")].sort.each { |f| require f } Dir[Rails.root.join("spec/jobs/shared_examples/**/*.rb")].sort.each { |f| require f } +Dir[Rails.root.join("spec/workers/shared_examples/**/*.rb")].sort.each { |f| require f } # Checks for pending migrations before tests are run. # If you are not using ActiveRecord, you can remove this line. diff --git a/spec/workers/articles/bust_cache_worker_spec.rb b/spec/workers/articles/bust_cache_worker_spec.rb new file mode 100644 index 000000000..83dd19f0a --- /dev/null +++ b/spec/workers/articles/bust_cache_worker_spec.rb @@ -0,0 +1,46 @@ +require "rails_helper" + +class NewBuster + def self.bust_article(*); end +end + +RSpec.describe Articles::BustCacheWorker, type: :worker do + include_examples "#enqueues_on_correct_queue", "high_priority", 1 + + describe "#perform" do + let(:worker) { subject } + + context "with article" do + let(:article) { double } + let(:article_id) { 1 } + + before do + allow(Article).to receive(:find_by).with(id: article_id).and_return(article) + end + + it "with cache buster defined busts cache with defined buster" do + allow(NewBuster).to receive(:bust_article) + worker.perform(article_id, "NewBuster") + expect(NewBuster).to have_received(:bust_article).with(article) + end + + it "without cache buster defined busts cache with default" do + allow(CacheBuster).to receive(:bust_article) + worker.perform(article_id) + expect(CacheBuster).to have_received(:bust_article).with(article) + end + end + + context "without article" do + it "does not error" do + expect { worker.perform(nil, "CacheBuster") }.not_to raise_error + end + + it "does not bust cache" do + allow(CacheBuster).to receive(:bust_article) + worker.perform(nil) + expect(CacheBuster).not_to have_received(:bust_article) + end + end + end +end diff --git a/spec/workers/shared_examples/enqueues_on_correct_queue.rb b/spec/workers/shared_examples/enqueues_on_correct_queue.rb new file mode 100644 index 000000000..badb8b37f --- /dev/null +++ b/spec/workers/shared_examples/enqueues_on_correct_queue.rb @@ -0,0 +1,11 @@ +RSpec.shared_examples "#enqueues_on_correct_queue" do |queue_name, args| + describe "#perform_async" do + it "enqueues the job" do + Sidekiq::Testing.fake! + + expect do + described_class.perform_async(args) + end.to change { Sidekiq::Queues[queue_name].size }.by(1) + end + end +end