2012-04-27 00:43:12 -04:00
|
|
|
require 'abstract_unit'
|
2012-09-13 18:09:15 -04:00
|
|
|
require 'active_support/queueing'
|
2012-04-27 00:43:12 -04:00
|
|
|
|
|
|
|
class TestQueueTest < ActiveSupport::TestCase
|
2012-07-03 08:03:48 -04:00
|
|
|
def setup
|
2012-09-13 18:09:15 -04:00
|
|
|
@queue = ActiveSupport::TestQueue.new
|
2012-07-03 08:03:48 -04:00
|
|
|
end
|
2012-04-27 00:43:12 -04:00
|
|
|
|
2012-07-03 08:03:48 -04:00
|
|
|
class ExceptionRaisingJob
|
2012-04-27 00:43:12 -04:00
|
|
|
def run
|
2012-07-03 08:03:48 -04:00
|
|
|
raise
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-14 20:00:46 -04:00
|
|
|
def test_drain_raises_exceptions_from_running_jobs
|
2012-07-03 08:03:48 -04:00
|
|
|
@queue.push ExceptionRaisingJob.new
|
2012-04-27 18:07:35 -04:00
|
|
|
assert_raises(RuntimeError) { @queue.drain }
|
|
|
|
end
|
|
|
|
|
2012-04-27 17:58:04 -04:00
|
|
|
def test_jobs
|
|
|
|
@queue.push 1
|
|
|
|
@queue.push 2
|
|
|
|
assert_equal [1,2], @queue.jobs
|
|
|
|
end
|
|
|
|
|
2012-07-03 08:03:48 -04:00
|
|
|
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
|
|
|
|
|
2012-04-27 00:43:12 -04:00
|
|
|
def test_contents
|
2012-07-03 08:03:48 -04:00
|
|
|
job = EquivalentJob.new
|
2012-09-14 20:00:46 -04:00
|
|
|
assert @queue.empty?
|
2012-04-27 00:43:12 -04:00
|
|
|
@queue.push job
|
2012-04-27 17:26:26 -04:00
|
|
|
refute @queue.empty?
|
|
|
|
assert_equal job, @queue.pop
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
|
2012-07-03 08:03:48 -04:00
|
|
|
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
|
2012-04-27 16:21:51 -04:00
|
|
|
|
2012-07-03 08:03:48 -04:00
|
|
|
def test_order
|
|
|
|
ProcessingJob.clear_processed
|
|
|
|
job1 = ProcessingJob.new(1)
|
|
|
|
job2 = ProcessingJob.new(2)
|
2012-04-27 16:21:51 -04:00
|
|
|
|
2012-04-27 16:15:51 -04:00
|
|
|
@queue.push job1
|
|
|
|
@queue.push job2
|
|
|
|
@queue.drain
|
2012-04-27 16:21:51 -04:00
|
|
|
|
2012-07-03 08:03:48 -04:00
|
|
|
assert_equal [1,2], ProcessingJob.processed
|
2012-04-27 16:15:51 -04:00
|
|
|
end
|
|
|
|
|
2012-07-03 08:03:48 -04:00
|
|
|
class ThreadTrackingJob
|
|
|
|
attr_reader :thread_id
|
2012-04-27 00:43:12 -04:00
|
|
|
|
2012-07-03 08:03:48 -04:00
|
|
|
def run
|
|
|
|
@thread_id = Thread.current.object_id
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
|
2012-07-03 08:03:48 -04:00
|
|
|
def ran?
|
|
|
|
@thread_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_drain
|
|
|
|
@queue.push ThreadTrackingJob.new
|
|
|
|
job = @queue.jobs.last
|
2012-04-27 00:43:12 -04:00
|
|
|
@queue.drain
|
|
|
|
|
2012-04-27 17:26:26 -04:00
|
|
|
assert @queue.empty?
|
2012-07-03 08:03:48 -04:00
|
|
|
assert job.ran?, "The job runs synchronously when the queue is drained"
|
2012-09-18 18:22:13 -04:00
|
|
|
assert_equal job.thread_id, Thread.current.object_id
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
2012-10-12 13:42:26 -04:00
|
|
|
|
|
|
|
class IdentifiableJob
|
|
|
|
def initialize(id)
|
|
|
|
@id = id
|
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
other.same_id?(@id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def same_id?(other_id)
|
|
|
|
other_id == @id
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_queue_can_be_observed
|
|
|
|
jobs = (1..10).map do |id|
|
|
|
|
IdentifiableJob.new(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
jobs.each do |job|
|
|
|
|
@queue.push job
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal jobs, @queue.jobs
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_adding_an_unmarshallable_job
|
|
|
|
anonymous_class_instance = Struct.new(:run).new
|
|
|
|
|
|
|
|
assert_raises TypeError do
|
|
|
|
@queue.push anonymous_class_instance
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_attempting_to_add_a_reference_to_itself
|
|
|
|
job = {reference: @queue}
|
|
|
|
assert_raises TypeError do
|
|
|
|
@queue.push job
|
|
|
|
end
|
|
|
|
end
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|