summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/request_profiler_spec.rb
blob: f157189a72da297dbd53a795e894025324b87a30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::RequestProfiler do
  describe '.profile_token' do
    it 'returns a token' do
      expect(described_class.profile_token).to be_present
    end

    it 'caches the token' do
      expect(Rails.cache).to receive(:fetch).with('profile-token')

      described_class.profile_token
    end
  end

  context 'with temporary PROFILES_DIR' do
    let(:tmpdir) { Dir.mktmpdir('profiler-test') }
    let(:profile_name) { '|api|v4|version.txt_1562854738_memory.html' }
    let(:profile_path) { File.join(tmpdir, profile_name) }

    before do
      stub_const('Gitlab::RequestProfiler::PROFILES_DIR', tmpdir)
      FileUtils.touch(profile_path)
    end

    after do
      FileUtils.rm_rf(tmpdir)
    end

    describe '.remove_all_profiles' do
      it 'removes Gitlab::RequestProfiler::PROFILES_DIR directory' do
        described_class.remove_all_profiles

        expect(Dir.exist?(tmpdir)).to be false
      end
    end

    describe '.all' do
      subject { described_class.all }

      it 'returns all profiles' do
        expect(subject.map(&:name)).to contain_exactly(profile_name)
      end
    end

    describe '.find' do
      subject { described_class.find(profile_name) }

      it 'returns all profiles' do
        expect(subject.name).to eq(profile_name)
      end
    end
  end
end