This is a small modification of Trevor's work,
mostly redoing the tests.

Signed-off-by: Luke Kanies <[email protected]>
---
 lib/puppet/transaction.rb    |   16 ++++++++--------
 lib/puppet/type.rb           |    9 +++++----
 spec/integration/defaults.rb |    4 ++++
 spec/unit/transaction.rb     |   37 +++++++++++++++++++------------------
 spec/unit/type.rb            |   11 +++++++++++
 5 files changed, 47 insertions(+), 30 deletions(-)

diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb
index b3099b8..fb05d9d 100644
--- a/lib/puppet/transaction.rb
+++ b/lib/puppet/transaction.rb
@@ -282,14 +282,14 @@ class Transaction
                 end
                 ret = nil
                 seconds = thinmark do
-                begin 
-                    Timeout::timeout(resource[:timeout]) {
-                        ret = eval_resource(resource)
-                    }
-                 rescue(Timeout::Error) 
-                     resource.err("#{resource.to_s} expired at 
#{resource[:timeout]} seconds.")
-                     @failures[resource] += 1
-                 end
+                    begin 
+                        Timeout::timeout(resource[:timeout]) {
+                            ret = eval_resource(resource)
+                        }
+                    rescue(Timeout::Error) 
+                        resource.err("expired at #{resource[:timeout]} 
seconds.")
+                        @failures[resource] += 1
+                    end
                 end
 
                 if Puppet[:evaltrace] and @catalog.host_config?
diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb
index 77a4165..6e1bc45 100644
--- a/lib/puppet/type.rb
+++ b/lib/puppet/type.rb
@@ -1253,14 +1253,15 @@ class Type
     end
 
     newmetaparam(:timeout) do
-        desc "The time that a component is allowed to run prior to being 
forced to exit. Defaults to '0' (infinite)."
-        defaultto Puppet[:timeout].to_i
+        desc "The time that a component is allowed to run prior to being 
forced to exit. Defaults to the value of the 'timeout' setting, which defaults 
to '0' (infinite)."
 
-        munge do |timeout|
+        defaultto { Puppet[:timeout] }
+
+        munge do |val|
             val = val.shift if val.is_a?(Array)
             begin
                 Float(val)
-            rescue
+            rescue => detail
                 raise ArgumentError, "Timeout was not a number: %s" % detail
             end
         end
diff --git a/spec/integration/defaults.rb b/spec/integration/defaults.rb
index 780b3e1..f2f882e 100755
--- a/spec/integration/defaults.rb
+++ b/spec/integration/defaults.rb
@@ -96,4 +96,8 @@ describe "Puppet defaults" do
             ENV["PATH"].split(File::PATH_SEPARATOR).should be_include("/sbin")
         end
     end
+
+    it "should have a :timeout setting defaulting to 0" do
+        Puppet.settings[:timeout].should == 0
+    end
 end
diff --git a/spec/unit/transaction.rb b/spec/unit/transaction.rb
index 778621e..b805927 100755
--- a/spec/unit/transaction.rb
+++ b/spec/unit/transaction.rb
@@ -87,36 +87,37 @@ describe Puppet::Transaction, " when evaluating" do
         @transaction = Puppet::Transaction.new(@catalog)
     end
 
-    it "should have a defalut timeout value of 0" do
-        Puppet::Type.type(:exec).new(:name => "/bin/sleep 1")[:timeout].should 
== Float(0)
-    end
-
-    it "should timeout with a useful message when the running time of the 
resource is greater than the 'timeout' parameter" do
-        resource = Puppet::Type.type(:exec).new(:name => "/bin/sleep 3", 
:timeout => "1")
+    it "should use the resource's timeout to set a timeout on each resource" do
+        resource = Puppet::Type.type(:exec).new(:name => "/bin/sleep 3", 
:timeout => 50)
         @catalog.add_resource(resource)
 
-        resource.expects(:err)
-        lambda { @transaction.evaluate }.should_not raise_error
+        Timeout.expects(:timeout).with(50.0)
+        @transaction.expects(:eval_resource).with(resource).never # because we 
didn't yield from the above method
+
+        @transaction.evaluate
     end
 
-    it "should not timeout when the running time of the resource is less than 
the 'timeout' parameter" do
-        resource = Puppet::Type.type(:exec).create(:name => "/bin/sleep 3", 
:timeout => "5")
+    it "should log but not propagate timeout errors" do
+        resource = Puppet::Type.type(:exec).new(:name => "/bin/sleep 3", 
:timeout => 50)
         @catalog.add_resource(resource)
 
-        lambda { @transaction.evaluate }.should_not raise_error
-    end
+        Timeout.expects(:timeout).with(50.0).yields
+        @transaction.expects(:eval_resource).with(resource).raises 
Timeout::Error
 
-    it "should not timeout when the running time of the resource is equal to 
0" do
-        resource = Puppet::Type.type(:exec).create(:name => "/bin/sleep 3", 
:timeout => "0")
-        @catalog.add_resource(resource)
+        resource.expects(:err)
 
         lambda { @transaction.evaluate }.should_not raise_error
     end
 
-    it "should not timeout when the running time of the resource is negative" 
do
-        resource = Puppet::Type.type(:exec).create(:name => "/bin/sleep 3", 
:timeout => "-1")
+    it "should record a resource that times out as failed" do
+        resource = Puppet::Type.type(:exec).new(:name => "/bin/sleep 3", 
:timeout => 50)
         @catalog.add_resource(resource)
 
-        lambda { @transaction.evaluate }.should_not raise_error
+        Timeout.expects(:timeout).with(50.0).yields
+        @transaction.expects(:eval_resource).with(resource).raises 
Timeout::Error
+
+        @transaction.evaluate
+
+        @transaction.should be_failed(resource)
     end
 end
diff --git a/spec/unit/type.rb b/spec/unit/type.rb
index 90dbd39..e0ad44c 100755
--- a/spec/unit/type.rb
+++ b/spec/unit/type.rb
@@ -42,6 +42,17 @@ describe Puppet::Type do
         Puppet::Type.type(:mount).new(:name => "foo").parameter(:noop).should 
be_nil
     end
 
+    it "should have a :timeout metaparam" do
+        Puppet::Type.metaparamclass(:timeout).should 
equal(Puppet::Type::MetaParamTimeout)
+    end
+
+    it "should default to the value of the :timeout setting, converted to a 
float, as its timeout value" do
+        Puppet.settings.expects(:value).with(:timeout).returns "5.0"
+        resource = Puppet::Type.type(:mount).new(:name => "foo")
+
+        resource[:timeout].should == 5.0
+    end
+
     describe "when initializing" do
         describe "and passed a TransObject" do
             it "should fail" do
-- 
1.6.1


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to