From 75bf4a939ada5c5a87f0e586216dbaad0ad7b653 Mon Sep 17 00:00:00 2001 From: "matej.risek" Date: Thu, 21 Sep 2023 09:30:24 +0200 Subject: [PATCH] Handle strategy fallbacks properly If strategy was set for server and not for client in a hash - that would lead to on_client_conflict to return a hash back. Which would in turn lead to an error in on_conflict.rb --- lib/sidekiq_unique_jobs/lock_config.rb | 14 ++++++++++---- spec/sidekiq_unique_jobs/lock_config_spec.rb | 12 ++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/sidekiq_unique_jobs/lock_config.rb b/lib/sidekiq_unique_jobs/lock_config.rb index 9c30dd59e..a0fdb613f 100644 --- a/lib/sidekiq_unique_jobs/lock_config.rb +++ b/lib/sidekiq_unique_jobs/lock_config.rb @@ -113,14 +113,20 @@ def errors_as_string # the strategy to use as conflict resolution from sidekiq client def on_client_conflict - @on_client_conflict ||= on_conflict["client"] || on_conflict[:client] if on_conflict.is_a?(Hash) - @on_client_conflict ||= on_conflict + if on_conflict.is_a?(Hash) + @on_client_conflict ||= on_conflict["client"] || on_conflict[:client] + else + @on_client_conflict ||= on_conflict + end end # the strategy to use as conflict resolution from sidekiq server def on_server_conflict - @on_server_conflict ||= on_conflict["server"] || on_conflict[:server] if on_conflict.is_a?(Hash) - @on_server_conflict ||= on_conflict + if on_conflict.is_a?(Hash) + @on_server_conflict ||= on_conflict["server"] || on_conflict[:server] + else + @on_server_conflict ||= on_conflict + end end end end diff --git a/spec/sidekiq_unique_jobs/lock_config_spec.rb b/spec/sidekiq_unique_jobs/lock_config_spec.rb index cce5825f5..97782ad68 100644 --- a/spec/sidekiq_unique_jobs/lock_config_spec.rb +++ b/spec/sidekiq_unique_jobs/lock_config_spec.rb @@ -146,6 +146,12 @@ it { is_expected.to eq(:replace) } end + + context "when on_conflict is defined only for server" do + let(:on_conflict) { {server: :log}} + + it { is_expected.to be_nil } + end end describe "#on_server_conflict" do @@ -158,5 +164,11 @@ it { is_expected.to eq(:reschedule) } end + + context "when on_conflict is defined only for client" do + let(:on_conflict) { {client: :log}} + + it { is_expected.to be_nil } + end end end