From 128ed47cd3a4dbd84c0774c96eb0218bda5b303b Mon Sep 17 00:00:00 2001 From: Mikael Henriksson Date: Mon, 25 Jun 2018 11:45:35 +0200 Subject: [PATCH 1/9] Speed up tests a little --- examples/until_and_while_executing_job.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/until_and_while_executing_job.rb b/examples/until_and_while_executing_job.rb index 5e9d3ca14..b2782e562 100644 --- a/examples/until_and_while_executing_job.rb +++ b/examples/until_and_while_executing_job.rb @@ -5,7 +5,7 @@ class UntilAndWhileExecutingJob include Sidekiq::Worker - sidekiq_options queue: :working, unique: :until_and_while_executing, lock_timeout: 1 + sidekiq_options queue: :working, unique: :until_and_while_executing, lock_timeout: 0 def perform(one) [one] From b8a7edcece622d164a7cb52ba6ba26fc20adb483 Mon Sep 17 00:00:00 2001 From: Mikael Henriksson Date: Mon, 25 Jun 2018 11:46:05 +0200 Subject: [PATCH 2/9] Adds explicit integration specs --- .../lock/while_executing_spec.rb | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 spec/integration/sidekiq_unique_jobs/lock/while_executing_spec.rb diff --git a/spec/integration/sidekiq_unique_jobs/lock/while_executing_spec.rb b/spec/integration/sidekiq_unique_jobs/lock/while_executing_spec.rb new file mode 100644 index 000000000..0273174f6 --- /dev/null +++ b/spec/integration/sidekiq_unique_jobs/lock/while_executing_spec.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe SidekiqUniqueJobs::Lock::WhileExecuting, redis: :redis do + include SidekiqHelpers + + let(:process_one) { described_class.new(item_one) } + let(:process_two) { described_class.new(item_two) } + + let(:jid_one) { 'jid one' } + let(:jid_two) { 'jid two' } + let(:worker_class) { WhileExecutingRejectJob } + let(:unique) { :while_executing_reject } + let(:queue) { :rejecting } + let(:args) { %w[array of arguments] } + let(:callback) { -> {} } + let(:item_one) do + { 'jid' => jid_one, + 'class' => worker_class.to_s, + 'queue' => queue, + 'unique' => unique, + 'args' => args } + end + let(:item_two) do + { 'jid' => jid_two, + 'class' => worker_class.to_s, + 'queue' => queue, + 'unique' => unique, + 'args' => args } + end + + before do + allow(callback).to receive(:call).and_call_original + end + + describe '#execute' do + it 'does not lock jobs' do + expect(process_one.lock).to eq(true) + expect(process_one.locked?).to eq(false) + + expect(process_two.lock).to eq(true) + expect(process_two.locked?).to eq(false) + end + + context 'when job is executing' do + it 'locks the process' do + process_one.execute(callback) do + expect(process_one.locked?).to eq(true) + end + end + + it 'calls back' do + process_one.execute(callback) do + # NO OP + end + expect(callback).to have_received(:call) + end + + it 'prevents other processes from executing' do + process_one.execute(callback) do + expect(process_two.lock).to eq(true) + expect(process_two.locked?).to eq(false) + unset = true + process_two.execute(callback) do + unset = false + end + expect(unset).to eq(true) + end + + expect(callback).to have_received(:call).once + end + end + end +end From 311494fea75f7af4a346eb5e0d0242cefe91aa7b Mon Sep 17 00:00:00 2001 From: Mikael Henriksson Date: Mon, 25 Jun 2018 11:59:40 +0200 Subject: [PATCH 3/9] Fix UntilExpired job --- examples/until_expired_job.rb | 2 +- lib/sidekiq_unique_jobs/lock/until_expired.rb | 1 + .../lock/until_expired_spec.rb | 96 +++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb diff --git a/examples/until_expired_job.rb b/examples/until_expired_job.rb index b49da941a..85b32b34a 100644 --- a/examples/until_expired_job.rb +++ b/examples/until_expired_job.rb @@ -4,7 +4,7 @@ class UntilExpiredJob include Sidekiq::Worker - sidekiq_options unique: :until_expired, lock_expiration: 10 * 60 + sidekiq_options unique: :until_expired, lock_expiration: 1, lock_timeout: 0 def perform(one) TestClass.run(one) diff --git a/lib/sidekiq_unique_jobs/lock/until_expired.rb b/lib/sidekiq_unique_jobs/lock/until_expired.rb index 4d50eca49..5924c599e 100644 --- a/lib/sidekiq_unique_jobs/lock/until_expired.rb +++ b/lib/sidekiq_unique_jobs/lock/until_expired.rb @@ -8,6 +8,7 @@ def unlock end def execute(callback) + return unless locked? yield if block_given? callback.call end diff --git a/spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb b/spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb new file mode 100644 index 000000000..a6e69f667 --- /dev/null +++ b/spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe SidekiqUniqueJobs::Lock::UntilExpired, redis: :redis do + include SidekiqHelpers + + let(:process_one) { described_class.new(item_one) } + let(:process_two) { described_class.new(item_two) } + + let(:jid_one) { 'jid one' } + let(:jid_two) { 'jid two' } + let(:worker_class) { UntilExpiredJob } + let(:unique) { :until_expired } + let(:queue) { :rejecting } + let(:args) { %w[array of arguments] } + let(:callback) { -> {} } + let(:item_one) do + { 'jid' => jid_one, + 'class' => worker_class.to_s, + 'queue' => queue, + 'unique' => unique, + 'args' => args } + end + let(:item_two) do + { 'jid' => jid_two, + 'class' => worker_class.to_s, + 'queue' => queue, + 'unique' => unique, + 'args' => args } + end + + before do + allow(callback).to receive(:call).and_call_original + end + + describe '#execute' do + it 'process one can be locked' do + expect(process_one.lock).to eq(jid_one) + expect(process_one.locked?).to eq(true) + end + + context 'when process one has locked the job' do + before do + process_one.lock + end + + it 'process two cannot achieve a lock' do + expect(process_two.lock).to eq(nil) + end + + it 'process two cannot execute the lock' do + unset = true + process_two.execute(callback) do + unset = false + end + + expect(unset).to eq(true) + end + + it 'process one can execute the job' do + set = false + process_one.execute(callback) do + set = true + end + + expect(set).to eq(true) + end + + it 'the job is still locked after executing' do + process_one.execute(callback) {} + + expect(process_one.locked?).to eq(true) + end + + it 'calls back' do + process_one.execute(callback) do + # NO OP + end + + expect(callback).to have_received(:call) + end + + it 'prevents ' do + end + + it 'callbacks are only called once (for the locked process)' do + process_one.execute(callback) do + process_two.execute(callback) {} + end + + expect(callback).to have_received(:call).once + end + end + end +end From dc0e60b2a5e7a2c87e3a1c80f734afb90fc80844 Mon Sep 17 00:00:00 2001 From: Mikael Henriksson Date: Mon, 25 Jun 2018 11:59:59 +0200 Subject: [PATCH 4/9] Fix the tests for WhileExecuting --- .../sidekiq_unique_jobs/lock/while_executing_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/integration/sidekiq_unique_jobs/lock/while_executing_spec.rb b/spec/integration/sidekiq_unique_jobs/lock/while_executing_spec.rb index 0273174f6..432ece911 100644 --- a/spec/integration/sidekiq_unique_jobs/lock/while_executing_spec.rb +++ b/spec/integration/sidekiq_unique_jobs/lock/while_executing_spec.rb @@ -10,9 +10,9 @@ let(:jid_one) { 'jid one' } let(:jid_two) { 'jid two' } - let(:worker_class) { WhileExecutingRejectJob } - let(:unique) { :while_executing_reject } - let(:queue) { :rejecting } + let(:worker_class) { WhileExecutingJob } + let(:unique) { :while_executing } + let(:queue) { :while_executing } let(:args) { %w[array of arguments] } let(:callback) { -> {} } let(:item_one) do From b8a495bf86e833b3bcc78ef2545d35bee8580aa0 Mon Sep 17 00:00:00 2001 From: Mikael Henriksson Date: Mon, 25 Jun 2018 17:10:18 +0200 Subject: [PATCH 5/9] Run example specs with guard --- Guardfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Guardfile b/Guardfile index 9edcf9a58..b9b18024b 100644 --- a/Guardfile +++ b/Guardfile @@ -34,6 +34,7 @@ guard :rspec, cmd: "env COV=false bundle exec rspec" do rspec = dsl.rspec watch(%r{^lib/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" } watch(%r{^lib/(.+)\.rb$}) { |m| "spec/integration/#{m[1]}_spec.rb" } + watch(%r{^examples/(.+)\.rb$}) { |m| "spec/examples/#{m[1]}_spec.rb" } watch(rspec.spec_helper) { rspec.spec_dir } watch(rspec.spec_support) { rspec.spec_dir } watch(rspec.spec_files) From 29627dd46f33a5cbbc60fe24be9f5c9d18e990db Mon Sep 17 00:00:00 2001 From: Mikael Henriksson Date: Mon, 25 Jun 2018 17:14:23 +0200 Subject: [PATCH 6/9] Fix broken specs --- spec/examples/until_expired_job_spec.rb | 3 +- .../lock/until_expired_spec.rb | 29 +++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/spec/examples/until_expired_job_spec.rb b/spec/examples/until_expired_job_spec.rb index 9ea585545..c1b8a79a9 100644 --- a/spec/examples/until_expired_job_spec.rb +++ b/spec/examples/until_expired_job_spec.rb @@ -6,7 +6,8 @@ it_behaves_like 'sidekiq with options' do let(:options) do { - 'lock_expiration' => 600, + 'lock_expiration' => 1, + 'lock_timeout' => 0, 'retry' => true, 'unique' => :until_expired, } diff --git a/spec/unit/sidekiq_unique_jobs/lock/until_expired_spec.rb b/spec/unit/sidekiq_unique_jobs/lock/until_expired_spec.rb index 7e71b98d6..c9ea0edeb 100644 --- a/spec/unit/sidekiq_unique_jobs/lock/until_expired_spec.rb +++ b/spec/unit/sidekiq_unique_jobs/lock/until_expired_spec.rb @@ -18,12 +18,35 @@ it { is_expected.to eq(true) } end + before do + allow(empty_callback).to receive(:call) + end + describe '#execute' do subject(:execute) { lock.execute(empty_callback) } - it 'calls the callback' do - expect(empty_callback).to receive(:call) - execute + let(:locked?) { false } + + before do + allow(lock).to receive(:locked?).and_return(locked?) + end + + context 'when locked?' do + let(:locked?) { true } + + it 'calls back' do + execute + + expect(empty_callback).to have_received(:call) + end + end + + context 'when not locked?' do + it 'does not call back' do + execute + + expect(empty_callback).not_to have_received(:call) + end end end end From f90b208beaa786fd90706455eb5f4e05cad84fc2 Mon Sep 17 00:00:00 2001 From: Mikael Henriksson Date: Tue, 26 Jun 2018 14:59:10 +0200 Subject: [PATCH 7/9] Remove unused codes/tests --- lib/sidekiq_unique_jobs/unlockable.rb | 4 ---- lib/sidekiq_unique_jobs/util.rb | 5 ----- .../sidekiq_unique_jobs/lock/until_expired_spec.rb | 3 --- 3 files changed, 12 deletions(-) diff --git a/lib/sidekiq_unique_jobs/unlockable.rb b/lib/sidekiq_unique_jobs/unlockable.rb index b3eee983c..11d0bc3e7 100644 --- a/lib/sidekiq_unique_jobs/unlockable.rb +++ b/lib/sidekiq_unique_jobs/unlockable.rb @@ -13,9 +13,5 @@ def delete(item) SidekiqUniqueJobs::UniqueArgs.digest(item) SidekiqUniqueJobs::Locksmith.new(item).delete! end - - def logger - SidekiqUniqueJobs.logger - end end end diff --git a/lib/sidekiq_unique_jobs/util.rb b/lib/sidekiq_unique_jobs/util.rb index b516c3b16..366022690 100644 --- a/lib/sidekiq_unique_jobs/util.rb +++ b/lib/sidekiq_unique_jobs/util.rb @@ -62,11 +62,6 @@ def current_time Time.now end - def prefix_keys(keys) - keys = Array(keys).compact - keys.map { |key| prefix(key) } - end - def prefix(key) return key if unique_prefix.nil? return key if key.start_with?("#{unique_prefix}:") diff --git a/spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb b/spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb index a6e69f667..2f3c5b093 100644 --- a/spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb +++ b/spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb @@ -81,9 +81,6 @@ expect(callback).to have_received(:call) end - it 'prevents ' do - end - it 'callbacks are only called once (for the locked process)' do process_one.execute(callback) do process_two.execute(callback) {} From ceb4ae12a6d455fb2969eefb769a3ed3e1d4ef31 Mon Sep 17 00:00:00 2001 From: Mikael Henriksson Date: Tue, 26 Jun 2018 15:18:30 +0200 Subject: [PATCH 8/9] Adds coverage --- lib/sidekiq_unique_jobs/cli.rb | 4 - lib/sidekiq_unique_jobs/unique_args.rb | 6 +- .../lock/until_expired_spec.rb | 13 +++ .../lock/while_executing_reject_spec.rb | 24 ++++- spec/unit/sidekiq_unique_jobs/cli_spec.rb | 92 +++++++++---------- .../lock/until_and_while_executing_spec.rb | 15 +++ .../sidekiq_unique_jobs/unique_args_spec.rb | 31 +++++-- spec/unit/sidekiq_unique_jobs_spec.rb | 14 +++ 8 files changed, 131 insertions(+), 68 deletions(-) diff --git a/lib/sidekiq_unique_jobs/cli.rb b/lib/sidekiq_unique_jobs/cli.rb index c975841dc..f7a066aa2 100644 --- a/lib/sidekiq_unique_jobs/cli.rb +++ b/lib/sidekiq_unique_jobs/cli.rb @@ -40,10 +40,6 @@ def console end no_commands do - def logger - SidekiqUniqueJobs.logger - end - def console_class require 'pry' Pry diff --git a/lib/sidekiq_unique_jobs/unique_args.rb b/lib/sidekiq_unique_jobs/unique_args.rb index 1bf777288..7c5979f20 100644 --- a/lib/sidekiq_unique_jobs/unique_args.rb +++ b/lib/sidekiq_unique_jobs/unique_args.rb @@ -79,14 +79,12 @@ def filtered_args(args) when Symbol filter_by_symbol(json_args) else - log_debug { "#{__method__} arguments not filtered (using all arguments for uniqueness)" } + log_debug("#{__method__} arguments not filtered (using all arguments for uniqueness)") json_args end end def filter_by_proc(args) - return args if unique_args_method.nil? - unique_args_method.call(args) end @@ -95,7 +93,7 @@ def filter_by_symbol(args) worker_class.send(unique_args_method, args) rescue ArgumentError => ex - log_fatal ex + log_fatal(ex) args end diff --git a/spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb b/spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb index 2f3c5b093..14b9a8b85 100644 --- a/spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb +++ b/spec/integration/sidekiq_unique_jobs/lock/until_expired_spec.rb @@ -90,4 +90,17 @@ end end end + + describe '#unlock' do + context 'when lock is locked' do + before { process_one.lock } + + it 'keeps the lock even when unlocking' do + expect(process_one.unlock).to eq(true) + expect(process_one.locked?).to eq(true) + end + end + + it { expect(process_one.unlock).to eq(true) } + end end diff --git a/spec/integration/sidekiq_unique_jobs/lock/while_executing_reject_spec.rb b/spec/integration/sidekiq_unique_jobs/lock/while_executing_reject_spec.rb index c03f2fc80..a13490c6b 100644 --- a/spec/integration/sidekiq_unique_jobs/lock/while_executing_reject_spec.rb +++ b/spec/integration/sidekiq_unique_jobs/lock/while_executing_reject_spec.rb @@ -46,13 +46,27 @@ end end - it 'moves subsequent jobs to dead queue' do - process_one.execute(callback) do - expect(dead_count).to eq(0) - expect { process_two.execute(callback) {} } - .to change { dead_count }.from(0).to(1) + shared_examples 'rejects job to deadset' do + it 'moves subsequent jobs to dead queue' do + process_one.execute(callback) do + expect(dead_count).to eq(0) + expect { process_two.execute(callback) {} } + .to change { dead_count }.from(0).to(1) + end end end + + context 'when Sidekiq::DeadSet respond to kill' do + it_behaves_like 'rejects job to deadset' + end + + context 'when Sidekiq::DeadSet does not respond to kill' do + before do + allow(process_two).to receive(:deadset_kill?).and_return(false) + end + + it_behaves_like 'rejects job to deadset' + end end end end diff --git a/spec/unit/sidekiq_unique_jobs/cli_spec.rb b/spec/unit/sidekiq_unique_jobs/cli_spec.rb index 963299cd4..80e133c1b 100644 --- a/spec/unit/sidekiq_unique_jobs/cli_spec.rb +++ b/spec/unit/sidekiq_unique_jobs/cli_spec.rb @@ -18,9 +18,10 @@ let(:pattern) { '*' } describe '#help' do - let(:output) { capture(:stdout) { described_class.start(%w[help]) } } - let(:banner) do - <<~HEADER + subject(:help) { capture(:stdout) { described_class.start(%w[help]) } } + + it 'displays help' do + expect(help).to include <<~HEADER Commands: jobs console # drop into a console with easy access to helper methods jobs del PATTERN # deletes unique keys from redis by pattern @@ -29,14 +30,11 @@ HEADER end - it 'displays help' do - expect(output).to include(banner) - end - describe '#help del' do - let(:output) { capture(:stdout) { described_class.start(%w[help del]) } } - let(:banner) do - <<~HEADER + subject(:help) { capture(:stdout) { described_class.start(%w[help del]) } } + + it 'displays help about the `del` command' do + expect(help).to eq <<~HEADER Usage: jobs del PATTERN @@ -48,16 +46,13 @@ deletes unique keys from redis by pattern HEADER end - - it 'displays help about the `del` command' do - expect(output).to eq(banner) - end end describe '#help keys' do - let(:output) { capture(:stdout) { described_class.start(%w[help keys]) } } - let(:banner) do - <<~HEADER + subject(:help) { capture(:stdout) { described_class.start(%w[help keys]) } } + + it 'displays help about the `key` command' do + expect(help).to eq <<~HEADER Usage: jobs keys PATTERN @@ -68,20 +63,14 @@ list all unique keys and their expiry time HEADER end - - it 'displays help about the `key` command' do - expect(output).to eq(banner) - end end end describe '.keys' do - let(:output) { capture(:stdout) { described_class.start(%w[keys * --count 1000]) } } + subject(:keys) { capture(:stdout) { described_class.start(%w[keys * --count 1000]) } } context 'when no keys exist' do - let(:expected) { "Found 0 keys matching '#{pattern}':\n" } - - specify { expect(output).to eq(expected) } + it { is_expected.to eq("Found 0 keys matching '#{pattern}':\n") } end context 'when a key exists' do @@ -91,47 +80,52 @@ after { SidekiqUniqueJobs::Util.del('*', 1000) } - specify do - expect(output).to include("Found 3 keys matching '*':") - expect(output).to include('uniquejobs:abcdefab:EXISTS') - expect(output).to include('uniquejobs:abcdefab:GRABBED') - expect(output).to include('uniquejobs:abcdefab:GRABBED') - end + it { is_expected.to include("Found 3 keys matching '*':") } + it { is_expected.to include('uniquejobs:abcdefab:EXISTS') } + it { is_expected.to include('uniquejobs:abcdefab:GRABBED') } + it { is_expected.to include('uniquejobs:abcdefab:VERSION') } end end describe '.del' do - let(:expected) do - <<~HEADER - Deleted 3 keys matching '*' - HEADER - end + subject(:del) { capture(:stdout) { described_class.start(args) } } + + let(:args) { %W[del * #{options} --count 1000] } before do SidekiqUniqueJobs::Locksmith.new(item).lock end - specify do - output = capture(:stdout) { described_class.start(%w[del * --no-dry-run --count 1000]) } - expect(output).to eq(expected) - expect(SidekiqUniqueJobs::Util.keys).to eq([]) + context 'with argument --dry-run' do + let(:options) { '--dry-run' } + + specify do + expect(del).to eq("Would delete 3 keys matching '*'\n") + expect(SidekiqUniqueJobs::Util.keys).not_to eq([]) + end + end + + context 'with argument --no-dry-run' do + let(:options) { '--no-dry-run' } + + specify do + expect(del).to eq("Deleted 3 keys matching '*'\n") + expect(SidekiqUniqueJobs::Util.keys).to eq([]) + end end end describe '.console', ruby_ver: '>= 2.5.1' do - let(:expected) do - <<~HEADER - Use `keys '*', 1000 to display the first 1000 unique keys matching '*' - Use `del '*', 1000, true (default) to see how many keys would be deleted for the pattern '*' - Use `del '*', 1000, false to delete the first 1000 keys matching '*' - HEADER - end - let(:output) { capture(:stdout) { described_class.start(%w[console]) } } + subject(:console) { capture(:stdout) { described_class.start(%w[console]) } } specify do expect(Object).to receive(:include).with(SidekiqUniqueJobs::Util).and_return(true) allow(Pry).to receive(:start).and_return(true) - expect(output).to eq(expected) + expect(console).to eq <<~HEADER + Use `keys '*', 1000 to display the first 1000 unique keys matching '*' + Use `del '*', 1000, true (default) to see how many keys would be deleted for the pattern '*' + Use `del '*', 1000, false to delete the first 1000 keys matching '*' + HEADER end end end diff --git a/spec/unit/sidekiq_unique_jobs/lock/until_and_while_executing_spec.rb b/spec/unit/sidekiq_unique_jobs/lock/until_and_while_executing_spec.rb index 7e6c04214..9f736450c 100644 --- a/spec/unit/sidekiq_unique_jobs/lock/until_and_while_executing_spec.rb +++ b/spec/unit/sidekiq_unique_jobs/lock/until_and_while_executing_spec.rb @@ -53,4 +53,19 @@ end end end + + describe '#runtime_lock' do + subject(:runtime_lock) { lock.runtime_lock } + + it { is_expected.to be_a(SidekiqUniqueJobs::Lock::WhileExecuting) } + + it 'initializes with the right arguments' do + allow(SidekiqUniqueJobs::Lock::WhileExecuting).to receive(:new) + runtime_lock + + expect(SidekiqUniqueJobs::Lock::WhileExecuting) + .to have_received(:new) + .with(item, redis_pool) + end + end end diff --git a/spec/unit/sidekiq_unique_jobs/unique_args_spec.rb b/spec/unit/sidekiq_unique_jobs/unique_args_spec.rb index 76f8ca15d..75343c2bf 100644 --- a/spec/unit/sidekiq_unique_jobs/unique_args_spec.rb +++ b/spec/unit/sidekiq_unique_jobs/unique_args_spec.rb @@ -151,6 +151,31 @@ end end + describe '#filtered_args' do + subject(:filtered_args) { unique_args.filtered_args(args) } + + let(:args) { [1, 'test' => 'it'] } + + before do + allow(unique_args).to receive(:unique_args_method).and_return(unique_args_method) + end + + context 'when #unique_args_method is nil' do + let(:unique_args_method) { nil } + + it 'logs a debug message' do + allow(unique_args).to receive(:log_debug) + filtered_args + + expect(unique_args) + .to have_received(:log_debug) + .with('filtered_args arguments not filtered (using all arguments for uniqueness)') + end + + it { is_expected.to eq(args) } + end + end + describe '#filter_by_proc' do subject(:filter_by_proc) { unique_args.filter_by_proc(args) } @@ -164,12 +189,6 @@ it { is_expected.to eq('it') } end - context 'when #unique_args_method is nil' do - before { allow(unique_args).to receive(:unique_args_method).and_return(nil) } - - it { is_expected.to eq(args) } - end - with_default_worker_options(unique_args: ->(args) { args.first }) do it { is_expected.to eq(1) } end diff --git a/spec/unit/sidekiq_unique_jobs_spec.rb b/spec/unit/sidekiq_unique_jobs_spec.rb index 7442357d2..7a691ab18 100644 --- a/spec/unit/sidekiq_unique_jobs_spec.rb +++ b/spec/unit/sidekiq_unique_jobs_spec.rb @@ -58,6 +58,20 @@ end end + describe '.logger=' do + let(:original_logger) { Sidekiq.logger } + let(:another_logger) { Logger.new('/dev/null') } + + it 'changes the SidekiqUniqueJobs.logger' do + expect { described_class.logger = another_logger } + .to change(described_class, :logger) + .from(original_logger) + .to(another_logger) + + described_class.logger = original_logger + end + end + describe '.redis_version' do subject(:redis_version) { described_class.redis_version } From 1b633fc0f4ab00181485d4a6fcf46ce2415cdc57 Mon Sep 17 00:00:00 2001 From: Mikael Henriksson Date: Tue, 26 Jun 2018 15:23:57 +0200 Subject: [PATCH 9/9] Filter example workers from coverage --- .simplecov | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.simplecov b/.simplecov index 6da7c3fb1..15c4f7b32 100644 --- a/.simplecov +++ b/.simplecov @@ -11,7 +11,8 @@ SimpleCov.start do add_filter '/spec/' add_filter '/bin/' add_filter '/gemfiles/' - add_group 'Workers', 'examples/' + add_filter '/examples/' + add_group 'Client', 'lib/sidekiq_unique_jobs/client' add_group 'Locks', 'lib/sidekiq_unique_jobs/lock' add_group 'Server', 'lib/sidekiq_unique_jobs/server'