Create new BustCacheWorker and start sending jobs to it (#5302) [deploy]

This commit is contained in:
Molly Struve 2019-12-31 14:14:23 -05:00 committed by GitHub
parent df796f7e07
commit 8650f5c5aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 1 deletions

View file

@ -647,6 +647,6 @@ class Article < ApplicationRecord
end
def async_bust
Articles::BustCacheJob.perform_later(id)
Articles::BustCacheWorker.perform_async(id)
end
end

View file

@ -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

View file

@ -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.

View file

@ -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

View file

@ -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