Skip to content

Commit

Permalink
Merge pull request #18266 from cristianbica/ajfixes
Browse files Browse the repository at this point in the history
ActiveJob testing improvements
  • Loading branch information
rafaelfranca committed Dec 31, 2014
2 parents 04852b8 + 8a73f4b commit 0d1ab34
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion activejob/Rakefile
@@ -1,7 +1,7 @@
require 'rake/testtask'
require 'rubygems/package_task'

ACTIVEJOB_ADAPTERS = %w(inline delayed_job qu que queue_classic resque sidekiq sneakers sucker_punch backburner)
ACTIVEJOB_ADAPTERS = %w(inline delayed_job qu que queue_classic resque sidekiq sneakers sucker_punch backburner test)
ACTIVEJOB_ADAPTERS -= %w(queue_classic) if defined?(JRUBY_VERSION)

task default: :test
Expand Down
6 changes: 3 additions & 3 deletions activejob/lib/active_job/queue_adapter.rb
Expand Up @@ -2,7 +2,7 @@
require 'active_support/core_ext/string/inflections'

module ActiveJob
# The <tt>ActionJob::QueueAdapter</tt> module is used to load the
# The <tt>ActionJob::QueueAdapter</tt> module is used to load the
# correct adapter. The default queue adapter is the :inline queue.
module QueueAdapter #:nodoc:
extend ActiveSupport::Concern
Expand All @@ -21,8 +21,8 @@ def queue_adapter=(name_or_adapter)
ActiveJob::QueueAdapters::TestAdapter.new
when Symbol, String
load_adapter(name_or_adapter)
when Class
name_or_adapter
else
name_or_adapter if name_or_adapter.respond_to?(:enqueue)
end
end

Expand Down
17 changes: 11 additions & 6 deletions activejob/lib/active_job/queue_adapters/test_adapter.rb
Expand Up @@ -14,6 +14,11 @@ class TestAdapter
attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs)
attr_writer(:enqueued_jobs, :performed_jobs)

def initialize
self.perform_enqueued_jobs = false
self.perform_enqueued_at_jobs = false
end

# Provides a store of all the enqueued jobs with the TestAdapter so you can check them.
def enqueued_jobs
@enqueued_jobs ||= []
Expand All @@ -26,19 +31,19 @@ def performed_jobs

def enqueue(job) #:nodoc:
if perform_enqueued_jobs
performed_jobs << {job: job.class, args: job.arguments, queue: job.queue_name}
job.perform_now
performed_jobs << {job: job.class, args: job.serialize['arguments'], queue: job.queue_name}
Base.execute job.serialize
else
enqueued_jobs << {job: job.class, args: job.arguments, queue: job.queue_name}
enqueued_jobs << {job: job.class, args: job.serialize['arguments'], queue: job.queue_name}
end
end

def enqueue_at(job, timestamp) #:nodoc:
if perform_enqueued_at_jobs
performed_jobs << {job: job.class, args: job.arguments, queue: job.queue_name, at: timestamp}
job.perform_now
performed_jobs << {job: job.class, args: job.serialize['arguments'], queue: job.queue_name, at: timestamp}
Base.execute job.serialize
else
enqueued_jobs << {job: job.class, args: job.arguments, queue: job.queue_name, at: timestamp}
enqueued_jobs << {job: job.class, args: job.serialize['arguments'], queue: job.queue_name, at: timestamp}
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions activejob/lib/active_job/test_helper.rb
@@ -1,3 +1,5 @@
require 'active_support/core_ext/hash/keys'

module ActiveJob
# Provides helper methods for testing Active Job
module TestHelper
Expand Down
3 changes: 3 additions & 0 deletions activejob/test/adapters/test.rb
@@ -0,0 +1,3 @@
ActiveJob::Base.queue_adapter = :test
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = true
3 changes: 1 addition & 2 deletions activejob/test/cases/adapter_test.rb
Expand Up @@ -2,7 +2,6 @@

class AdapterTest < ActiveSupport::TestCase
test "should load #{ENV['AJADAPTER']} adapter" do
ActiveJob::Base.queue_adapter = ENV['AJADAPTER'].to_sym
assert_equal "active_job/queue_adapters/#{ENV['AJADAPTER']}_adapter".classify.constantize, ActiveJob::Base.queue_adapter
assert_equal "active_job/queue_adapters/#{ENV['AJADAPTER']}_adapter".classify, ActiveJob::Base.queue_adapter.name
end
end

0 comments on commit 0d1ab34

Please sign in to comment.