lib/sidekiq/testing.rb in sidekiq-4.0.0 vs lib/sidekiq/testing.rb in sidekiq-4.0.1

- old
+ new

@@ -66,85 +66,27 @@ alias_method :raw_push_real, :raw_push def raw_push(payloads) if Sidekiq::Testing.fake? payloads.each do |job| - Queues.jobs[job['queue']] << Sidekiq.load_json(Sidekiq.dump_json(job)) + job['class'].constantize.jobs << Sidekiq.load_json(Sidekiq.dump_json(job)) end true elsif Sidekiq::Testing.inline? payloads.each do |job| + job['jid'] ||= SecureRandom.hex(12) klass = job['class'].constantize - job['id'] ||= SecureRandom.hex(12) - job_hash = Sidekiq.load_json(Sidekiq.dump_json(job)) - klass.process_job(job_hash) + klass.jobs.unshift Sidekiq.load_json(Sidekiq.dump_json(job)) + klass.perform_one end true else raw_push_real(payloads) end end end - module Queues - ## - # The Queues class is only for testing the fake queue implementation. - # The data is structured as a hash with queue name as hash key and array - # of job data as the value. - # - # { - # "default"=>[ - # { - # "class"=>"TestTesting::QueueWorker", - # "args"=>[1, 2], - # "retry"=>true, - # "queue"=>"default", - # "jid"=>"abc5b065c5c4b27fc1102833", - # "created_at"=>1447445554.419934 - # } - # ] - # } - # - # Example: - # - # require 'sidekiq/testing' - # - # assert_equal 0, Sidekiq::Queues["default"].size - # HardWorker.perform_async(:something) - # assert_equal 1, Sidekiq::Queues["default"].size - # assert_equal :something, Sidekiq::Queues["default"].first['args'][0] - # - # You can also clear all workers' jobs: - # - # assert_equal 0, Sidekiq::Queues["default"].size - # HardWorker.perform_async(:something) - # Sidekiq::Queues.clear_all - # assert_equal 0, Sidekiq::Queues["default"].size - # - # This can be useful to make sure jobs don't linger between tests: - # - # RSpec.configure do |config| - # config.before(:each) do - # Sidekiq::Queues.clear_all - # end - # end - # - class << self - def [](queue) - jobs[queue.to_s] - end - - def jobs - @jobs ||= Hash.new { |hash, key| hash[key] = [] } - end - - def clear_all - jobs.clear - end - end - end - module Worker ## # The Sidekiq testing infrastructure overrides perform_async # so that it does not actually touch the network. Instead it # stores the asynchronous jobs in a per-class array so that @@ -199,40 +141,32 @@ # When I sign up as "foo@example.com" # Then I should receive a welcome email to "foo@example.com" # module ClassMethods - # Queue for this worker - def queue - self.sidekiq_options["queue"].to_s - end - # Jobs queued for this worker def jobs - Queues.jobs[queue].select { |job| job["class"] == self.to_s } + Worker.jobs[self] end # Clear all jobs for this worker def clear - Queues.jobs[queue].clear + jobs.clear end # Drain and run all jobs for this worker def drain - while jobs.any? - next_job = jobs.first - Queues.jobs[queue].delete_if { |job| job["jid"] == next_job["jid"] } - process_job(next_job) + while job = jobs.shift do + process_job(job) end end # Pop out a single job and perform it def perform_one raise(EmptyQueueError, "perform_one called with empty job queue") if jobs.empty? - next_job = jobs.first - Queues.jobs[queue].delete_if { |job| job["jid"] == next_job["jid"] } - process_job(next_job) + job = jobs.shift + process_job(job) end def process_job(job) worker = new worker.jid = job['jid'] @@ -247,25 +181,21 @@ end end class << self def jobs # :nodoc: - Queues.jobs.values.flatten + @jobs ||= Hash.new { |hash, key| hash[key] = [] } end # Clear all queued jobs across all workers def clear_all - Queues.clear_all + jobs.clear end # Drain all queued jobs across all workers def drain_all - while jobs.any? - worker_classes = jobs.map { |job| job["class"] }.uniq - - worker_classes.each do |worker_class| - worker_class.constantize.drain - end + until jobs.values.all?(&:empty?) do + jobs.keys.each(&:drain) end end end end end