2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Jobs do
|
2013-02-05 14:16:51 -05:00
|
|
|
describe "enqueue" do
|
2019-03-14 10:47:38 -04:00
|
|
|
describe "run_later!" do
|
|
|
|
before { Jobs.run_later! }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "enqueues a job in sidekiq" do
|
2019-01-08 17:51:11 -05:00
|
|
|
Sidekiq::Testing.fake! do
|
|
|
|
jobs = Jobs::ProcessPost.jobs
|
|
|
|
|
|
|
|
jobs.clear
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1)
|
|
|
|
expect(jobs.length).to eq(1)
|
|
|
|
job = jobs.first
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
"class" => "Jobs::ProcessPost",
|
|
|
|
"args" => [{ "post_id" => 1, "current_site_id" => "default" }],
|
|
|
|
"queue" => "default",
|
|
|
|
}
|
|
|
|
expect(job.slice("class", "args", "queue")).to eq(expected)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2020-12-07 19:05:01 -05:00
|
|
|
it "enqueues the job after the current transaction has committed" do
|
|
|
|
jobs = Jobs::ProcessPost.jobs
|
|
|
|
expect(jobs.length).to eq(0)
|
|
|
|
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1)
|
|
|
|
expect(jobs.length).to eq(1)
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1)
|
|
|
|
expect(jobs.length).to eq(1)
|
|
|
|
end
|
|
|
|
expect(jobs.length).to eq(2)
|
|
|
|
|
2021-05-20 21:43:47 -04:00
|
|
|
# Failed transaction
|
2020-12-07 19:05:01 -05:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1)
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(jobs.length).to eq(2) # No change
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it "does not pass current_site_id when 'all_sites' is present" do
|
2019-01-08 17:51:11 -05:00
|
|
|
Sidekiq::Testing.fake! do
|
|
|
|
jobs = Jobs::ProcessPost.jobs
|
|
|
|
|
|
|
|
jobs.clear
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1, all_sites: true)
|
|
|
|
|
|
|
|
expect(jobs.length).to eq(1)
|
|
|
|
job = jobs.first
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
"class" => "Jobs::ProcessPost",
|
|
|
|
"args" => [{ "post_id" => 1 }],
|
|
|
|
"queue" => "default",
|
|
|
|
}
|
|
|
|
expect(job.slice("class", "args", "queue")).to eq(expected)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
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
|
2019-01-08 17:51:11 -05:00
|
|
|
Sidekiq::Testing.fake! do
|
|
|
|
jobs = Jobs::ProcessPost.jobs
|
|
|
|
|
|
|
|
jobs.clear
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1, current_site_id: "test_db")
|
|
|
|
|
|
|
|
expect(jobs.length).to eq(1)
|
|
|
|
job = jobs.first
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
"class" => "Jobs::ProcessPost",
|
|
|
|
"args" => [{ "post_id" => 1, "current_site_id" => "test_db" }],
|
|
|
|
"queue" => "default",
|
|
|
|
}
|
|
|
|
expect(job.slice("class", "args", "queue")).to eq(expected)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-14 10:47:38 -04:00
|
|
|
describe "run_immediately!" do
|
|
|
|
before { Jobs.run_immediately! }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
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
|
2022-11-02 05:47:59 -04:00
|
|
|
Jobs::ProcessPost
|
|
|
|
.any_instance
|
2023-01-10 00:41:25 -05:00
|
|
|
.expects(:perform_immediately)
|
|
|
|
.with({ "post_id" => 1, "current_site_id" => "default" })
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
Jobs.enqueue(:process_post, post_id: 1)
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when current_site_id option is given and does not match the current connection" do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
|
|
|
Sidekiq::Client.stubs(:enqueue)
|
|
|
|
Jobs::ProcessPost.any_instance.stubs(:execute).returns(true)
|
|
|
|
end
|
|
|
|
|
2018-03-28 04:20:08 -04:00
|
|
|
it "should raise an exception" do
|
2013-02-05 14:16:51 -05:00
|
|
|
Jobs::ProcessPost.any_instance.expects(:execute).never
|
2018-03-28 04:20:08 -04:00
|
|
|
RailsMultisite::ConnectionManagement.expects(:establish_connection).never
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
expect {
|
|
|
|
Jobs.enqueue(:process_post, post_id: 1, current_site_id: "test_db")
|
|
|
|
}.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-07 14:25:41 -04:00
|
|
|
describe "cancel_scheduled_job" do
|
2024-12-17 23:48:50 -05:00
|
|
|
class Jobs::SomeJob < ::Jobs::Base
|
|
|
|
def execute(args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-28 04:20:08 -04:00
|
|
|
let(:scheduled_jobs) { Sidekiq::ScheduledSet.new }
|
|
|
|
|
|
|
|
after { scheduled_jobs.clear }
|
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
|
|
|
Sidekiq::Testing.disable! do
|
2018-06-01 11:34:19 -04:00
|
|
|
scheduled_jobs.clear
|
2016-01-13 03:08:26 -05:00
|
|
|
expect(scheduled_jobs.size).to eq(0)
|
|
|
|
|
2024-12-17 23:48:50 -05:00
|
|
|
Jobs.enqueue_in(1.year, :some_job, topic_id: 123)
|
|
|
|
Jobs.enqueue_in(2.years, :some_job, topic_id: 456)
|
|
|
|
Jobs.enqueue_in(3.years, :some_job, topic_id: 123, current_site_id: "foo")
|
|
|
|
Jobs.enqueue_in(4.years, :some_job, 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
|
|
|
|
2024-12-17 23:48:50 -05:00
|
|
|
Jobs.cancel_scheduled_job(:some_job, topic_id: 123)
|
2016-08-12 07:10:52 -04:00
|
|
|
|
|
|
|
expect(scheduled_jobs.size).to eq(3)
|
|
|
|
|
2024-12-17 23:48:50 -05:00
|
|
|
Jobs.cancel_scheduled_job(:some_job, 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
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "enqueue_at" do
|
|
|
|
it "calls enqueue_in for you" do
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time
|
2020-07-24 05:16:52 -04:00
|
|
|
|
|
|
|
expect_enqueued_with(job: :process_post, at: 3.hours.from_now) do
|
|
|
|
Jobs.enqueue_at(3.hours.from_now, :process_post, {})
|
|
|
|
end
|
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
|
2020-07-24 05:16:52 -04:00
|
|
|
|
|
|
|
expect_enqueued_with(job: :process_post, at: Time.zone.now) do
|
|
|
|
Jobs.enqueue_at(3.hours.ago, :process_post, {})
|
|
|
|
end
|
2013-05-07 14:25:41 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|