Permalink
Browse files

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.
  • Loading branch information...
1 parent 55456ad commit 8e1d8fd0934b8e04fd6b1f8f8d4de17cd3b1abe5 @threedaymonk threedaymonk committed Jul 3, 2012
Showing with 24 additions and 14 deletions.
  1. +24 −14 railties/test/application/queue_test.rb
@@ -30,34 +30,44 @@ def app_const
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

0 comments on commit 8e1d8fd

Please sign in to comment.