From 8e1d8fd0934b8e04fd6b1f8f8d4de17cd3b1abe5 Mon Sep 17 00:00:00 2001 From: Paul Battley Date: Tue, 3 Jul 2012 10:29:32 +0100 Subject: Track queue threading with named classes Using an anonymous class prevented marshalling: we're not doing that yet, but the next commit will introduce this. This also provided an opportunity to improve the expressivity of the tests and to make the assertion failure messages clearer. --- railties/test/application/queue_test.rb | 38 +++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/railties/test/application/queue_test.rb b/railties/test/application/queue_test.rb index da8bdeed52..d5198b07e8 100644 --- a/railties/test/application/queue_test.rb +++ b/railties/test/application/queue_test.rb @@ -30,34 +30,44 @@ module ApplicationTests assert_kind_of Rails::Queueing::Queue, Rails.queue end - test "in development mode, an enqueued job will be processed in a separate thread" do - app("development") + class ThreadTrackingJob + def initialize + @origin = Thread.current.object_id + end - job = Struct.new(:origin, :target).new(Thread.current) - def job.run - self.target = Thread.current + def run + @target = Thread.current.object_id end + def ran_in_different_thread? + @origin != @target + end + + def ran? + @target + end + end + + test "in development mode, an enqueued job will be processed in a separate thread" do + app("development") + + job = ThreadTrackingJob.new Rails.queue.push job sleep 0.1 - assert job.target, "The job was run" - assert_not_equal job.origin, job.target + assert job.ran?, "Expected job to be run" + assert job.ran_in_different_thread?, "Expected job to run in a different thread" end test "in test mode, explicitly draining the queue will process it in a separate thread" do app("test") - job = Struct.new(:origin, :target).new(Thread.current) - def job.run - self.target = Thread.current - end - + job = ThreadTrackingJob.new Rails.queue.push job Rails.queue.drain - assert job.target, "The job was run" - assert_not_equal job.origin, job.target + assert job.ran?, "Expected job to be run" + assert job.ran_in_different_thread?, "Expected job to run in a different thread" end test "in test mode, the queue can be observed" do -- cgit v1.2.3