Welcome to mirror list, hosted at ThFree Co, Russian Federation.

batch_average_counter_spec.rb « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 43c7a1554f792331c2772b4a8e8f75ea76489275 (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
57
58
59
60
61
62
63
64
65
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::BatchAverageCounter do
  let(:model)  { Issue }
  let(:column) { :weight }

  let(:in_transaction) { false }

  before do
    allow(model.connection).to receive(:transaction_open?).and_return(in_transaction)
  end

  describe '#count' do
    before do
      create_list(:issue, 2, weight: 4)
      create_list(:issue, 2, weight: 2)
      create_list(:issue, 2, weight: 3)
    end

    subject(:batch_average_counter) { described_class.new(model, column) }

    it 'returns correct average of weights' do
      expect(subject.count).to eq(3.0)
    end

    it 'does no raise an exception if transaction is not open' do
      expect { subject.count }.not_to raise_error
    end

    context 'when transaction is open' do
      let(:in_transaction) { true }

      it 'raises an error' do
        expect { subject.count }.to raise_error('BatchAverageCounter can not be run inside a transaction')
      end
    end

    context 'when batch size is small' do
      let(:batch_size) { 2 }

      it 'returns correct average of weights' do
        expect(subject.count(batch_size: batch_size)).to eq(3.0)
      end
    end

    context 'when column passed is an Arel attribute' do
      let(:column) { model.arel_table[:weight] }

      it 'returns correct average of weights' do
        expect(subject.count).to eq(3.0)
      end
    end

    context 'when column has total count of zero' do
      before do
        Issue.update_all(weight: nil)
      end

      it 'returns the fallback value' do
        expect(subject.count).to eq(-1)
      end
    end

    context 'when one batch has nil weights (no average)' do
      before do
        issues = Issue.where(weight: 4)
        issues.update_all(weight: nil)
      end

      let(:batch_size) { 2 }

      it 'calculates average of weights with no errors' do
        expect(subject.count(batch_size: batch_size)).to eq(2.5)
      end
    end

    context 'when batch fetch query is cancelled' do
      let(:batch_size) { 22_000 }
      let(:relation) { instance_double(ActiveRecord::Relation, to_sql: batch_average_query) }

      context 'when all retries fail' do
        let(:batch_average_query) { 'SELECT AVG(weight) FROM issues WHERE weight BETWEEN 2 and 5' }
        let(:query_timed_out_exception) { ActiveRecord::QueryCanceled.new('query timed out') }

        before do
          allow(model).to receive(:where).and_return(relation)
          allow(relation).to receive(:pick).and_raise(query_timed_out_exception)
        end

        it 'logs failing query' do
          expect(Gitlab::AppJsonLogger).to receive(:error).with(
            event: 'batch_count',
            relation: model.table_name,
            operation: 'average',
            start: 2,
            query: batch_average_query,
            message: 'Query has been canceled with message: query timed out'
          )

          expect(subject.count(batch_size: batch_size)).to eq(-1)
        end
      end
    end
  end
end