Skip to content

Commit

Permalink
specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mensfeld committed Oct 25, 2023
1 parent 2d1d043 commit 0a52d70
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/karafka/core/configurable/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ def compile

if lazy_leaf && !initialized
build_dynamic_accessor(value)
elsif lazy_leaf && initialized
singleton_class.attr_accessor(value.name)
public_send("#{value.name}=", initialized)
else
public_send("#{value.name}=", initialized)
end
Expand Down
90 changes: 90 additions & 0 deletions spec/lib/karafka/core/configurable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,51 @@
expect(configurable_class.config.to_h[:superscope][:additional]).to eq(7)
end
end

context 'when we define a lazy evaluated root setting' do
let(:configurable_class) do
default1 = default
constructor1 = constructor

Class.new do
extend Karafka::Core::Configurable

setting(
:lazy_setting,
default: default1,
constructor: constructor1,
lazy: true
)
end
end

let(:config) { configurable_class.config }
let(:constructor) { ->(default) { default || 1 } }

context 'when default is not false nor nil' do
let(:default) { 100 }

it { expect(config.lazy_setting).to eq(100) }
end

context 'when default is false' do
let(:default) { false }

it { expect(config.lazy_setting).to eq(1) }
end

context 'when default is false and value is false for some time' do
let(:attempts) { [1, 10, false, false, false, false] }
let(:default) { false }
let(:constructor) { ->(default) { default || attempts.pop } }

it 'expect to retry until non-false is present and then cache it' do
3.times { expect(config.lazy_setting).to eq(false) }
expect(config.lazy_setting).to eq(10)
expect(config.lazy_setting).to eq(10)
end
end
end
end

context 'when we define settings on an instance level' do
Expand Down Expand Up @@ -290,5 +335,50 @@ def testable
end
end
end

context 'when we define a lazy evaluated root setting' do
let(:configurable_class) do
default1 = default
constructor1 = constructor

Class.new do
include Karafka::Core::Configurable

setting(
:lazy_setting,
default: default1,
constructor: constructor1,
lazy: true
)
end
end

let(:config) { configurable_class.new.tap(&:configure).config }
let(:constructor) { ->(default) { default || 1 } }

context 'when default is not false nor nil' do
let(:default) { 100 }

it { expect(config.lazy_setting).to eq(100) }
end

context 'when default is false' do
let(:default) { false }

it { expect(config.lazy_setting).to eq(1) }
end

context 'when default is false and value is false for some time' do
let(:attempts) { [1, 10, false, false, false, false, false] }
let(:default) { false }
let(:constructor) { ->(default) { default || attempts.pop } }

it 'expect to retry until non-false is present and then cache it' do
3.times { expect(config.lazy_setting).to eq(false) }
expect(config.lazy_setting).to eq(10)
expect(config.lazy_setting).to eq(10)
end
end
end
end
end

0 comments on commit 0a52d70

Please sign in to comment.