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

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

require 'spec_helper'
require 'rspec-parameterized'

RSpec.describe Gitlab::SidekiqConfig::WorkerRouter do
  describe '.queue_name_from_worker_name' do
    using RSpec::Parameterized::TableSyntax

    def create_worker(name, namespace = nil)
      Class.new.tap do |worker|
        worker.define_singleton_method(:name) { name }
        worker.define_singleton_method(:queue_namespace) { namespace }
      end
    end

    where(:worker, :expected_name) do
      create_worker('PagesWorker') | 'pages'
      create_worker('PipelineNotificationWorker') | 'pipeline_notification'
      create_worker('PostReceive') | 'post_receive'
      create_worker('PostReceive', :git) | 'git:post_receive'
      create_worker('PipelineHooksWorker', :pipeline_hooks) | 'pipeline_hooks:pipeline_hooks'
      create_worker('Gitlab::JiraImport::AdvanceStageWorker') | 'jira_import_advance_stage'
      create_worker('Gitlab::PhabricatorImport::ImportTasksWorker', :importer) | 'importer:phabricator_import_import_tasks'
    end

    with_them do
      it 'generates a valid queue name from worker name' do
        expect(described_class.queue_name_from_worker_name(worker)).to eql(expected_name)
      end
    end
  end

  shared_context 'router examples setup' do
    using RSpec::Parameterized::TableSyntax

    let(:worker) do
      Class.new do
        def self.name
          'Gitlab::Foo::BarWorker'
        end

        include ApplicationWorker

        feature_category :feature_a
        urgency :low
        worker_resource_boundary :cpu
        tags :expensive
      end
    end

    where(:routing_rules, :expected_queue) do
      # Default, no configuration
      [] | 'foo_bar'
      # Does not match, fallback to the named queue
      [
        ['feature_category=feature_b|urgency=high', 'queue_a'],
        ['resource_boundary=memory', 'queue_b'],
        ['tags=cheap', 'queue_c']
      ] | 'foo_bar'
      # Match a nil queue, fallback to named queue
      [
        ['feature_category=feature_b|urgency=high', 'queue_a'],
        ['resource_boundary=cpu', nil],
        ['tags=cheap', 'queue_c']
      ] | 'foo_bar'
      # Match an empty string, fallback to named queue
      [
        ['feature_category=feature_b|urgency=high', 'queue_a'],
        ['resource_boundary=cpu', ''],
        ['tags=cheap', 'queue_c']
      ] | 'foo_bar'
      # Match the first rule
      [
        ['feature_category=feature_a|urgency=high', 'queue_a'],
        ['resource_boundary=cpu', 'queue_b'],
        ['tags=cheap', 'queue_c']
      ] | 'queue_a'
      # Match the first rule 2
      [
        ['feature_category=feature_b|urgency=low', 'queue_a'],
        ['resource_boundary=cpu', 'queue_b'],
        ['tags=cheap', 'queue_c']
      ] | 'queue_a'
      # Match the third rule
      [
        ['feature_category=feature_b|urgency=high', 'queue_a'],
        ['resource_boundary=memory', 'queue_b'],
        ['tags=expensive', 'queue_c']
      ] | 'queue_c'
      # Match all, first match wins
      [
        ['feature_category=feature_a|urgency=low', 'queue_a'],
        ['resource_boundary=cpu', 'queue_b'],
        ['tags=expensive', 'queue_c']
      ] | 'queue_a'
      # Match the same rule multiple times, the first match wins
      [
        ['feature_category=feature_a', 'queue_a'],
        ['feature_category=feature_a', 'queue_b'],
        ['feature_category=feature_a', 'queue_c']
      ] | 'queue_a'
      # Match wildcard
      [
        ['feature_category=feature_b|urgency=high', 'queue_a'],
        ['resource_boundary=memory', 'queue_b'],
        ['tags=cheap', 'queue_c'],
        ['*', 'default']
      ] | 'default'
      # Match wildcard at the top of the chain. It makes the following rules useless
      [
        ['*', 'queue_foo'],
        ['feature_category=feature_a|urgency=low', 'queue_a'],
        ['resource_boundary=cpu', 'queue_b'],
        ['tags=expensive', 'queue_c']
      ] | 'queue_foo'
    end
  end

  describe '.global' do
    before do
      described_class.remove_instance_variable(:@global_worker_router) if described_class.instance_variable_defined?(:@global_worker_router)
    end

    after do
      described_class.remove_instance_variable(:@global_worker_router)
    end

    context 'valid routing rules' do
      include_context 'router examples setup'

      with_them do
        before do
          stub_config(sidekiq: { routing_rules: routing_rules })
        end

        it 'routes the worker to the correct queue' do
          expect(described_class.global.route(worker)).to eql(expected_queue)
        end
      end
    end

    context 'invalid routing rules' do
      let(:worker) do
        Class.new do
          def self.name
            'Gitlab::Foo::BarWorker'
          end

          include ApplicationWorker
        end
      end

      before do
        stub_config(sidekiq: { routing_rules: routing_rules })
      end

      context 'invalid routing rules format' do
        let(:routing_rules) { ['feature_category=a'] }

        it 'captures the error and falls back to an empty route' do
          expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).with(be_a(described_class::InvalidRoutingRuleError))

          expect(described_class.global.route(worker)).to eql('foo_bar')
        end
      end

      context 'invalid predicate' do
        let(:routing_rules) { [['invalid_term=a', 'queue_a']] }

        it 'captures the error and falls back to an empty route' do
          expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).with(
            be_a(Gitlab::SidekiqConfig::WorkerMatcher::UnknownPredicate)
          )

          expect(described_class.global.route(worker)).to eql('foo_bar')
        end
      end
    end
  end

  describe '#route' do
    context 'valid routing rules' do
      include_context 'router examples setup'

      with_them do
        it 'routes the worker to the correct queue' do
          router = described_class.new(routing_rules)

          expect(router.route(worker)).to eql(expected_queue)
        end
      end
    end

    context 'invalid routing rules' do
      it 'raises an exception' do
        expect { described_class.new(nil) }.to raise_error(described_class::InvalidRoutingRuleError)
        expect { described_class.new(['feature_category=a']) }.to raise_error(described_class::InvalidRoutingRuleError)
        expect { described_class.new([['feature_category=a', 'queue_a', 'queue_b']]) }.to raise_error(described_class::InvalidRoutingRuleError)
        expect do
          described_class.new(
            [
              ['feature_category=a', 'queue_b'],
              ['feature_category=b']
            ]
          )
        end.to raise_error(described_class::InvalidRoutingRuleError)
        expect { described_class.new([['invalid_term=a', 'queue_a']]) }.to raise_error(Gitlab::SidekiqConfig::WorkerMatcher::UnknownPredicate)
      end
    end
  end
end