Sha256: 844f5736bc27f472b91357927ece605ef748c01c6dfe62f00b8046cf2029aa1e

Contents?: true

Size: 1.74 KB

Versions: 35

Compression:

Stored size: 1.74 KB

Contents

module ActiveJob
  module QueueAdapters
    # == Test adapter for Active Job
    #
    # The test adapter should be used only in testing. Along with
    # <tt>ActiveJob::TestCase</tt> and <tt>ActiveJob::TestHelper</tt>
    # it makes a great tool to test your Rails application.
    #
    # To use the test adapter set queue_adapter config to +:test+.
    #
    #   Rails.application.config.active_job.queue_adapter = :test
    class TestAdapter
      attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter)
      attr_writer(:enqueued_jobs, :performed_jobs)

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

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

      def enqueue(job) #:nodoc:
        return if filtered?(job)

        job_data = job_to_hash(job)
        enqueue_or_perform(perform_enqueued_jobs, job, job_data)
      end

      def enqueue_at(job, timestamp) #:nodoc:
        return if filtered?(job)

        job_data = job_to_hash(job, at: timestamp)
        enqueue_or_perform(perform_enqueued_at_jobs, job, job_data)
      end

      private

      def job_to_hash(job, extras = {})
        { job: job.class, args: job.serialize.fetch('arguments'), queue: job.queue_name }.merge!(extras)
      end

      def enqueue_or_perform(perform, job, job_data)
        if perform
          performed_jobs << job_data
          Base.execute job.serialize
        else
          enqueued_jobs << job_data
        end
      end

      def filtered?(job)
        filter && !Array(filter).include?(job.class)
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 4 rubygems

Version Path
activejob-5.0.7.2 lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.7.1 lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.7 lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.6 lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.6.rc1 lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.5 lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.5.rc2 lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.5.rc1 lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.4 lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.4.rc1 lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.3 lib/active_job/queue_adapters/test_adapter.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activejob-5.0.2/lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.2 lib/active_job/queue_adapters/test_adapter.rb
activejob-5.0.2.rc1 lib/active_job/queue_adapters/test_adapter.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activejob-5.0.1/lib/active_job/queue_adapters/test_adapter.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activejob-5.0.1/lib/active_job/queue_adapters/test_adapter.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activejob-5.0.1/lib/active_job/queue_adapters/test_adapter.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activejob-5.0.1/lib/active_job/queue_adapters/test_adapter.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activejob-5.0.1/lib/active_job/queue_adapters/test_adapter.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activejob-5.0.1/lib/active_job/queue_adapters/test_adapter.rb