Sha256: 894ee1fcd1453ccf050956704fc7c210652a2cd4d02cdc63c93265a0fe27e883

Contents?: true

Size: 1.48 KB

Versions: 36

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module JobIteration
  # Include JobIteration::TestHelper to mock interruption when testing your jobs.
  module TestHelper
    class StoppingSupervisor # @private
      def initialize(stop_after_count)
        @stop_after_count = stop_after_count
        @calls = 0
      end

      def call
        @calls += 1
        (@calls % @stop_after_count) == 0
      end
    end

    # 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
    def iterate_exact_times(n_times)
      JobIteration.stubs(:interruption_adapter).returns(StoppingSupervisor.new(n_times.size))
    end

    # Stubs interruption adapter to interrupt the job after every sing iteration.
    # @see #iterate_exact_times
    def iterate_once
      iterate_exact_times(1.times)
    end

    # Removes previous stubs and tells the job to iterate until the end.
    def continue_iterating
      stub_shutdown_adapter_to_return(false)
    end

    # Stubs the worker as already interrupted.
    def mark_job_worker_as_interrupted
      stub_shutdown_adapter_to_return(true)
    end

    private

    def stub_shutdown_adapter_to_return(value)
      adapter = mock
      adapter.stubs(:call).returns(value)
      JobIteration.stubs(:interruption_adapter).returns(adapter)
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
job-iteration-1.7.0 lib/job-iteration/test_helper.rb
job-iteration-1.6.0 lib/job-iteration/test_helper.rb
job-iteration-1.5.1 lib/job-iteration/test_helper.rb
job-iteration-1.5.0 lib/job-iteration/test_helper.rb
job-iteration-1.4.1 lib/job-iteration/test_helper.rb
job-iteration-1.4.0 lib/job-iteration/test_helper.rb
job-iteration-1.3.6 lib/job-iteration/test_helper.rb
job-iteration-1.3.5 lib/job-iteration/test_helper.rb
job-iteration-1.3.4 lib/job-iteration/test_helper.rb
job-iteration-1.3.3 lib/job-iteration/test_helper.rb
job-iteration-1.3.2 lib/job-iteration/test_helper.rb
job-iteration-1.3.1 lib/job-iteration/test_helper.rb
job-iteration-1.3.0 lib/job-iteration/test_helper.rb
job-iteration-1.2.0 lib/job-iteration/test_helper.rb
job-iteration-1.1.14 lib/job-iteration/test_helper.rb
job-iteration-1.1.13 lib/job-iteration/test_helper.rb
job-iteration-1.1.12 lib/job-iteration/test_helper.rb
job-iteration-1.1.11 lib/job-iteration/test_helper.rb
job-iteration-1.1.10 lib/job-iteration/test_helper.rb
job-iteration-1.1.9 lib/job-iteration/test_helper.rb