diff options
-rw-r--r-- | railties/test/queueing/test_queue_test.rb | 85 |
1 files changed, 60 insertions, 25 deletions
diff --git a/railties/test/queueing/test_queue_test.rb b/railties/test/queueing/test_queue_test.rb index 78c6c617fe..2f0f507adb 100644 --- a/railties/test/queueing/test_queue_test.rb +++ b/railties/test/queueing/test_queue_test.rb @@ -2,22 +2,18 @@ require 'abstract_unit' require 'rails/queueing' class TestQueueTest < ActiveSupport::TestCase - class Job - def initialize(&block) - @block = block - end + def setup + @queue = Rails::Queueing::TestQueue.new + end + class ExceptionRaisingJob def run - @block.call if @block + raise end end - def setup - @queue = Rails::Queueing::TestQueue.new - end - def test_drain_raises - @queue.push Job.new { raise } + @queue.push ExceptionRaisingJob.new assert_raises(RuntimeError) { @queue.drain } end @@ -27,41 +23,80 @@ class TestQueueTest < ActiveSupport::TestCase assert_equal [1,2], @queue.jobs end + class EquivalentJob + def initialize + @initial_id = self.object_id + end + + def run + end + + def ==(other) + other.same_initial_id?(@initial_id) + end + + def same_initial_id?(other_id) + other_id == @initial_id + end + end + def test_contents assert @queue.empty? - job = Job.new + job = EquivalentJob.new @queue.push job refute @queue.empty? assert_equal job, @queue.pop end - def test_order - processed = [] + class ProcessingJob + def self.clear_processed + @processed = [] + end + + def self.processed + @processed + end + + def initialize(object) + @object = object + end + + def run + self.class.processed << @object + end + end - job1 = Job.new { processed << 1 } - job2 = Job.new { processed << 2 } + def test_order + ProcessingJob.clear_processed + job1 = ProcessingJob.new(1) + job2 = ProcessingJob.new(2) @queue.push job1 @queue.push job2 @queue.drain - assert_equal [1,2], processed + assert_equal [1,2], ProcessingJob.processed end - def test_drain - t = nil - ran = false + class ThreadTrackingJob + attr_reader :thread_id - job = Job.new do - ran = true - t = Thread.current + def run + @thread_id = Thread.current.object_id end - @queue.push job + def ran? + @thread_id + end + end + + def test_drain + @queue.push ThreadTrackingJob.new + job = @queue.jobs.last @queue.drain assert @queue.empty? - assert ran, "The job runs synchronously when the queue is drained" - assert_not_equal t, Thread.current + assert job.ran?, "The job runs synchronously when the queue is drained" + assert_not_equal job.thread_id, Thread.current.object_id end end |