2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-10-01 03:04:02 -04:00
|
|
|
require_dependency 'jobs/base'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe Jobs do
|
|
|
|
|
|
|
|
describe 'enqueue' do
|
|
|
|
|
|
|
|
describe 'when queue_jobs is true' do
|
|
|
|
before do
|
2013-03-22 11:35:32 -04:00
|
|
|
SiteSetting.expects(:queue_jobs?).at_least_once.returns(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues a job in sidekiq' do
|
|
|
|
Sidekiq::Client.expects(:enqueue).with(Jobs::ProcessPost, post_id: 1, current_site_id: 'default')
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not pass current_site_id when 'all_sites' is present" do
|
|
|
|
Sidekiq::Client.expects(:enqueue).with(Jobs::ProcessPost, post_id: 1)
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1, all_sites: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't execute the job" do
|
|
|
|
Sidekiq::Client.stubs(:enqueue)
|
|
|
|
Jobs::ProcessPost.any_instance.expects(:perform).never
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should enqueue with the correct database id when the current_site_id option is given" do
|
|
|
|
Sidekiq::Client.expects(:enqueue).with do |arg1, arg2|
|
2013-03-04 19:42:44 -05:00
|
|
|
arg2[:current_site_id] == 'test_db' && arg2[:sync_exec].nil?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1, current_site_id: 'test_db')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when queue_jobs is false' do
|
|
|
|
before do
|
2013-03-22 11:35:32 -04:00
|
|
|
SiteSetting.expects(:queue_jobs?).at_least_once.returns(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't enqueue in sidekiq" do
|
|
|
|
Sidekiq::Client.expects(:enqueue).with(Jobs::ProcessPost, {}).never
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "executes the job right away" do
|
|
|
|
Jobs::ProcessPost.any_instance.expects(:perform).with(post_id: 1, sync_exec: true, current_site_id: "default")
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and current_site_id option is given and does not match the current connection' do
|
|
|
|
before do
|
|
|
|
Sidekiq::Client.stubs(:enqueue)
|
|
|
|
Jobs::ProcessPost.any_instance.stubs(:execute).returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not execute the job' do
|
|
|
|
Jobs::ProcessPost.any_instance.expects(:execute).never
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1, current_site_id: 'test_db') rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise an exception' do
|
|
|
|
expect {
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1, current_site_id: 'test_db')
|
|
|
|
}.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not connect to the given database' do
|
|
|
|
RailsMultisite::ConnectionManagement.expects(:establish_connection).never
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1, current_site_id: 'test_db') rescue nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-07 14:25:41 -04:00
|
|
|
describe 'cancel_scheduled_job' do
|
2016-01-13 03:08:26 -05:00
|
|
|
|
2013-05-07 14:25:41 -04:00
|
|
|
it 'deletes the matching job' do
|
2016-01-13 03:08:26 -05:00
|
|
|
SiteSetting.queue_jobs = true
|
|
|
|
|
|
|
|
Sidekiq::Testing.disable! do
|
|
|
|
scheduled_jobs = Sidekiq::ScheduledSet.new
|
|
|
|
|
|
|
|
expect(scheduled_jobs.size).to eq(0)
|
|
|
|
|
2016-08-12 07:10:52 -04:00
|
|
|
Jobs.enqueue_in(1.year, :run_heartbeat, topic_id: 123)
|
|
|
|
Jobs.enqueue_in(2.years, :run_heartbeat, topic_id: 456)
|
|
|
|
Jobs.enqueue_in(3.years, :run_heartbeat, topic_id: 123, current_site_id: 'foo')
|
|
|
|
Jobs.enqueue_in(4.years, :run_heartbeat, topic_id: 123, current_site_id: 'bar')
|
2013-05-07 14:25:41 -04:00
|
|
|
|
2016-08-12 07:10:52 -04:00
|
|
|
expect(scheduled_jobs.size).to eq(4)
|
2016-01-13 03:08:26 -05:00
|
|
|
|
2016-08-12 07:10:52 -04:00
|
|
|
Jobs.cancel_scheduled_job(:run_heartbeat, topic_id: 123)
|
|
|
|
|
|
|
|
expect(scheduled_jobs.size).to eq(3)
|
|
|
|
|
|
|
|
Jobs.cancel_scheduled_job(:run_heartbeat, topic_id: 123, all_sites: true)
|
2016-01-13 03:08:26 -05:00
|
|
|
|
|
|
|
expect(scheduled_jobs.size).to eq(1)
|
|
|
|
end
|
2013-05-07 14:25:41 -04:00
|
|
|
end
|
2016-01-13 03:08:26 -05:00
|
|
|
|
2013-05-07 14:25:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'enqueue_at' do
|
|
|
|
it 'calls enqueue_in for you' do
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time
|
|
|
|
Jobs.expects(:enqueue_in).with(3 * 60 * 60, :eat_lunch, {}).returns(true)
|
|
|
|
Jobs.enqueue_at(3.hours.from_now, :eat_lunch, {})
|
2013-05-07 14:25:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles datetimes that are in the past' do
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time
|
|
|
|
Jobs.expects(:enqueue_in).with(0, :eat_lunch, {}).returns(true)
|
|
|
|
Jobs.enqueue_at(3.hours.ago, :eat_lunch, {})
|
2013-05-07 14:25:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|