aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/queueing.rb7
-rw-r--r--railties/test/application/queue_test.rb36
2 files changed, 35 insertions, 8 deletions
diff --git a/railties/lib/rails/queueing.rb b/railties/lib/rails/queueing.rb
index b4bc7fcd18..8a76914548 100644
--- a/railties/lib/rails/queueing.rb
+++ b/railties/lib/rails/queueing.rb
@@ -22,6 +22,13 @@ module Rails
@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
diff --git a/railties/test/application/queue_test.rb b/railties/test/application/queue_test.rb
index d5198b07e8..e8ee8c5c9f 100644
--- a/railties/test/application/queue_test.rb
+++ b/railties/test/application/queue_test.rb
@@ -62,24 +62,36 @@ module ApplicationTests
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 @@ module ApplicationTests
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"