From 2187b5f2f1d383b7a46baf72334c9a3332a23e84 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 27 Apr 2012 14:26:26 -0700 Subject: Extend stdlib queue for the test queue --- railties/lib/rails/queueing.rb | 20 ++------------------ railties/test/queueing/test_queue_test.rb | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/railties/lib/rails/queueing.rb b/railties/lib/rails/queueing.rb index 4892f85ae1..516447315f 100644 --- a/railties/lib/rails/queueing.rb +++ b/railties/lib/rails/queueing.rb @@ -7,27 +7,11 @@ module Rails # # Jobs are run in a separate thread to catch mistakes where code # assumes that the job is run in the same thread. - class TestQueue - attr_reader :contents - - def initialize - @contents = [] - end - + class TestQueue < ::Queue def drain # run the jobs in a separate thread so assumptions of synchronous # jobs are caught in test mode. - t = Thread.new do - while job = @contents.shift - job.run - end - end - t.join - end - - # implement the Queue API - def push(object) - @contents << object + Thread.new { pop.run until empty? }.join end end diff --git a/railties/test/queueing/test_queue_test.rb b/railties/test/queueing/test_queue_test.rb index 466af22351..fbb09c6fa5 100644 --- a/railties/test/queueing/test_queue_test.rb +++ b/railties/test/queueing/test_queue_test.rb @@ -3,9 +3,7 @@ require 'rails/queueing' class TestQueueTest < ActiveSupport::TestCase class Job - attr_reader :id - def initialize(id, &block) - @id = id + def initialize(&block) @block = block end @@ -15,21 +13,23 @@ class TestQueueTest < ActiveSupport::TestCase end def setup + #@queue = Rails::Queueing::TestQueue.new @queue = Rails::Queueing::TestQueue.new end def test_contents - assert_equal [], @queue.contents - job = Job.new(1) + assert @queue.empty? + job = Job.new @queue.push job - assert_equal [job], @queue.contents + refute @queue.empty? + assert_equal job, @queue.pop end def test_order processed = [] - job1 = Job.new(1) { processed << 1 } - job2 = Job.new(2) { processed << 2 } + job1 = Job.new { processed << 1 } + job2 = Job.new { processed << 2 } @queue.push job1 @queue.push job2 @@ -42,7 +42,7 @@ class TestQueueTest < ActiveSupport::TestCase t = nil ran = false - job = Job.new(1) do + job = Job.new do ran = true t = Thread.current end @@ -50,7 +50,7 @@ class TestQueueTest < ActiveSupport::TestCase @queue.push job @queue.drain - assert_equal [], @queue.contents + assert @queue.empty? assert ran, "The job runs synchronously when the queue is drained" assert_not_equal t, Thread.current end -- cgit v1.2.3