diff options
Diffstat (limited to 'spec/rubocop/cop/sidekiq_load_balancing')
-rw-r--r-- | spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_spec.rb | 50 | ||||
-rw-r--r-- | spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_with_deduplication_spec.rb | 166 |
2 files changed, 216 insertions, 0 deletions
diff --git a/spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_spec.rb b/spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_spec.rb new file mode 100644 index 00000000000..cf8d0d1b66f --- /dev/null +++ b/spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require_relative '../../../../rubocop/cop/sidekiq_load_balancing/worker_data_consistency' + +RSpec.describe RuboCop::Cop::SidekiqLoadBalancing::WorkerDataConsistency do + subject(:cop) { described_class.new } + + before do + allow(cop) + .to receive(:in_worker?) + .and_return(true) + end + + it 'adds an offense when not defining data_consistency' do + expect_offense(<<~CODE) + class SomeWorker + ^^^^^^^^^^^^^^^^ Should define data_consistency expectation.[...] + include ApplicationWorker + + queue_namespace :pipeline_hooks + feature_category :continuous_integration + urgency :high + end + CODE + end + + it 'adds no offense when defining data_consistency' do + expect_no_offenses(<<~CODE) + class SomeWorker + include ApplicationWorker + + queue_namespace :pipeline_hooks + feature_category :continuous_integration + data_consistency :delayed + urgency :high + end + CODE + end + + it 'adds no offense when worker is not an ApplicationWorker' do + expect_no_offenses(<<~CODE) + class SomeWorker + queue_namespace :pipeline_hooks + feature_category :continuous_integration + urgency :high + end + CODE + end +end diff --git a/spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_with_deduplication_spec.rb b/spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_with_deduplication_spec.rb new file mode 100644 index 00000000000..6e7212b1002 --- /dev/null +++ b/spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_with_deduplication_spec.rb @@ -0,0 +1,166 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'rspec-parameterized' +require_relative '../../../../rubocop/cop/sidekiq_load_balancing/worker_data_consistency_with_deduplication' + +RSpec.describe RuboCop::Cop::SidekiqLoadBalancing::WorkerDataConsistencyWithDeduplication do + using RSpec::Parameterized::TableSyntax + + subject(:cop) { described_class.new } + + before do + allow(cop) + .to receive(:in_worker?) + .and_return(true) + end + + where(:data_consistency) { %i[delayed sticky] } + + with_them do + let(:strategy) { described_class::DEFAULT_STRATEGY } + let(:corrected) do + <<~CORRECTED + class SomeWorker + include ApplicationWorker + + data_consistency :#{data_consistency} + + deduplicate #{strategy}, including_scheduled: true + idempotent! + end + CORRECTED + end + + context 'when deduplication strategy is not explicitly set' do + it 'registers an offense and corrects using default strategy' do + expect_offense(<<~CODE) + class SomeWorker + include ApplicationWorker + + data_consistency :#{data_consistency} + + idempotent! + ^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...] + end + CODE + + expect_correction(corrected) + end + + context 'when identation is different' do + let(:corrected) do + <<~CORRECTED + class SomeWorker + include ApplicationWorker + + data_consistency :#{data_consistency} + + deduplicate #{strategy}, including_scheduled: true + idempotent! + end + CORRECTED + end + + it 'registers an offense and corrects with correct identation' do + expect_offense(<<~CODE) + class SomeWorker + include ApplicationWorker + + data_consistency :#{data_consistency} + + idempotent! + ^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...] + end + CODE + + expect_correction(corrected) + end + end + end + + context 'when deduplication strategy does not include including_scheduling option' do + let(:strategy) { ':until_executed' } + + it 'registers an offense and corrects' do + expect_offense(<<~CODE) + class SomeWorker + include ApplicationWorker + + data_consistency :#{data_consistency} + + deduplicate :until_executed + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...] + idempotent! + end + CODE + + expect_correction(corrected) + end + end + + context 'when deduplication strategy has including_scheduling option disabled' do + let(:strategy) { ':until_executed' } + + it 'registers an offense and corrects' do + expect_offense(<<~CODE) + class SomeWorker + include ApplicationWorker + + data_consistency :#{data_consistency} + + deduplicate :until_executed, including_scheduled: false + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...] + idempotent! + end + CODE + + expect_correction(corrected) + end + end + + context "when deduplication strategy is :none" do + it 'does not register an offense' do + expect_no_offenses(<<~CODE) + class SomeWorker + include ApplicationWorker + + data_consistency :always + + deduplicate :none + idempotent! + end + CODE + end + end + + context "when deduplication strategy has including_scheduling option enabled" do + it 'does not register an offense' do + expect_no_offenses(<<~CODE) + class SomeWorker + include ApplicationWorker + + data_consistency :always + + deduplicate :until_executing, including_scheduled: true + idempotent! + end + CODE + end + end + end + + context "data_consistency: :always" do + it 'does not register an offense' do + expect_no_offenses(<<~CODE) + class SomeWorker + include ApplicationWorker + + data_consistency :always + + idempotent! + end + CODE + end + end +end |