mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make TestQueueTest work with marshalling queue
This requires all jobs to be instances of named classes, without block implementations of methods.
This commit is contained in:
parent
a3ade2e99c
commit
b44104ae13
1 changed files with 63 additions and 28 deletions
|
@ -2,22 +2,18 @@ require 'abstract_unit'
|
|||
require 'rails/queueing'
|
||||
|
||||
class TestQueueTest < ActiveSupport::TestCase
|
||||
class Job
|
||||
def initialize(&block)
|
||||
@block = block
|
||||
end
|
||||
|
||||
def run
|
||||
@block.call if @block
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
@queue = Rails::Queueing::TestQueue.new
|
||||
end
|
||||
|
||||
class ExceptionRaisingJob
|
||||
def run
|
||||
raise
|
||||
end
|
||||
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
|
||||
|
||||
job1 = Job.new { processed << 1 }
|
||||
job2 = Job.new { processed << 2 }
|
||||
def self.processed
|
||||
@processed
|
||||
end
|
||||
|
||||
def initialize(object)
|
||||
@object = object
|
||||
end
|
||||
|
||||
def run
|
||||
self.class.processed << @object
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
class ThreadTrackingJob
|
||||
attr_reader :thread_id
|
||||
|
||||
def run
|
||||
@thread_id = Thread.current.object_id
|
||||
end
|
||||
|
||||
def ran?
|
||||
@thread_id
|
||||
end
|
||||
end
|
||||
|
||||
def test_drain
|
||||
t = nil
|
||||
ran = false
|
||||
|
||||
job = Job.new do
|
||||
ran = true
|
||||
t = Thread.current
|
||||
end
|
||||
|
||||
@queue.push job
|
||||
@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
|
||||
|
|
Loading…
Reference in a new issue