diff options
author | Luke Kanies <luke@madstop.com> | 2008-05-20 23:12:06 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-05-20 23:12:06 -0500 |
commit | 400bab9fcf167bc76710a260822e0b0e114c98fa (patch) | |
tree | 42e7294cf4a56664a56adac8062468e39535f9bc | |
parent | d235f26cb7be5b24d63f7f9a57aabdcf86fb16b4 (diff) | |
download | facter-400bab9fcf167bc76710a260822e0b0e114c98fa.tar.gz facter-400bab9fcf167bc76710a260822e0b0e114c98fa.tar.xz facter-400bab9fcf167bc76710a260822e0b0e114c98fa.zip |
Adding a timeout to fact retrieval, fixing #56.
The timeout is currently 0.5 seconds, which should generally
be enough time.
-rw-r--r-- | lib/facter/util/fact.rb | 12 | ||||
-rwxr-xr-x | spec/unit/util/fact.rb | 7 |
2 files changed, 18 insertions, 1 deletions
diff --git a/lib/facter/util/fact.rb b/lib/facter/util/fact.rb index c73dfbf..0b2d5b9 100644 --- a/lib/facter/util/fact.rb +++ b/lib/facter/util/fact.rb @@ -1,6 +1,8 @@ require 'facter' require 'facter/util/resolution' +require 'timeout' + class Facter::Util::Fact attr_accessor :name, :searching, :ldapname @@ -80,7 +82,15 @@ class Facter::Util::Fact next unless resolve.suitable? foundsuits = true - tmp = resolve.value + tmp = nil + begin + Timeout.timeout(0.5) do + tmp = resolve.value + end + rescue Timeout::Error => detail + warn "Timed out seeking value for %s" % self.name + next + end break tmp unless tmp.nil? or tmp == "" } diff --git a/spec/unit/util/fact.rb b/spec/unit/util/fact.rb index 1652032..cee70b9 100755 --- a/spec/unit/util/fact.rb +++ b/spec/unit/util/fact.rb @@ -121,6 +121,13 @@ describe Facter::Util::Fact do @fact.value.should be_nil end + + it "should timeout after 0.5 seconds" do + @fact.expects(:warn) + @fact.add { setcode { sleep 2; raise "This is a test" } } + + @fact.value.should be_nil + end end it "should have a method for flushing the cached fact" do |