From 3d306b665008373539b7c0f775c1766f62416f5b Mon Sep 17 00:00:00 2001 From: Kyle Rames Date: Wed, 12 Dec 2012 12:14:44 -0600 Subject: [PATCH] Updated Fog.wait_for to throw a timeout exception instead of returning false #1368 --- lib/fog/brightbox/models/compute/server.rb | 9 ++++----- lib/fog/core/errors.rb | 2 ++ lib/fog/core/wait_for.rb | 2 +- tests/core/wait_for_tests.rb | 13 +++++++++++++ 4 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 tests/core/wait_for_tests.rb diff --git a/lib/fog/brightbox/models/compute/server.rb b/lib/fog/brightbox/models/compute/server.rb index 6ff6adb15..2c5177d04 100644 --- a/lib/fog/brightbox/models/compute/server.rb +++ b/lib/fog/brightbox/models/compute/server.rb @@ -188,11 +188,10 @@ module Fog def soft_reboot shutdown # FIXME Using side effect of wait_for's (evaluated block) to detect timeouts - if wait_for(20) { ! ready? } - # Server is now down, start it up again - start - else - # We timed out + begin + wait_for(20) { ! ready? } + start + rescue Fog::Errors::Timeout => e false end end diff --git a/lib/fog/core/errors.rb b/lib/fog/core/errors.rb index 5f4c107ff..aaea7e6f8 100644 --- a/lib/fog/core/errors.rb +++ b/lib/fog/core/errors.rb @@ -17,6 +17,8 @@ module Fog class NotFound < Fog::Errors::Error; end class LoadError < LoadError; end + + class TimeoutError< Fog::Errors::Error; end # @return [String] The error message that will be raised, if credentials cannot be found def self.missing_credentials diff --git a/lib/fog/core/wait_for.rb b/lib/fog/core/wait_for.rb index baf94bd0d..ca43f05ce 100644 --- a/lib/fog/core/wait_for.rb +++ b/lib/fog/core/wait_for.rb @@ -7,7 +7,7 @@ module Fog duration = Time.now - start end if duration > timeout - false + raise Errors::TimeoutError.new("The specified timeout (#{timeout} seconds) was exceeded") else { :duration => duration } end diff --git a/tests/core/wait_for_tests.rb b/tests/core/wait_for_tests.rb new file mode 100644 index 000000000..1259b1c43 --- /dev/null +++ b/tests/core/wait_for_tests.rb @@ -0,0 +1,13 @@ +Shindo.tests('Fog#wait_for', 'core') do + tests("success") do + tests('Fog#wait_for').formats(:duration => Integer) do + Fog.wait_for(1) { true } + end + end + + tests("failure") do + tests('Fog#wait_for').raises(Fog::Errors::TimeoutError) do + Fog.wait_for(2) { false } + end + end +end