debian-mirror-gitlab/spec/lib/gitlab/runtime_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

184 lines
4.7 KiB
Ruby
Raw Permalink Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-07-09 08:55:56 +05:30
RSpec.describe Gitlab::Runtime, feature_category: :application_performance do
2020-03-13 15:44:24 +05:30
shared_examples "valid runtime" do |runtime, max_threads|
it "identifies itself" do
expect(subject.identify).to eq(runtime)
expect(subject.public_send("#{runtime}?")).to be(true)
end
it "does not identify as others" do
(described_class::AVAILABLE_RUNTIMES - [runtime]).each do |runtime|
expect(subject.public_send("#{runtime}?")).to eq(false)
end
end
it "reports its maximum concurrency" do
expect(subject.max_threads).to eq(max_threads)
end
end
before do
allow(described_class).to receive(:process_name).and_return('ruby')
stub_rails_env('production')
end
context "when unknown" do
2022-04-04 11:22:00 +05:30
describe '.identify' do
it "raises an exception when trying to identify" do
expect { subject.identify }.to raise_error(subject::UnknownProcessError)
end
end
describe '.safe_identify' do
it "returns nil" do
expect(subject.safe_identify).to be_nil
end
2020-03-13 15:44:24 +05:30
end
end
2023-07-09 08:55:56 +05:30
context 'with Puma' do
before do
stub_const('::Puma::Server', double)
end
describe '.puma?' do
it 'returns true' do
expect(subject.puma?).to be true
end
end
end
2020-03-13 15:44:24 +05:30
context "on multiple matches" do
before do
2023-07-09 08:55:56 +05:30
stub_const('::Puma::Server', double)
2020-03-13 15:44:24 +05:30
stub_const('::Rails::Console', double)
end
2022-04-04 11:22:00 +05:30
describe '.identify' do
it "raises an exception when trying to identify" do
expect { subject.identify }.to raise_error(subject::AmbiguousProcessError)
end
end
describe '.safe_identify' do
it "returns nil" do
expect(subject.safe_identify).to be_nil
end
2020-03-13 15:44:24 +05:30
end
end
2021-06-08 01:23:25 +05:30
# Puma has no cli_config method unless `puma/cli` is required
context "puma without cli_config" do
let(:puma_type) { double('::Puma') }
before do
stub_const('::Puma', puma_type)
2023-07-09 08:55:56 +05:30
allow(described_class).to receive(:puma?).and_return(true)
2021-06-08 01:23:25 +05:30
end
2021-12-11 22:18:48 +05:30
it_behaves_like "valid runtime", :puma, 1 + Gitlab::ActionCable::Config.worker_pool_size
2021-06-08 01:23:25 +05:30
end
context "puma with cli_config" do
2020-03-13 15:44:24 +05:30
let(:puma_type) { double('::Puma') }
2021-03-11 19:13:27 +05:30
let(:max_workers) { 2 }
2020-03-13 15:44:24 +05:30
before do
stub_const('::Puma', puma_type)
2023-07-09 08:55:56 +05:30
allow(described_class).to receive(:puma?).and_return(true)
2021-03-11 19:13:27 +05:30
allow(puma_type).to receive_message_chain(:cli_config, :options).and_return(max_threads: 2, workers: max_workers)
2020-03-13 15:44:24 +05:30
end
2021-12-11 22:18:48 +05:30
it_behaves_like "valid runtime", :puma, 3 + Gitlab::ActionCable::Config.worker_pool_size
2020-07-28 23:09:34 +05:30
2022-05-07 20:08:51 +05:30
it 'identifies as an application runtime' do
expect(Gitlab::Runtime.application?).to be true
end
2021-12-11 22:18:48 +05:30
context "when ActionCable worker pool size is configured" do
2020-07-28 23:09:34 +05:30
before do
2021-12-11 22:18:48 +05:30
stub_env('ACTION_CABLE_WORKER_POOL_SIZE', 10)
2020-07-28 23:09:34 +05:30
end
2021-12-11 22:18:48 +05:30
it_behaves_like "valid runtime", :puma, 13
2020-07-28 23:09:34 +05:30
end
2021-03-11 19:13:27 +05:30
describe ".puma_in_clustered_mode?" do
context 'when Puma is set up with workers > 0' do
let(:max_workers) { 4 }
specify { expect(described_class.puma_in_clustered_mode?).to be true }
end
context 'when Puma is set up with workers = 0' do
let(:max_workers) { 0 }
specify { expect(described_class.puma_in_clustered_mode?).to be false }
end
end
2020-03-13 15:44:24 +05:30
end
context "sidekiq" do
let(:sidekiq_type) { double('::Sidekiq') }
before do
stub_const('::Sidekiq', sidekiq_type)
allow(sidekiq_type).to receive(:server?).and_return(true)
2023-01-13 00:05:48 +05:30
allow(sidekiq_type).to receive(:[]).with(:concurrency).and_return(2)
2020-03-13 15:44:24 +05:30
end
2021-12-11 22:18:48 +05:30
it_behaves_like "valid runtime", :sidekiq, 5
2022-05-07 20:08:51 +05:30
it 'identifies as an application runtime' do
expect(Gitlab::Runtime.application?).to be true
end
2020-03-13 15:44:24 +05:30
end
context "console" do
before do
stub_const('::Rails::Console', double('::Rails::Console'))
end
it_behaves_like "valid runtime", :console, 1
2022-05-07 20:08:51 +05:30
it 'does not identify as an application runtime' do
expect(Gitlab::Runtime.application?).to be false
end
2020-03-13 15:44:24 +05:30
end
context "test suite" do
before do
stub_rails_env('test')
end
it_behaves_like "valid runtime", :test_suite, 1
2022-05-07 20:08:51 +05:30
it 'does not identify as an application runtime' do
expect(Gitlab::Runtime.application?).to be false
end
2020-03-13 15:44:24 +05:30
end
context "geo log cursor" do
before do
stub_const('::GeoLogCursorOptionParser', double('::GeoLogCursorOptionParser'))
end
it_behaves_like "valid runtime", :geo_log_cursor, 1
end
context "rails runner" do
before do
stub_const('::Rails::Command::RunnerCommand', double('::Rails::Command::RunnerCommand'))
end
it_behaves_like "valid runtime", :rails_runner, 1
2022-05-07 20:08:51 +05:30
it 'does not identify as an application runtime' do
expect(Gitlab::Runtime.application?).to be false
end
2020-03-13 15:44:24 +05:30
end
end