Permalink
Browse files

Ensure test jobs are marshallable

By marshalling and unmarshalling jobs when adding them to the test queue, we
can ensure that jobs created during test runs are valid candidates for
marshalling, and, thus, that they can be used with queueing backends other than
the default simple in-process implementation.

This will also be used in a subsequent commit to ensure that jobs pushed to the
queue do not contain a reference to the queue itself.
  • Loading branch information...
1 parent 8e1d8fd commit 33113ba0e73004d6508fc473a6d02f91cbb35709 @threedaymonk threedaymonk committed Jul 3, 2012
Showing with 35 additions and 8 deletions.
  1. +7 −0 railties/lib/rails/queueing.rb
  2. +28 −8 railties/test/application/queue_test.rb
@@ -22,6 +22,13 @@ def jobs
@que.dup
end
+ # Marshal and unmarshal job before pushing it onto the queue. This will
+ # raise an exception on any attempts in tests to push jobs that can't (or
+ # shouldn't) be marshalled.
+ def push(job)
+ super Marshal.load(Marshal.dump(job))
+ end
+
# Drain the queue, running all jobs in a different thread. This method
# may not be available on production queues.
def drain
@@ -62,24 +62,36 @@ def ran?
test "in test mode, explicitly draining the queue will process it in a separate thread" do
app("test")
- job = ThreadTrackingJob.new
- Rails.queue.push job
+ Rails.queue.push ThreadTrackingJob.new
+ job = Rails.queue.jobs.last
Rails.queue.drain
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
- app("test")
+ class IdentifiableJob
+ def initialize(id)
+ @id = id
+ end
- job = Struct.new(:id) do
- def run
- end
+ def ==(other)
+ other.same_id?(@id)
+ end
+
+ def same_id?(other_id)
+ other_id == @id
+ end
+
+ def run
end
+ end
+
+ test "in test mode, the queue can be observed" do
+ app("test")
jobs = (1..10).map do |id|
- job.new(id)
+ IdentifiableJob.new(id)
end
jobs.each do |job|
@@ -89,6 +101,14 @@ def run
assert_equal jobs, Rails.queue.jobs
end
+ test "in test mode, adding an unmarshallable job will raise an exception" do
+ app("test")
+ anonymous_class_instance = Struct.new(:run).new
+ assert_raises TypeError do
+ Rails.queue.push anonymous_class_instance
+ end
+ end
+
def setup_custom_queue
add_to_env_config "production", <<-RUBY
require "my_queue"

0 comments on commit 33113ba

Please sign in to comment.