aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/lib/active_job/queue_adapters/test_adapter.rb
blob: 185d6fc7e65c9bc9f3ccaba154394b77b81b3d83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module ActiveJob
  module QueueAdapters
    class TestAdapter
      attr_accessor(:perform_enqueued_jobs) { false }
      attr_accessor(:perform_enqueued_at_jobs) { false }
      delegate :name, to: :class

      # Provides a store of all the enqueued jobs with the TestAdapter so you can check them.
      def enqueued_jobs
        @enqueued_jobs ||= []
      end

      # Allows you to overwrite the default enqueued jobs store from an array to some
      # other object.  If you just want to clear the store,
      # call ActiveJob::QueueAdapters::TestAdapter.enqueued_jobs.clear.
      #
      # If you place another object here, please make sure it responds to:
      #
      # * << (message)
      # * clear
      # * length
      # * size
      # * and other common Array methods
      def enqueued_jobs=(val)
        @enqueued_jobs = val
      end

      # Provides a store of all the performed jobs with the TestAdapter so you can check them.
      def performed_jobs
        @performed_jobs ||= []
      end

      # Allows you to overwrite the default performed jobs store from an array to some
      # other object.  If you just want to clear the store,
      # call ActiveJob::QueueAdapters::TestAdapter.performed_jobs.clear.
      #
      # If you place another object here, please make sure it responds to:
      #
      # * << (message)
      # * clear
      # * length
      # * size
      # * and other common Array methods
      def performed_jobs=(val)
        @performed_jobs = val
      end

      def enqueue(job, *args)
        if perform_enqueued_jobs?
          performed_jobs << {job: job, args: args, queue: job.queue_name}
          job.new.execute(*args)
        else
          enqueued_jobs << {job: job, args: args, queue: job.queue_name}
        end
      end

      def enqueue_at(job, timestamp, *args)
        if perform_enqueued_at_jobs?
          performed_jobs << {job: job, args: args, queue: job.queue_name, run_at: timestamp}
          job.new.execute(*args)
        else
          enqueued_jobs << {job: job, args: args, queue: job.queue_name, run_at: timestamp}
        end
      end

      private
        def perform_enqueued_jobs?
          perform_enqueued_jobs
        end

        def perform_enqueued_at_jobs?
          perform_enqueued_at_jobs
        end
    end
  end
end