transaction_spec.rb 5.94 KB
Newer Older
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
require 'spec_helper'

describe Gitlab::Sherlock::Transaction do
  let(:transaction) { described_class.new('POST', '/cat_pictures') }

  describe '#id' do
    it 'returns the transaction ID' do
      expect(transaction.id).to be_an_instance_of(String)
    end
  end

  describe '#type' do
    it 'returns the type' do
      expect(transaction.type).to eq('POST')
    end
  end

  describe '#path' do
    it 'returns the path' do
      expect(transaction.path).to eq('/cat_pictures')
    end
  end

  describe '#queries' do
    it 'returns an Array of queries' do
      expect(transaction.queries).to be_an_instance_of(Array)
    end
  end

  describe '#file_samples' do
    it 'returns an Array of file samples' do
      expect(transaction.file_samples).to be_an_instance_of(Array)
    end
  end

  describe '#started_at' do
    it 'returns the start time' do
      allow(transaction).to receive(:profile_lines).and_yield

      transaction.run { 'cats are amazing' }

      expect(transaction.started_at).to be_an_instance_of(Time)
    end
  end

  describe '#finished_at' do
    it 'returns the completion time' do
      allow(transaction).to receive(:profile_lines).and_yield

      transaction.run { 'cats are amazing' }

      expect(transaction.finished_at).to be_an_instance_of(Time)
    end
  end

56 57 58 59 60 61 62 63 64 65
  describe '#view_counts' do
    it 'returns a Hash' do
      expect(transaction.view_counts).to be_an_instance_of(Hash)
    end

    it 'sets the default value of a key to 0' do
      expect(transaction.view_counts['cats.rb']).to be_zero
    end
  end

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  describe '#run' do
    it 'runs the transaction' do
      allow(transaction).to receive(:profile_lines).and_yield

      retval = transaction.run { 'cats are amazing' }

      expect(retval).to eq('cats are amazing')
    end
  end

  describe '#duration' do
    it 'returns the duration in seconds' do
      start_time = Time.now

      allow(transaction).to receive(:started_at).and_return(start_time)
      allow(transaction).to receive(:finished_at).and_return(start_time + 5)

      expect(transaction.duration).to be_within(0.1).of(5.0)
    end
  end

  describe '#to_param' do
    it 'returns the transaction ID' do
      expect(transaction.to_param).to eq(transaction.id)
    end
  end

  describe '#sorted_queries' do
    it 'returns the queries in descending order' do
      start_time = Time.now

      query1 = Gitlab::Sherlock::Query.new('SELECT 1', start_time, start_time)

      query2 = Gitlab::Sherlock::Query.
        new('SELECT 2', start_time, start_time + 5)

      transaction.queries << query1
      transaction.queries << query2

      expect(transaction.sorted_queries).to eq([query2, query1])
    end
  end

  describe '#sorted_file_samples' do
    it 'returns the file samples in descending order' do
      sample1 = Gitlab::Sherlock::FileSample.new(__FILE__, [], 10.0, 1)
      sample2 = Gitlab::Sherlock::FileSample.new(__FILE__, [], 15.0, 1)

      transaction.file_samples << sample1
      transaction.file_samples << sample2

      expect(transaction.sorted_file_samples).to eq([sample2, sample1])
    end
  end

  describe '#find_query' do
    it 'returns a Query when found' do
      query = Gitlab::Sherlock::Query.new('SELECT 1', Time.now, Time.now)

      transaction.queries << query

      expect(transaction.find_query(query.id)).to eq(query)
    end

    it 'returns nil when no query could be found' do
      expect(transaction.find_query('cats')).to be_nil
    end
  end

  describe '#find_file_sample' do
    it 'returns a FileSample when found' do
      sample = Gitlab::Sherlock::FileSample.new(__FILE__, [], 10.0, 1)

      transaction.file_samples << sample

      expect(transaction.find_file_sample(sample.id)).to eq(sample)
    end

    it 'returns nil when no file sample could be found' do
      expect(transaction.find_file_sample('cats')).to be_nil
    end
  end

  describe '#profile_lines' do
    describe 'when line profiling is enabled' do
      it 'yields the block using the line profiler' do
        allow(Gitlab::Sherlock).to receive(:enable_line_profiler?).
          and_return(true)

        allow_any_instance_of(Gitlab::Sherlock::LineProfiler).
          to receive(:profile).and_return('cats are amazing', [])

        retval = transaction.profile_lines { 'cats are amazing' }

        expect(retval).to eq('cats are amazing')
      end
    end

    describe 'when line profiling is disabled' do
      it 'yields the block' do
        allow(Gitlab::Sherlock).to receive(:enable_line_profiler?).
          and_return(false)

        retval = transaction.profile_lines { 'cats are amazing' }

        expect(retval).to eq('cats are amazing')
      end
    end
  end
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

  describe '#subscribe_to_active_record' do
    let(:subscription) { transaction.subscribe_to_active_record }
    let(:time) { Time.now }
    let(:query_data) { { sql: 'SELECT 1', binds: [] } }

    after do
      ActiveSupport::Notifications.unsubscribe(subscription)
    end

    it 'tracks executed queries' do
      expect(transaction).to receive(:track_query).
        with('SELECT 1', [], time, time)

      subscription.publish('test', time, time, nil, query_data)
    end

    it 'only tracks queries triggered from the transaction thread' do
      expect(transaction).to_not receive(:track_query)

      Thread.new { subscription.publish('test', time, time, nil, query_data) }.
        join
    end
  end

  describe '#subscribe_to_action_view' do
    let(:subscription) { transaction.subscribe_to_action_view }
    let(:time) { Time.now }
    let(:view_data) { { identifier: 'foo.rb' } }

    after do
      ActiveSupport::Notifications.unsubscribe(subscription)
    end

    it 'tracks rendered views' do
      expect(transaction).to receive(:track_view).with('foo.rb')

      subscription.publish('test', time, time, nil, view_data)
    end

    it 'only tracks views rendered from the transaction thread' do
      expect(transaction).to_not receive(:track_view)

      Thread.new { subscription.publish('test', time, time, nil, view_data) }.
        join
    end
  end
222
end