|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module JobIteration |
| 6 | + class TestHelperTest < ActiveSupport::TestCase |
| 7 | + include JobIteration::TestHelper |
| 8 | + |
| 9 | + class CounterJob < ActiveJob::Base |
| 10 | + include JobIteration::Iteration |
| 11 | + |
| 12 | + class << self |
| 13 | + attr_writer :count |
| 14 | + |
| 15 | + def count |
| 16 | + @count ||= 0 |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + def build_enumerator(times, cursor:) |
| 21 | + enumerator_builder.array(Array.new(times), cursor: cursor) |
| 22 | + end |
| 23 | + |
| 24 | + def each_iteration(_, _) |
| 25 | + self.class.count += 1 |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + teardown do |
| 30 | + CounterJob.count = nil |
| 31 | + ActiveJob::Base.queue_adapter.enqueued_jobs = [] |
| 32 | + end |
| 33 | + |
| 34 | + test "#iterate_exact_times interrupts jobs after the given number of iterations" do |
| 35 | + iterate_exact_times(3.times) |
| 36 | + |
| 37 | + CounterJob.perform_now(10) |
| 38 | + |
| 39 | + assert_equal 3, CounterJob.count |
| 40 | + end |
| 41 | + |
| 42 | + test "#iterate_once interrupts jobs after a single iteration" do |
| 43 | + iterate_once |
| 44 | + |
| 45 | + CounterJob.perform_now(10) |
| 46 | + |
| 47 | + assert_equal 1, CounterJob.count |
| 48 | + end |
| 49 | + |
| 50 | + test "#continue_iterating allows jobs to iterate until the end" do |
| 51 | + iterate_exact_times(3.times) |
| 52 | + continue_iterating |
| 53 | + |
| 54 | + CounterJob.perform_now(10) |
| 55 | + |
| 56 | + assert_equal 10, CounterJob.count |
| 57 | + end |
| 58 | + |
| 59 | + test "#iterate_once allows jobs to run one iteration at a time" do |
| 60 | + iterate_once |
| 61 | + |
| 62 | + job = CounterJob.new(10) |
| 63 | + job.perform_now |
| 64 | + job.perform_now |
| 65 | + |
| 66 | + assert_equal 2, CounterJob.count |
| 67 | + end |
| 68 | + |
| 69 | + test "#continue_iterating allows the job to finish after running initial iterations" do |
| 70 | + iterate_once |
| 71 | + |
| 72 | + job = CounterJob.new(10) |
| 73 | + job.perform_now |
| 74 | + |
| 75 | + continue_iterating |
| 76 | + |
| 77 | + job.perform_now |
| 78 | + |
| 79 | + assert_equal 10, CounterJob.count |
| 80 | + end |
| 81 | + |
| 82 | + test "#mark_job_worker_as_interrupted marks the job as interrupted" do |
| 83 | + mark_job_worker_as_interrupted |
| 84 | + |
| 85 | + CounterJob.perform_now(10) |
| 86 | + |
| 87 | + # Since we only check if we should interrupt after each iteration, the job runs once. |
| 88 | + assert_equal 1, CounterJob.count |
| 89 | + end |
| 90 | + |
| 91 | + test "interruption can be triggered by the job itself" do |
| 92 | + test_context = self |
| 93 | + job_class = Class.new(CounterJob) do |
| 94 | + define_method(:each_iteration) do |*args| |
| 95 | + super(*args) |
| 96 | + |
| 97 | + test_context.mark_job_worker_as_interrupted |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + job_class.perform_now(10) |
| 102 | + |
| 103 | + assert_equal 1, job_class.count |
| 104 | + end |
| 105 | + end |
| 106 | +end |
0 commit comments