Skip to content

Commit

Permalink
Inheritance over composition
Browse files Browse the repository at this point in the history
  • Loading branch information
Srivathsava Rangarajan committed Feb 17, 2016
1 parent ef9a01b commit a2799d5
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 130 deletions.
2 changes: 0 additions & 2 deletions lib/sidekiq-unique-jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
require 'sidekiq_unique_jobs/cli'
require 'sidekiq_unique_jobs/core_ext'
require 'sidekiq_unique_jobs/timeout_calculator'
require 'sidekiq_unique_jobs/queue_lock_timeout_calculator'
require 'sidekiq_unique_jobs/run_lock_timeout_calculator'
require 'sidekiq_unique_jobs/options_with_fallback'
require 'sidekiq_unique_jobs/scripts'
require 'sidekiq_unique_jobs/unique_args'
Expand Down
25 changes: 0 additions & 25 deletions lib/sidekiq_unique_jobs/queue_lock_timeout_calculator.rb

This file was deleted.

25 changes: 0 additions & 25 deletions lib/sidekiq_unique_jobs/run_lock_timeout_calculator.rb

This file was deleted.

33 changes: 32 additions & 1 deletion lib/sidekiq_unique_jobs/timeout_calculator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module SidekiqUniqueJobs
module TimeoutCalculator
class TimeoutCalculator
def self.for_item(item)
new(item)
end

def initialize(item)
@item = item
end

def time_until_scheduled
scheduled = item[AT_KEY]
return 0 unless scheduled
Expand Down Expand Up @@ -31,4 +39,27 @@ def worker_class_expiration_for(key)

attr_reader :item
end

class RunLockTimeoutCalculator < TimeoutCalculator
def seconds
@seconds ||= (
worker_class_run_lock_expiration ||
SidekiqUniqueJobs.config.default_run_lock_expiration
).to_i
end
end

class QueueLockTimeoutCalculator < TimeoutCalculator
def seconds
queue_lock_expiration + time_until_scheduled
end

