2012-04-27 00:43:12 -04:00
|
|
|
require 'isolation/abstract_unit'
|
|
|
|
require 'rack/test'
|
|
|
|
|
|
|
|
module ApplicationTests
|
|
|
|
class GeneratorsTest < ActiveSupport::TestCase
|
|
|
|
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 TestQueue in test mode" do
|
|
|
|
app("test")
|
|
|
|
assert_kind_of Rails::Queueing::TestQueue, Rails.application.queue
|
|
|
|
assert_kind_of Rails::Queueing::TestQueue, Rails.queue
|
|
|
|
end
|
|
|
|
|
|
|
|
test "the queue is a Queue in development mode" do
|
|
|
|
app("development")
|
2012-04-28 03:26:23 -04:00
|
|
|
assert_kind_of Rails::Queueing::Queue, Rails.application.queue
|
|
|
|
assert_kind_of Rails::Queueing::Queue, Rails.queue
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
|
2012-07-03 05:29:32 -04:00
|
|
|
class ThreadTrackingJob
|
|
|
|
def initialize
|
|
|
|
@origin = Thread.current.object_id
|
|
|
|
end
|
2012-04-27 00:43:12 -04:00
|
|
|
|
2012-07-03 05:29:32 -04:00
|
|
|
def run
|
|
|
|
@target = Thread.current.object_id
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
|
2012-07-03 05:29:32 -04:00
|
|
|
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
|
2012-04-27 00:43:12 -04:00
|
|
|
Rails.queue.push job
|
|
|
|
sleep 0.1
|
|
|
|
|
2012-07-03 05:29:32 -04:00
|
|
|
assert job.ran?, "Expected job to be run"
|
|
|
|
assert job.ran_in_different_thread?, "Expected job to run in a different thread"
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "in test mode, explicitly draining the queue will process it in a separate thread" do
|
|
|
|
app("test")
|
|
|
|
|
2012-07-03 06:55:46 -04:00
|
|
|
Rails.queue.push ThreadTrackingJob.new
|
|
|
|
job = Rails.queue.jobs.last
|
2012-04-27 00:43:12 -04:00
|
|
|
Rails.queue.drain
|
|
|
|
|
2012-07-03 05:29:32 -04:00
|
|
|
assert job.ran?, "Expected job to be run"
|
|
|
|
assert job.ran_in_different_thread?, "Expected job to run in a different thread"
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
|
2012-07-03 06:55:46 -04:00
|
|
|
class IdentifiableJob
|
|
|
|
def initialize(id)
|
|
|
|
@id = id
|
|
|
|
end
|
2012-04-27 00:43:12 -04:00
|
|
|
|
2012-07-03 06:55:46 -04:00
|
|
|
def ==(other)
|
|
|
|
other.same_id?(@id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def same_id?(other_id)
|
|
|
|
other_id == @id
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
|
2012-07-03 06:55:46 -04:00
|
|
|
def run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "in test mode, the queue can be observed" do
|
|
|
|
app("test")
|
|
|
|
|
2012-04-27 00:43:12 -04:00
|
|
|
jobs = (1..10).map do |id|
|
2012-07-03 06:55:46 -04:00
|
|
|
IdentifiableJob.new(id)
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
jobs.each do |job|
|
|
|
|
Rails.queue.push job
|
|
|
|
end
|
|
|
|
|
2012-04-27 18:51:40 -04:00
|
|
|
assert_equal jobs, Rails.queue.jobs
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
|
2012-07-03 06:55:46 -04:00
|
|
|
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
|
|
|
|
|
2012-07-03 06:59:38 -04:00
|
|
|
test "attempting to marshal a queue will raise an exception" do
|
|
|
|
app("test")
|
|
|
|
assert_raises TypeError do
|
|
|
|
Marshal.dump Rails.queue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "attempting to add a reference to itself to the queue will raise an exception" do
|
|
|
|
app("test")
|
|
|
|
job = {reference: Rails.queue}
|
|
|
|
assert_raises TypeError do
|
|
|
|
Rails.queue.push job
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-02 23:10:27 -04:00
|
|
|
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")
|
2012-05-02 23:10:27 -04:00
|
|
|
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
|
|
|
|
|
2012-05-02 23:10:27 -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
|
2012-05-02 23:10:27 -04:00
|
|
|
|
|
|
|
test "a custom consumer implementation can be provided" do
|
|
|
|
add_to_env_config "production", <<-RUBY
|
|
|
|
require "my_queue_consumer"
|
|
|
|
config.queue_consumer = MyQueueConsumer
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file "lib/my_queue_consumer.rb", <<-RUBY
|
|
|
|
class MyQueueConsumer < Rails::Queueing::ThreadedConsumer
|
|
|
|
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
|