summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/performance_bar
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/gitlab/performance_bar')
-rw-r--r--spec/lib/gitlab/performance_bar/redis_adapter_when_peek_enabled_spec.rb64
-rw-r--r--spec/lib/gitlab/performance_bar/stats_spec.rb42
2 files changed, 106 insertions, 0 deletions
diff --git a/spec/lib/gitlab/performance_bar/redis_adapter_when_peek_enabled_spec.rb b/spec/lib/gitlab/performance_bar/redis_adapter_when_peek_enabled_spec.rb
new file mode 100644
index 00000000000..bbc8b0d67e0
--- /dev/null
+++ b/spec/lib/gitlab/performance_bar/redis_adapter_when_peek_enabled_spec.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::PerformanceBar::RedisAdapterWhenPeekEnabled do
+ include ExclusiveLeaseHelpers
+
+ let(:peek_adapter) do
+ Class.new do
+ prepend Gitlab::PerformanceBar::RedisAdapterWhenPeekEnabled
+
+ def initialize(client)
+ @client = client
+ end
+
+ def save(id)
+ # no-op
+ end
+ end
+ end
+
+ describe '#save' do
+ let(:client) { double }
+ let(:uuid) { 'foo' }
+
+ before do
+ allow(Gitlab::PerformanceBar).to receive(:enabled_for_request?).and_return(true)
+ end
+
+ it 'stores request id and enqueues stats job' do
+ expect_to_obtain_exclusive_lease(GitlabPerformanceBarStatsWorker::LEASE_KEY, uuid)
+ expect(GitlabPerformanceBarStatsWorker).to receive(:perform_in).with(GitlabPerformanceBarStatsWorker::WORKER_DELAY, uuid)
+ expect(client).to receive(:sadd).with(GitlabPerformanceBarStatsWorker::STATS_KEY, uuid)
+
+ peek_adapter.new(client).save('foo')
+ end
+
+ context 'when performance_bar_stats is disabled' do
+ before do
+ stub_feature_flags(performance_bar_stats: false)
+ end
+
+ it 'ignores stats processing for the request' do
+ expect(GitlabPerformanceBarStatsWorker).not_to receive(:perform_in)
+ expect(client).not_to receive(:sadd)
+
+ peek_adapter.new(client).save('foo')
+ end
+ end
+
+ context 'when exclusive lease has been already taken' do
+ before do
+ stub_exclusive_lease_taken(GitlabPerformanceBarStatsWorker::LEASE_KEY)
+ end
+
+ it 'stores request id but does not enqueue any job' do
+ expect(GitlabPerformanceBarStatsWorker).not_to receive(:perform_in)
+ expect(client).to receive(:sadd).with(GitlabPerformanceBarStatsWorker::STATS_KEY, uuid)
+
+ peek_adapter.new(client).save('foo')
+ end
+ end
+ end
+end
diff --git a/spec/lib/gitlab/performance_bar/stats_spec.rb b/spec/lib/gitlab/performance_bar/stats_spec.rb
new file mode 100644
index 00000000000..c34c6f7b31f
--- /dev/null
+++ b/spec/lib/gitlab/performance_bar/stats_spec.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::PerformanceBar::Stats do
+ describe '#process' do
+ let(:request) { fixture_file('lib/gitlab/performance_bar/peek_data.json') }
+ let(:redis) { double(Gitlab::Redis::SharedState) }
+ let(:logger) { double(Gitlab::PerformanceBar::Logger) }
+ let(:request_id) { 'foo' }
+ let(:stats) { described_class.new(redis) }
+
+ describe '#process' do
+ subject(:process) { stats.process(request_id) }
+
+ before do
+ allow(stats).to receive(:logger).and_return(logger)
+ end
+
+ it 'logs each SQL query including its duration' do
+ allow(redis).to receive(:get).and_return(request)
+
+ expect(logger).to receive(:info)
+ .with({ duration_ms: 1.096, filename: 'lib/gitlab/pagination/offset_pagination.rb',
+ filenum: 53, method: 'add_pagination_headers', request_id: 'foo', type: :sql })
+ expect(logger).to receive(:info)
+ .with({ duration_ms: 0.817, filename: 'lib/api/helpers.rb',
+ filenum: 112, method: 'find_project', request_id: 'foo', type: :sql }).twice
+
+ subject
+ end
+
+ it 'logs an error when the request could not be processed' do
+ allow(redis).to receive(:get).and_return(nil)
+
+ expect(logger).to receive(:error).with(message: anything)
+
+ subject
+ end
+ end
+ end
+end