2022-08-27 11:52:29 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
RSpec.describe 'diagnostic reports', :aggregate_failures, feature_category: :application_performance do
|
2022-08-27 11:52:29 +05:30
|
|
|
subject(:load_initializer) do
|
|
|
|
load Rails.root.join('config/initializers/diagnostic_reports.rb')
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
shared_examples 'does not modify worker hooks' do
|
2022-08-27 11:52:29 +05:30
|
|
|
it do
|
|
|
|
expect(Gitlab::Cluster::LifecycleEvents).not_to receive(:on_worker_start)
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(Gitlab::Cluster::LifecycleEvents).not_to receive(:on_worker_stop)
|
2022-08-27 11:52:29 +05:30
|
|
|
expect(Gitlab::Memory::ReportsDaemon).not_to receive(:instance)
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(Gitlab::Memory::Reporter).not_to receive(:new)
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
load_initializer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when GITLAB_DIAGNOSTIC_REPORTS_ENABLED is set to true' do
|
|
|
|
before do
|
|
|
|
stub_env('GITLAB_DIAGNOSTIC_REPORTS_ENABLED', true)
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
context 'when run in Puma context' do
|
2022-08-27 11:52:29 +05:30
|
|
|
before do
|
2022-11-25 23:54:43 +05:30
|
|
|
allow(::Gitlab::Runtime).to receive(:puma?).and_return(true)
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
let(:report_daemon) { instance_double(Gitlab::Memory::ReportsDaemon) }
|
2023-03-04 22:38:38 +05:30
|
|
|
let(:reporter) { instance_double(Gitlab::Memory::Reporter) }
|
2022-08-27 11:52:29 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it 'modifies worker startup hooks, starts Gitlab::Memory::ReportsDaemon' do
|
2022-08-27 11:52:29 +05:30
|
|
|
expect(Gitlab::Cluster::LifecycleEvents).to receive(:on_worker_start).and_call_original
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(Gitlab::Cluster::LifecycleEvents).to receive(:on_worker_stop) # stub this out to not mutate global state
|
2022-11-25 23:54:43 +05:30
|
|
|
expect_next_instance_of(Gitlab::Memory::ReportsDaemon) do |daemon|
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(daemon).to receive(:start)
|
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
load_initializer
|
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it 'writes scheduled heap dumps in on_worker_stop' do
|
|
|
|
expect(Gitlab::Cluster::LifecycleEvents).to receive(:on_worker_start)
|
|
|
|
expect(Gitlab::Cluster::LifecycleEvents).to receive(:on_worker_stop).and_call_original
|
|
|
|
expect(Gitlab::Memory::Reporter).to receive(:new).and_return(reporter)
|
|
|
|
expect(reporter).to receive(:run_report).with(an_instance_of(Gitlab::Memory::Reports::HeapDump))
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
load_initializer
|
2023-03-04 22:38:38 +05:30
|
|
|
# This is necessary because this hook normally fires during worker shutdown.
|
|
|
|
Gitlab::Cluster::LifecycleEvents.do_worker_stop
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
context 'when run in non-Puma context, such as rails console, tests, Sidekiq' do
|
2022-08-27 11:52:29 +05:30
|
|
|
before do
|
2022-11-25 23:54:43 +05:30
|
|
|
allow(::Gitlab::Runtime).to receive(:puma?).and_return(false)
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
include_examples 'does not modify worker hooks'
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when GITLAB_DIAGNOSTIC_REPORTS_ENABLED is not set' do
|
|
|
|
before do
|
2022-11-25 23:54:43 +05:30
|
|
|
allow(::Gitlab::Runtime).to receive(:puma?).and_return(true)
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
include_examples 'does not modify worker hooks'
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when GITLAB_DIAGNOSTIC_REPORTS_ENABLED is set to false' do
|
|
|
|
before do
|
|
|
|
stub_env('GITLAB_DIAGNOSTIC_REPORTS_ENABLED', false)
|
2022-11-25 23:54:43 +05:30
|
|
|
allow(::Gitlab::Runtime).to receive(:puma?).and_return(true)
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
include_examples 'does not modify worker hooks'
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
end
|