Hobo.busy added, and tested

This commit is contained in:
John Bender 2010-02-08 22:38:32 -08:00
parent cb89290b62
commit 3ccdaf9182
3 changed files with 70 additions and 0 deletions

View File

@ -8,6 +8,7 @@ require 'logger'
require 'virtualbox'
require 'net/ssh'
require 'ping'
require 'hobo/busy'
require 'hobo/util'
require 'hobo/config'
require 'hobo/env'

32
lib/hobo/busy.rb Normal file
View File

@ -0,0 +1,32 @@
module Hobo
def self.busy?
Busy.busy?
end
def self.busy(&block)
Mutex.new.synchronize do
Busy.busy = true
yield
Busy.busy = false
end
# In the case were an exception is thrown by the wrapped code
# make sure to set busy to sane state and reraise the error
rescue Exception => e
Busy.busy = false
raise
end
class Busy
@@busy = false
class << self
def busy?
@@busy
end
def busy=(val)
@@busy = val
end
end
end
end

37
test/hobo/busy_test.rb Normal file
View File

@ -0,0 +1,37 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class BusyTest < Test::Unit::TestCase
context "during an action in a busy block" do
should "hobo should report as busy" do
Hobo.busy do
# Inside the block Hobo.busy? should be true
assert Hobo.busy?
end
#After the block finishes Hobo.busy? should be false
assert !Hobo.busy?
end
should "set busy to false upon exception and reraise the error" do
assert_raise Exception do
Hobo.busy do
assert Hobo.busy?
raise Exception
end
end
assert !Hobo.busy?
end
should "report busy to the outside world regardless of thread" do
Thread.new do
Hobo.busy do
sleep(10)
end
end
# While the above thread is executing hobo should be busy
assert Hobo.busy?
end
end
end