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

worker_data_consistency_with_deduplication_spec.rb « sidekiq_load_balancing « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6e7212b1002fc4aa63e4274360bbf654d628c0ea (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
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rspec-parameterized'
require_relative '../../../../rubocop/cop/sidekiq_load_balancing/worker_data_consistency_with_deduplication'

RSpec.describe RuboCop::Cop::SidekiqLoadBalancing::WorkerDataConsistencyWithDeduplication do
  using RSpec::Parameterized::TableSyntax

  subject(:cop) { described_class.new }

  before do
    allow(cop)
      .to receive(:in_worker?)
      .and_return(true)
  end

  where(:data_consistency) { %i[delayed sticky] }

  with_them do
    let(:strategy) { described_class::DEFAULT_STRATEGY }
    let(:corrected) do
      <<~CORRECTED
      class SomeWorker
        include ApplicationWorker

        data_consistency :#{data_consistency}

        deduplicate #{strategy}, including_scheduled: true
        idempotent!
      end
      CORRECTED
    end

    context 'when deduplication strategy is not explicitly set' do
      it 'registers an offense and corrects using default strategy' do
        expect_offense(<<~CODE)
          class SomeWorker
            include ApplicationWorker

            data_consistency :#{data_consistency}

            idempotent!
            ^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...]
          end
        CODE

        expect_correction(corrected)
      end

      context 'when identation is different' do
        let(:corrected) do
          <<~CORRECTED
            class SomeWorker
                include ApplicationWorker

                data_consistency :#{data_consistency}

                deduplicate #{strategy}, including_scheduled: true
                idempotent!
            end
          CORRECTED
        end

        it 'registers an offense and corrects with correct identation' do
          expect_offense(<<~CODE)
            class SomeWorker
                include ApplicationWorker

                data_consistency :#{data_consistency}

                idempotent!
                ^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...]
            end
          CODE

          expect_correction(corrected)
        end
      end
    end

    context 'when deduplication strategy does not include including_scheduling option' do
      let(:strategy) { ':until_executed' }

      it 'registers an offense and corrects' do
        expect_offense(<<~CODE)
          class SomeWorker
            include ApplicationWorker

            data_consistency :#{data_consistency}

            deduplicate :until_executed
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...]
            idempotent!
          end
        CODE

        expect_correction(corrected)
      end
    end

    context 'when deduplication strategy has including_scheduling option disabled' do
      let(:strategy) { ':until_executed' }

      it 'registers an offense and corrects' do
        expect_offense(<<~CODE)
          class SomeWorker
            include ApplicationWorker

            data_consistency :#{data_consistency}

            deduplicate :until_executed, including_scheduled: false
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...]
            idempotent!
          end
        CODE

        expect_correction(corrected)
      end
    end

    context "when deduplication strategy is :none" do
      it 'does not register an offense' do
        expect_no_offenses(<<~CODE)
          class SomeWorker
            include ApplicationWorker

            data_consistency :always

            deduplicate :none
            idempotent!
          end
        CODE
      end
    end

    context "when deduplication strategy has including_scheduling option enabled" do
      it 'does not register an offense' do
        expect_no_offenses(<<~CODE)
          class SomeWorker
            include ApplicationWorker

            data_consistency :always

            deduplicate :until_executing, including_scheduled: true
            idempotent!
          end
        CODE
      end
    end
  end

  context "data_consistency: :always" do
    it 'does not register an offense' do
      expect_no_offenses(<<~CODE)
        class SomeWorker
          include ApplicationWorker

          data_consistency :always

          idempotent!
        end
      CODE
    end
  end
end