FIX: Ensure that topic timers belonging to trashed topics are destroyed.

This commit is contained in:
Guo Xiang Tan 2020-08-26 16:10:13 +08:00
parent b60c39fdf0
commit 5ff6c10320
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
2 changed files with 19 additions and 14 deletions

View File

@ -126,8 +126,7 @@ class TopicTimer < ActiveRecord::Base
end
def schedule_auto_open_job(time)
return unless topic
topic.update_status('closed', true, user) if !topic.closed
topic.update_status('closed', true, user) if !topic&.closed
Jobs.enqueue_at(time, :toggle_topic_closed,
topic_timer_id: id,
@ -136,8 +135,7 @@ class TopicTimer < ActiveRecord::Base
end
def schedule_auto_close_job(time)
return unless topic
topic.update_status('closed', false, user) if topic.closed
topic.update_status('closed', false, user) if topic&.closed
Jobs.enqueue_at(time, :toggle_topic_closed,
topic_timer_id: id,

View File

@ -232,27 +232,34 @@ RSpec.describe TopicTimer, type: :model do
Fabricate(:topic_timer, execute_at: Time.zone.now + 1.hour)
Fabricate(:topic_timer,
trashed_topic_timer = Fabricate(:topic_timer,
execute_at: Time.zone.now - 1.hour,
created_at: Time.zone.now - 2.hour
).topic.trash!
)
trashed_topic_timer.topic.trash!
# creating topic timers already enqueues jobs
# let's delete them to test ensure_consistency!
Sidekiq::Worker.clear_all
expect { described_class.ensure_consistency! }
.to change { Jobs::ToggleTopicClosed.jobs.count }.by(2)
.to change { Jobs::ToggleTopicClosed.jobs.count }.by(3)
job_args = Jobs::ToggleTopicClosed.jobs.first["args"].first
expect(job_enqueued?(job: :toggle_topic_closed, args: {
topic_timer_id: close_topic_timer.id,
state: true
})).to eq(true)
expect(job_args["topic_timer_id"]).to eq(close_topic_timer.id)
expect(job_args["state"]).to eq(true)
expect(job_enqueued?(job: :toggle_topic_closed, args: {
topic_timer_id: open_topic_timer.id,
state: false
})).to eq(true)
job_args = Jobs::ToggleTopicClosed.jobs.last["args"].first
expect(job_args["topic_timer_id"]).to eq(open_topic_timer.id)
expect(job_args["state"]).to eq(false)
expect(job_enqueued?(job: :toggle_topic_closed, args: {
topic_timer_id: trashed_topic_timer.id,
state: true
})).to eq(true)
end
it "should enqueue remind me jobs that have been missed" do