Commit 90baa159 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '194311-improve-performance-of-notification-service-spec' into 'master'

Improve performance of 'spec/services/notification_service_spec.rb'

See merge request gitlab-org/gitlab!22620
parents 55e200c1 2d4b127f
This diff is collapsed.
......@@ -6,7 +6,12 @@ module EmailHelpers
end
def reset_delivered_emails!
# We shouldn't actually send the emails, but we keep the following line for
# back-compatibility until we only check the mailer jobs enqueued in Sidekiq
ActionMailer::Base.deliveries.clear
# We should only check that the mailer jobs are enqueued in Sidekiq, hence
# clearing the background jobs queue
ActiveJob::Base.queue_adapter.enqueued_jobs.clear
end
def should_only_email(*users, kind: :to)
......
......@@ -36,4 +36,28 @@ module NotificationHelpers
setting = user.notification_settings_for(resource)
setting.update!(event => value)
end
def expect_delivery_jobs_count(count)
expect(ActionMailer::DeliveryJob).to have_been_enqueued.exactly(count).times
end
def expect_no_delivery_jobs
expect(ActionMailer::DeliveryJob).not_to have_been_enqueued
end
def expect_any_delivery_jobs
expect(ActionMailer::DeliveryJob).to have_been_enqueued.at_least(:once)
end
def have_enqueued_email(*args, mailer: "Notify", mail: "", delivery: "deliver_now")
have_enqueued_job(ActionMailer::DeliveryJob).with(mailer, mail, delivery, *args)
end
def expect_enqueud_email(*args, mailer: "Notify", mail: "", delivery: "deliver_now")
expect(ActionMailer::DeliveryJob).to have_been_enqueued.with(mailer, mail, delivery, *args)
end
def expect_not_enqueud_email(*args, mailer: "Notify", mail: "", delivery: "deliver_now")
expect(ActionMailer::DeliveryJob).not_to have_been_enqueued.with(mailer, mail, *args, any_args)
end
end
......@@ -3,7 +3,7 @@
# Note that we actually update the attribute on the target_project/group, rather than
# using `allow`. This is because there are some specs where, based on how the notification
# is done, using an `allow` doesn't change the correct object.
RSpec.shared_examples 'project emails are disabled' do
RSpec.shared_examples 'project emails are disabled' do |check_delivery_jobs_queue: false|
let(:target_project) { notification_target.is_a?(Project) ? notification_target : notification_target.project }
before do
......@@ -16,7 +16,13 @@ RSpec.shared_examples 'project emails are disabled' do
notification_trigger
should_not_email_anyone
if check_delivery_jobs_queue
# Only check enqueud jobs, not delivered emails
expect_no_delivery_jobs
else
# Deprecated: Check actual delivered emails
should_not_email_anyone
end
end
it 'sends emails to someone' do
......@@ -24,7 +30,13 @@ RSpec.shared_examples 'project emails are disabled' do
notification_trigger
should_email_anyone
if check_delivery_jobs_queue
# Only check enqueud jobs, not delivered emails
expect_any_delivery_jobs
else
# Deprecated: Check actual delivered emails
should_email_anyone
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment