Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/facter/custom_facts/core/resolvable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ module Resolvable
# Return the timeout period for resolving a value.
# (see #timeout)
# @return [Numeric]
# @comment requiring 'timeout' stdlib class causes Object#timeout to be
# defined which delegates to Timeout.timeout. This method may potentially
# overwrite the #timeout attr_reader on this class, so we define #limit to
# avoid conflicts.
# @deprecated Use timeout instead.
def limit
Facter::Log.new(self).warnonce('limit is deprecated and will be removed in a future version, use timeout instead')
@timeout || 0
end

Expand Down Expand Up @@ -74,14 +72,14 @@ def value
result = nil

with_timing do
Timeout.timeout(limit) do
Timeout.timeout(@timeout || 0) do
result = resolve_value
end
end

LegacyFacter::Util::Normalization.normalize(result)
rescue Timeout::Error => e
Facter.log_exception(e, "Timed out after #{limit} seconds while resolving #{qualified_name}")
Facter.log_exception(e, "Timed out after #{@timeout || 0} seconds while resolving #{qualified_name}")

nil
rescue LegacyFacter::Util::Normalization::NormalizationError => e
Expand Down
20 changes: 17 additions & 3 deletions spec/custom_facts/core/resolvable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@ class FlushFakeError < StandardError; end
describe LegacyFacter::Core::Resolvable do
subject(:resolvable) { FacterSpec::ResolvableClass.new('resolvable') }

let(:log) { instance_spy(Facter::Log) }

before do
allow(Facter::Log).to receive(:new).and_return(log)
end

after do
Facter.remove_instance_variable(:@logger) if Facter.instance_variable_defined?(:@logger)
end

it 'has a default timeout of 0 seconds' do
expect(resolvable.limit).to eq 0
end

it 'emits a deprecation warning when limit is called' do
expect(log).to receive(:warnonce).with('limit is deprecated and will be removed in a future version, use timeout instead')
resolvable.limit
end

it 'can specify a custom timeout' do
resolvable.timeout = 10
expect(resolvable.limit).to eq 10
Expand Down Expand Up @@ -61,9 +76,8 @@ class FlushFakeError < StandardError; end
end

describe 'timing out' do
it 'uses #limit instead of #timeout to determine the timeout period' do
expect(resolvable).not_to receive(:timeout)
allow(resolvable).to receive(:limit).and_return(25)
it 'uses @timeout directly to determine the timeout period' do
resolvable.timeout = 25
allow(Timeout).to receive(:timeout).with(25)

resolvable.value
Expand Down
Loading