summaryrefslogtreecommitdiff
path: root/spec/workers/concerns/worker_attributes_spec.rb
blob: d4b17c65f467fbc7fcca1aefe36ae70c350d55ff (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WorkerAttributes do
  let(:worker) do
    Class.new do
      def self.name
        "TestWorker"
      end

      include ApplicationWorker
    end
  end

  describe '.data_consistency' do
    context 'with valid data_consistency' do
      it 'returns correct data_consistency' do
        worker.data_consistency(:sticky)

        expect(worker.get_data_consistency).to eq(:sticky)
      end
    end

    context 'when data_consistency is not provided' do
      it 'defaults to :always' do
        expect(worker.get_data_consistency).to eq(:always)
      end
    end

    context 'with invalid data_consistency' do
      it 'raise exception' do
        expect { worker.data_consistency(:invalid) }
          .to raise_error('Invalid data consistency: invalid')
      end
    end

    context 'when job is idempotent' do
      context 'when data_consistency is not :always' do
        it 'raise exception' do
          worker.idempotent!

          expect { worker.data_consistency(:sticky) }
            .to raise_error("Class can't be marked as idempotent if data_consistency is not set to :always")
        end
      end

      context 'when feature_flag is provided' do
        before do
          stub_feature_flags(test_feature_flag: false)
          skip_feature_flags_yaml_validation
          skip_default_enabled_yaml_check
        end

        it 'returns correct feature flag value' do
          worker.data_consistency(:sticky, feature_flag: :test_feature_flag)

          expect(worker.get_data_consistency_feature_flag_enabled?).not_to be_truthy
        end
      end
    end
  end

  describe '.idempotent!' do
    it 'sets `idempotent` attribute of the worker class to true' do
      worker.idempotent!

      expect(worker.send(:class_attributes)[:idempotent]).to eq(true)
    end

    context 'when data consistency is not :always' do
      it 'raise exception' do
        worker.data_consistency(:sticky)

        expect { worker.idempotent! }
          .to raise_error("Class can't be marked as idempotent if data_consistency is not set to :always")
      end
    end
  end

  describe '.idempotent?' do
    subject(:idempotent?) { worker.idempotent? }

    context 'when the worker is idempotent' do
      before do
        worker.idempotent!
      end

      it { is_expected.to be_truthy }
    end

    context 'when the worker is not idempotent' do
      it { is_expected.to be_falsey }
    end
  end

  describe '.deduplicate' do
    it 'sets deduplication_strategy and deduplication_options' do
      worker.deduplicate(:until_executing, including_scheduled: true)

      expect(worker.send(:class_attributes)[:deduplication_strategy]).to eq(:until_executing)
      expect(worker.send(:class_attributes)[:deduplication_options]).to eq(including_scheduled: true)
    end
  end

  describe '#deduplication_enabled?' do
    subject(:deduplication_enabled?) { worker.deduplication_enabled? }

    context 'when no feature flag is set' do
      before do
        worker.deduplicate(:until_executing)
      end

      it { is_expected.to eq(true) }
    end

    context 'when feature flag is set' do
      before do
        skip_feature_flags_yaml_validation
        skip_default_enabled_yaml_check

        worker.deduplicate(:until_executing, feature_flag: :my_feature_flag)
      end

      context 'when the FF is enabled' do
        before do
          stub_feature_flags(my_feature_flag: true)
        end

        it { is_expected.to eq(true) }
      end

      context 'when the FF is disabled' do
        before do
          stub_feature_flags(my_feature_flag: false)
        end

        it { is_expected.to eq(false) }
      end
    end
  end
end