Skip to content

Commit

Permalink
Extend stdlib queue for the test queue
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Apr 27, 2012
1 parent 673fe05 commit 2187b5f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
20 changes: 2 additions & 18 deletions railties/lib/rails/queueing.rb
Expand Up @@ -7,27 +7,11 @@ module Queueing
#
# Jobs are run in a separate thread to catch mistakes where code

This comment has been minimized.

Copy link
@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.

This comment has been minimized.

Copy link
@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

Expand Down
20 changes: 10 additions & 10 deletions railties/test/queueing/test_queue_test.rb
Expand Up @@ -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

Expand All @@ -15,21 +13,23 @@ def run
end

def setup
#@queue = Rails::Queueing::TestQueue.new

This comment has been minimized.

Copy link
@sobrinho

sobrinho Apr 29, 2012

Contributor

leftover comment :)

This comment has been minimized.

Copy link
@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
Expand All @@ -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
Expand Down

0 comments on commit 2187b5f

Please sign in to comment.