discourse/spec/integration/topic_auto_close_spec.rb
Martin Brennan 0034cbda8a
DEV: Change Topic Timer from enqueue_at scheduled jobs to incrementally executed jobs (#11698)
Moves the topic timer jobs from being scheduled ahead of time with enqueue_at to a 5 minute scheduled run like bookmark reminders, in a new job called Jobs::EnqueueTopicTimers. Backwards compatibility is maintained by checking if an existing topic timer job is enqueued in sidekiq for the timer, and if it is not running it inside the new job.

The functionality to close/open a topic if it is in the opposite state still remains in the after_save block of TopicTimer, with further commentary, which is used for Open/Close Temporarily.

This also removes the ensure_consistency! functionality of topic timers as it is no longer needed; the new job will always pick up the timers because they are not stored in a fragile state of sidekiq.
2021-01-19 13:30:58 +10:00

91 lines
2.8 KiB
Ruby

# encoding: UTF-8
# frozen_string_literal: true
require 'rails_helper'
describe Topic do
let(:job_klass) { Jobs::CloseTopic }
context 'creating a topic without auto-close' do
let(:topic) { Fabricate(:topic, category: category) }
context 'uncategorized' do
let(:category) { nil }
it 'should not schedule the topic to auto-close' do
expect(topic.public_topic_timer).to eq(nil)
expect(job_klass.jobs).to eq([])
end
end
context 'category without default auto-close' do
let(:category) { Fabricate(:category, auto_close_hours: nil) }
it 'should not schedule the topic to auto-close' do
expect(topic.public_topic_timer).to eq(nil)
expect(job_klass.jobs).to eq([])
end
end
context 'jobs may be queued' do
before do
freeze_time
end
context 'category has a default auto-close' do
let(:category) { Fabricate(:category, auto_close_hours: 2.0) }
it 'should schedule the topic to auto-close' do
topic
topic_status_update = TopicTimer.last
expect(topic_status_update.topic).to eq(topic)
expect(topic.public_topic_timer.execute_at).to eq_time(2.hours.from_now)
end
context 'topic was created by staff user' do
let(:admin) { Fabricate(:admin) }
let(:staff_topic) { Fabricate(:topic, user: admin, category: category) }
it 'should schedule the topic to auto-close' do
staff_topic
topic_status_update = TopicTimer.last
expect(topic_status_update.topic).to eq(staff_topic)
expect(topic_status_update.execute_at).to eq_time(2.hours.from_now)
expect(topic_status_update.user).to eq(Discourse.system_user)
end
context 'topic is closed manually' do
it 'should remove the schedule to auto-close the topic' do
topic_timer_id = staff_topic.public_topic_timer.id
staff_topic.update_status('closed', true, admin)
expect(TopicTimer.with_deleted.find(topic_timer_id).deleted_at)
.to eq_time(Time.zone.now)
end
end
end
context 'topic was created by a non-staff user' do
let(:regular_user) { Fabricate(:user) }
let(:regular_user_topic) { Fabricate(:topic, user: regular_user, category: category) }
it 'should schedule the topic to auto-close' do
regular_user_topic
topic_status_update = TopicTimer.last
expect(topic_status_update.topic).to eq(regular_user_topic)
expect(topic_status_update.execute_at).to eq_time(2.hours.from_now)
expect(topic_status_update.user).to eq(Discourse.system_user)
end
end
end
end
end
end