Permalink
Browse files

Extend stdlib queue for the test queue

  • Loading branch information...
1 parent 673fe05 commit 2187b5f2f1d383b7a46baf72334c9a3332a23e84 @tenderlove tenderlove committed Apr 27, 2012
Showing with 12 additions and 28 deletions.
  1. +2 −18 railties/lib/rails/queueing.rb
  2. +10 −10 railties/test/queueing/test_queue_test.rb
@@ -7,27 +7,11 @@ module Queueing
#
# Jobs are run in a separate thread to catch mistakes where code
@josevalim

josevalim Apr 27, 2012

Contributor

Bro, you forgot to update the documentation. It was saying explicitly there is a method contents you could call to get the jobs added.

@josevalim

josevalim Apr 27, 2012

Contributor

I also don't like this approach much because it makes it harder to inspect what is in the queue, which you may want to do in the test environment.

# 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
@@ -3,9 +3,7 @@
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 @@ def run
end
def setup
+ #@queue = Rails::Queueing::TestQueue.new
@sobrinho

sobrinho Apr 29, 2012

Contributor

leftover comment :)

@vijaydev

vijaydev Apr 29, 2012

Member

this was fixed later.

@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,15 +42,15 @@ def test_drain
t = nil
ran = false
- job = Job.new(1) do
+ job = Job.new do
ran = true
t = Thread.current
end
@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

0 comments on commit 2187b5f

Please sign in to comment.