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

object_counter_spec.rb « github_import « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aa551195a35c60169bb4330091c38f712ae997ba (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
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::ObjectCounter, :clean_gitlab_redis_cache, feature_category: :importers do
  let_it_be(:project) { create(:project, :import_started, import_type: 'github', import_url: 'https://github.com/vim/vim.git') }

  before do
    allow(Gitlab::Redis::SharedState).to receive(:with).and_return('OK')
  end

  it 'validates the operation being incremented' do
    expect { described_class.increment(project, :issue, :unknown) }
      .to raise_error(ArgumentError, 'operation must be fetched or imported')
  end

  it 'increments the counter and saves the key to be listed in the summary later' do
    expect(Gitlab::Metrics)
      .to receive(:counter)
      .twice
      .with(:github_importer_fetched_issue, 'The number of fetched Github Issue')
      .and_return(double(increment: true))

    expect(Gitlab::Metrics)
      .to receive(:counter)
      .twice
      .with(:github_importer_imported_issue, 'The number of imported Github Issue')
      .and_return(double(increment: true))

    described_class.increment(project, :issue, :fetched)
    described_class.increment(project, :issue, :fetched)
    described_class.increment(project, :issue, :imported)
    described_class.increment(project, :issue, :imported)

    expect(described_class.summary(project)).to eq({
      'fetched' => { 'issue' => 2 },
      'imported' => { 'issue' => 2 }
    })
  end

  it 'does not increment the counter if the given value is <= 0' do
    expect(Gitlab::Metrics)
      .not_to receive(:counter)

    described_class.increment(project, :issue, :fetched, value: 0)
    described_class.increment(project, :issue, :imported, value: nil)

    expect(described_class.summary(project)).to eq({
      'fetched' => {},
      'imported' => {}
    })
  end

  it 'expires etag cache of relevant realtime change endpoints on increment' do
    expect_next_instance_of(Gitlab::EtagCaching::Store) do |instance|
      expect(instance).to receive(:touch).with(Gitlab::Routing.url_helpers.realtime_changes_import_github_path(format: :json))
    end

    described_class.increment(project, :issue, :fetched)
  end

  describe '.summary' do
    context 'when there are cached import statistics' do
      before do
        described_class.increment(project, :issue, :fetched, value: 10)
        described_class.increment(project, :issue, :imported, value: 8)
      end

      it 'includes cached object counts stats in response' do
        expect(described_class.summary(project)).to eq(
          'fetched' => { 'issue' => 10 },
          'imported' => { 'issue' => 8 }
        )
      end

      it 'uses the same TTL as when incrementing' do
        expect(Gitlab::Cache::Import::Caching)
          .to receive(:read_integer)
          .with(anything, timeout: described_class::IMPORT_CACHING_TIMEOUT)
          .twice
          .and_call_original

        described_class.summary(project)
      end
    end

    context 'when import is in progress but cache expired' do
      before do
        described_class.increment(project, :issue, :fetched, value: 10)
        described_class.increment(project, :issue, :imported, value: 8)
        allow(Gitlab::Cache::Import::Caching).to receive(:read_integer).and_return(nil)
      end

      it 'returns 0 instead of nil so process can complete' do
        expect(described_class.summary(project)).to eq(
          {
            "fetched" => {
              "issue" => 0
            },
            "imported" => {
              "issue" => 0
            }
          }
        )
      end
    end

    context 'when there are no cached import statistics' do
      context 'when project import is in progress' do
        it 'includes an empty object counts stats in response' do
          expect(described_class.summary(project)).to eq(described_class::EMPTY_SUMMARY)
        end
      end

      context 'when project import is not in progress' do
        let(:checksums) do
          {
            'fetched' => {
              "issue" => 2,
              "label" => 10,
              "note" => 2,
              "protected_branch" => 2,
              "pull_request" => 2
            },
            "imported" => {
              "issue" => 2,
              "label" => 10,
              "note" => 2,
              "protected_branch" => 2,
              "pull_request" => 2
            }
          }
        end

        before do
          project.import_state.update_columns(checksums: checksums, status: :finished)
        end

        it 'includes project import checksums in response' do
          expect(described_class.summary(project)).to eq(checksums)
        end
      end
    end
  end
end