Skip to content

Commit

Permalink
Test existing global max_job_runtime behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
sambostock committed Jul 19, 2022
1 parent c9f2f7c commit 318fa3a
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions test/unit/iteration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,34 @@ def test_jobs_using_on_complete_have_accurate_total_time
end
end

def test_global_max_job_runtime
freeze_time
with_global_max_job_runtime(1.minute) do
klass = build_slow_job_class(iterations: 3, iteration_duration: 30.seconds)
klass.perform_now
assert_partially_completed_job(cursor_position: 2)
end
end

private

# Allows building job classes that read max_job_runtime during the test,
# instead of when this file is read
def build_slow_job_class(iterations: 3, iteration_duration: 30.seconds)
Class.new(ActiveJob::Base) do
include JobIteration::Iteration
include ActiveSupport::Testing::TimeHelpers

define_method(:build_enumerator) do |cursor:|
enumerator_builder.build_times_enumerator(iterations, cursor: cursor)
end

define_method(:each_iteration) do |*|
travel(iteration_duration)
end
end
end

def assert_raises_cursor_error(&block)
error = assert_raises(JobIteration::Iteration::CursorError, &block)
inspected_cursor =
Expand All @@ -261,6 +287,13 @@ def assert_raises_cursor_error(&block)
)
end

def assert_partially_completed_job(cursor_position:)
message = "Expected to find partially completed job enqueued with cursor_position: #{cursor_position}"
enqueued_job = ActiveJob::Base.queue_adapter.enqueued_jobs.first
refute_nil(enqueued_job, message)
assert_equal(cursor_position, enqueued_job.fetch("cursor_position"))
end

def push(job, *args)
job.perform_later(*args)
end
Expand All @@ -269,4 +302,12 @@ def work_one_job
job = ActiveJob::Base.queue_adapter.enqueued_jobs.pop
ActiveJob::Base.execute(job)
end

def with_global_max_job_runtime(new)
original = JobIteration.max_job_runtime
JobIteration.max_job_runtime = new
yield
ensure
JobIteration.max_job_runtime = original
end
end

0 comments on commit 318fa3a

Please sign in to comment.