1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/test/application/queue_test.rb

144 lines
3.4 KiB
Ruby
Raw Normal View History

2012-04-27 00:43:12 -04:00
require 'isolation/abstract_unit'
module ApplicationTests
2012-08-28 12:08:22 -04:00
class QueueTest < ActiveSupport::TestCase
2012-04-27 00:43:12 -04:00
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
end
def teardown
teardown_app
end
def app_const
@app_const ||= Class.new(Rails::Application)
end
test "the queue is a SynchronousQueue in test mode" do
2012-04-27 00:43:12 -04:00
app("test")
assert_kind_of ActiveSupport::SynchronousQueue, Rails.application.queue[:default]
assert_kind_of ActiveSupport::SynchronousQueue, Rails.queue[:default]
2012-04-27 00:43:12 -04:00
end
test "the queue is a SynchronousQueue in development mode" do
2012-04-27 00:43:12 -04:00
app("development")
2012-09-13 18:09:15 -04:00
assert_kind_of ActiveSupport::SynchronousQueue, Rails.application.queue[:default]
assert_kind_of ActiveSupport::SynchronousQueue, Rails.queue[:default]
2012-04-27 00:43:12 -04:00
end
class ThreadTrackingJob
def initialize
@origin = Thread.current.object_id
end
2012-04-27 00:43:12 -04:00
def run
@target = Thread.current.object_id
2012-04-27 00:43:12 -04:00
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 the same thread" do
app("development")
job = ThreadTrackingJob.new
2012-04-27 00:43:12 -04:00
Rails.queue.push job
sleep 0.1
assert job.ran?, "Expected job to be run"
refute job.ran_in_different_thread?, "Expected job to run in the same thread"
2012-04-27 00:43:12 -04:00
end
test "in test mode, explicitly draining the queue will process it in the same thread" do
2012-04-27 00:43:12 -04:00
app("test")
Rails.queue.push ThreadTrackingJob.new
job = Rails.queue.jobs.last
2012-04-27 00:43:12 -04:00
Rails.queue.drain
assert job.ran?, "Expected job to be run"
refute job.ran_in_different_thread?, "Expected job to run in the same thread"
2012-04-27 00:43:12 -04:00
end
test "attempting to marshal a queue will raise an exception" do
app("test")
assert_raises TypeError do
Marshal.dump Rails.queue
end
end
def setup_custom_queue
2012-04-27 00:43:12 -04:00
add_to_env_config "production", <<-RUBY
require "my_queue"
config.queue = MyQueue
RUBY
app_file "lib/my_queue.rb", <<-RUBY
class MyQueue
def push(job)
job.run
end
end
RUBY
app("production")
end
test "a custom queue implementation can be provided" do
setup_custom_queue
2012-04-27 00:43:12 -04:00
assert_kind_of MyQueue, Rails.queue[:default]
2012-04-27 00:43:12 -04:00
job = Struct.new(:id, :ran) do
2012-04-27 00:43:12 -04:00
def run
self.ran = true
end
end
job1 = job.new(1)
Rails.queue.push job1
assert_equal true, job1.ran
end
test "a custom consumer implementation can be provided" do
add_to_env_config "production", <<-RUBY
require "my_queue_consumer"
2012-09-13 18:09:15 -04:00
config.queue = ActiveSupport::Queue
config.queue_consumer = MyQueueConsumer
RUBY
app_file "lib/my_queue_consumer.rb", <<-RUBY
2012-09-13 18:09:15 -04:00
class MyQueueConsumer < ActiveSupport::ThreadedQueueConsumer
attr_reader :started
def start
@started = true
self
end
end
RUBY
app("production")
assert_kind_of MyQueueConsumer, Rails.application.queue_consumer
assert Rails.application.queue_consumer.started
end
test "default consumer is not used with custom queue implementation" do
setup_custom_queue
assert_nil Rails.application.queue_consumer
end
2012-04-27 00:43:12 -04:00
end
end