2021-04-29 21:17:54 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe WorkerAttributes do
|
2022-07-23 23:45:48 +05:30
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
let(:worker) do
|
|
|
|
Class.new do
|
|
|
|
def self.name
|
|
|
|
"TestWorker"
|
|
|
|
end
|
|
|
|
|
|
|
|
include ApplicationWorker
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
let(:child_worker) do
|
|
|
|
Class.new(worker) do
|
|
|
|
def self.name
|
|
|
|
"TestChildworker"
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'class attributes' do
|
|
|
|
# rubocop: disable Layout/LineLength
|
|
|
|
where(:getter, :setter, :default, :values, :expected) do
|
|
|
|
:get_feature_category | :feature_category | nil | [:foo] | :foo
|
|
|
|
:get_urgency | :urgency | :low | [:high] | :high
|
|
|
|
:get_data_consistency | :data_consistency | :always | [:sticky] | :sticky
|
|
|
|
:get_worker_resource_boundary | :worker_resource_boundary | :unknown | [:cpu] | :cpu
|
|
|
|
:get_weight | :weight | 1 | [3] | 3
|
|
|
|
:get_tags | :tags | [] | [:foo, :bar] | [:foo, :bar]
|
|
|
|
:get_deduplicate_strategy | :deduplicate | :until_executing | [:none] | :none
|
|
|
|
:get_deduplication_options | :deduplicate | {} | [:none, including_scheduled: true] | { including_scheduled: true }
|
|
|
|
:worker_has_external_dependencies? | :worker_has_external_dependencies! | false | [] | true
|
|
|
|
:idempotent? | :idempotent! | false | [] | true
|
|
|
|
:big_payload? | :big_payload! | false | [] | true
|
|
|
|
end
|
|
|
|
# rubocop: enable Layout/LineLength
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
context 'when the attribute is set' do
|
|
|
|
before do
|
|
|
|
worker.public_send(setter, *values)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the expected value' do
|
|
|
|
expect(worker.public_send(getter)).to eq(expected)
|
|
|
|
expect(child_worker.public_send(getter)).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the attribute is not set' do
|
|
|
|
it 'returns the default value' do
|
|
|
|
expect(worker.public_send(getter)).to eq(default)
|
|
|
|
expect(child_worker.public_send(getter)).to eq(default)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the attribute is set in the child worker' do
|
|
|
|
before do
|
|
|
|
child_worker.public_send(setter, *values)
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it 'returns the default value for the parent, and the expected value for the child' do
|
|
|
|
expect(worker.public_send(getter)).to eq(default)
|
|
|
|
expect(child_worker.public_send(getter)).to eq(expected)
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
describe '.data_consistency' do
|
2021-04-29 21:17:54 +05:30
|
|
|
context 'with invalid data_consistency' do
|
|
|
|
it 'raise exception' do
|
|
|
|
expect { worker.data_consistency(:invalid) }
|
|
|
|
.to raise_error('Invalid data consistency: invalid')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
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
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
it 'returns correct feature flag value' do
|
|
|
|
worker.data_consistency(:sticky, feature_flag: :test_feature_flag)
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
expect(worker.get_data_consistency_feature_flag_enabled?).not_to be(true)
|
|
|
|
expect(child_worker.get_data_consistency_feature_flag_enabled?).not_to be(true)
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it 'returns true' do
|
|
|
|
expect(worker.deduplication_enabled?).to be(true)
|
|
|
|
expect(child_worker.deduplication_enabled?).to be(true)
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
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
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it 'returns true' do
|
|
|
|
expect(worker.deduplication_enabled?).to be(true)
|
|
|
|
expect(child_worker.deduplication_enabled?).to be(true)
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the FF is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(my_feature_flag: false)
|
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it 'returns false' do
|
|
|
|
expect(worker.deduplication_enabled?).to be(false)
|
|
|
|
expect(child_worker.deduplication_enabled?).to be(false)
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|