module JobIteration::TestHelper

Include JobIteration::TestHelper to mock interruption when testing your jobs.

Public Instance Methods

continue_iterating() click to toggle source

Removes previous stubs and tells the job to iterate until the end.

# File lib/job-iteration/test_helper.rb, line 36
def continue_iterating
  stub_shutdown_adapter_to_return(false)
end
iterate_exact_times(n_times) click to toggle source

Stubs interruption adapter to interrupt the job after every N iterations. @param [Integer] n_times Number of times before the job is interrupted @example

test "this stuff interrupts" do
  iterate_exact_times(3.times)
  MyJob.perform_now
end
# File lib/job-iteration/test_helper.rb, line 25
def iterate_exact_times(n_times)
  JobIteration.stubs(:interruption_adapter).returns(StoppingSupervisor.new(n_times.size))
end
iterate_once() click to toggle source

Stubs interruption adapter to interrupt the job after every sing iteration. @see iterate_exact_times

# File lib/job-iteration/test_helper.rb, line 31
def iterate_once
  iterate_exact_times(1.times)
end
mark_job_worker_as_interrupted() click to toggle source

Stubs the worker as already interrupted.

# File lib/job-iteration/test_helper.rb, line 41
def mark_job_worker_as_interrupted
  stub_shutdown_adapter_to_return(true)
end

Private Instance Methods

stub_shutdown_adapter_to_return(value) click to toggle source
# File lib/job-iteration/test_helper.rb, line 47
def stub_shutdown_adapter_to_return(value)
  adapter = mock
  adapter.stubs(:call).returns(value)
  JobIteration.stubs(:interruption_adapter).returns(adapter)
end