def queue_lock_expiration
@queue_lock_expiration ||=
(
worker_class_queue_lock_expiration ||
SidekiqUniqueJobs.config.default_queue_lock_expiration
).to_i
end
end
end
3 changes: 2 additions & 1 deletion spec/lib/sidekiq_unique_jobs/client/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ def do_it(_arg)
# jobs are set around the same time as the scheduled job itself feel free to improve.
it 'expires the digest when a scheduled job is scheduled at' do
expected_expires_at =
(Time.at(Time.now.to_i + 15 * 60) - Time.now.utc) + SidekiqUniqueJobs.config.default_queue_lock_expiration
(Time.at(Time.now.to_i + 15 * 60) - Time.now.utc) +
SidekiqUniqueJobs.config.default_queue_lock_expiration
jid = MyUniqueJob.perform_in(expected_expires_at, 'mike')
item = { 'class' => MyUniqueJob,
'queue' => 'customqueue',
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/sidekiq_unique_jobs/lock/while_executing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@

worker = SidekiqUniqueJobs.worker_class_constantize(item[SidekiqUniqueJobs::CLASS_KEY])
allow(worker).to receive(:get_sidekiq_options)
.and_return({"retry"=>true,
"queue"=>:dupsallowed,
"run_lock_expiration" => 0,
"unique"=>:until_and_while_executing})
.and_return('retry' => true,
'queue' => :dupsallowed,
'run_lock_expiration' => 0,
'unique' => :until_and_while_executing)

start_times = []
sleep_time = 0.1
Expand Down
18 changes: 12 additions & 6 deletions spec/lib/sidekiq_unique_jobs/queue_lock_timeout_calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

describe 'public api' do
it_behaves_like 'generic unscheduled job' do
it { is_expected.to respond_to(:worker_class_queue_lock_expiration) }
it { is_expected.to respond_to(:seconds) }
it { is_expected.to respond_to(:queue_lock_expiration) }
end
end

Expand All @@ -30,12 +30,18 @@
end

describe '#queue_lock_expiration' do
it_behaves_like 'generic unscheduled job' do
its(:queue_lock_expiration) { is_expected.to eq(SidekiqUniqueJobs.config.default_queue_lock_expiration) }
context 'using default unique_expiration' do
subject { described_class.new(nil) }
before { allow(subject).to receive(:worker_class_queue_lock_expiration).and_return(nil) }

its(:queue_lock_expiration) { is_expected.to eq(1_800) }
end

subject { described_class.new('class' => 'MyUniqueJob') }
its(:queue_lock_expiration) { is_expected.to eq(7_200) }
end
context 'using specified sidekiq option unique_expiration' do
subject { described_class.new(nil) }
before { allow(subject).to receive(:worker_class_queue_lock_expiration).and_return(9) }

its(:queue_lock_expiration) { is_expected.to eq(9) }
end
end
end
22 changes: 9 additions & 13 deletions spec/lib/sidekiq_unique_jobs/run_lock_timeout_calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

describe 'public api' do
it_behaves_like 'generic unscheduled job' do
it { is_expected.to respond_to(:worker_class_run_lock_expiration) }
it { is_expected.to respond_to(:seconds) }
end
end
Expand All @@ -20,21 +19,18 @@
end

describe '#seconds' do
subject { described_class.new(nil) }
context 'using default run_lock_expiration' do
subject { described_class.new(nil) }
before { allow(subject).to receive(:worker_class_run_lock_expiration).and_return(9) }

before do
allow(subject).to receive(:run_lock_expiration).and_return(9)
its(:seconds) { is_expected.to eq(9) }
end
its(:seconds) { is_expected.to eq(9) }
end

describe '#run_lock_expiration' do
it_behaves_like 'generic unscheduled job' do
its(:run_lock_expiration) { is_expected.to eq(SidekiqUniqueJobs.config.default_run_lock_expiration) }
end
context 'using specified sidekiq option run_lock_expiration' do
subject { described_class.new(nil) }
before { allow(subject).to receive(:worker_class_run_lock_expiration).and_return(nil) }

subject { described_class.new('class' => 'LongRunningJob') }
its(:run_lock_expiration) { is_expected.to eq(7_200) }
its(:seconds) { is_expected.to eq(60) }
end
end

end
77 changes: 24 additions & 53 deletions spec/lib/sidekiq_unique_jobs/timeout_calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,35 @@

RSpec.describe SidekiqUniqueJobs::TimeoutCalculator do
shared_context 'undefined worker class' do
subject do
Class.new do
include SidekiqUniqueJobs::TimeoutCalculator
def initialize(item)
@item = item
end
end.new('class' => 'test')
end
subject { described_class.new('class' => 'test') }
end

shared_context 'generic unscheduled job' do
subject do
Class.new do
include SidekiqUniqueJobs::TimeoutCalculator
def initialize(item)
@item = item
end
end.new('class' => 'MyUniqueJob')
end
shared_context 'item not scheduled' do
subject { described_class.new('class' => 'MyUniqueJob') }
end

describe 'public api' do
it_behaves_like 'generic unscheduled job' do
it { is_expected.to respond_to(:time_until_scheduled) }
it { is_expected.to respond_to(:worker_class_queue_lock_expiration) }
it { is_expected.to respond_to(:worker_class_run_lock_expiration) }
it { is_expected.to respond_to(:worker_class) }
subject { described_class.new(nil) }
it { is_expected.to respond_to(:time_until_scheduled) }
it { is_expected.to respond_to(:worker_class_queue_lock_expiration) }
it { is_expected.to respond_to(:worker_class_run_lock_expiration) }
it { is_expected.to respond_to(:worker_class) }
it { is_expected.to respond_to(:seconds) }
end

describe '.for_item' do
it 'initializes a new calculator' do
expect(described_class).to receive(:new).with('WAT')
described_class.for_item('WAT')
end
end

describe '#time_until_scheduled' do
it_behaves_like 'generic unscheduled job' do
it_behaves_like 'item not scheduled' do
its(:time_until_scheduled) { is_expected.to eq(0) }
end

subject do
Class.new do
include SidekiqUniqueJobs::TimeoutCalculator
def initialize(item)
@item = item
end
end.new('class' => 'MyUniqueJob', 'at' => schedule_time)
end
subject { described_class.new('class' => 'MyUniqueJob', 'at' => schedule_time) }
let(:schedule_time) { Time.now.utc.to_i + 24 * 60 * 60 }
let(:now_in_utc) { Time.now.utc.to_i }

Expand All @@ -57,43 +43,28 @@ def initialize(item)

describe '#worker_class_queue_lock_expiration' do
it_behaves_like 'undefined worker class' do
its (:worker_class_queue_lock_expiration) { is_expected.to eq(nil) }
its(:worker_class_queue_lock_expiration) { is_expected.to eq(nil) }
end

it_behaves_like 'generic unscheduled job' do
its (:worker_class_queue_lock_expiration) { is_expected.to eq(7_200) }
end
subject { described_class.new('class' => 'MyUniqueJob') }
its(:worker_class_queue_lock_expiration) { is_expected.to eq(7_200) }
end

describe '#worker_class_run_lock_expiration' do
it_behaves_like 'undefined worker class' do
its (:worker_class_queue_lock_expiration) { is_expected.to eq(nil) }
its(:worker_class_run_lock_expiration) { is_expected.to eq(nil) }
end

subject do
Class.new do
include SidekiqUniqueJobs::TimeoutCalculator
def initialize(item)
@item = item
end
end.new('class' => 'LongRunningJob')
end
its (:worker_class_run_lock_expiration) { is_expected.to eq(7_200) }
subject { described_class.new('class' => 'LongRunningJob') }
its(:worker_class_run_lock_expiration) { is_expected.to eq(7_200) }
end

describe '#worker_class' do
it_behaves_like 'undefined worker class' do
its(:worker_class) { is_expected.to eq('test') }
end

subject do
Class.new do
include SidekiqUniqueJobs::TimeoutCalculator
def initialize(item)
@item = item
end
end.new('class' => 'MyJob')
end
subject { described_class.new('class' => 'MyJob') }
its(:worker_class) { is_expected.to eq(MyJob) }
end
end

0 comments on commit a2799d5

Please sign in to comment.