Permalink
Browse files

cleaned up attach xvfb code

  • Loading branch information...
Subhash Chandra
Subhash Chandra committed Mar 31, 2012
1 parent 7cde9f7 commit 257e1b5bd3a0f7f82ba6a136fa76f2360a9fae30
Showing with 11 additions and 22 deletions.
  1. +11 −22 lib/headless.rb
View
@@ -47,6 +47,9 @@ class Headless
class Exception < RuntimeError
end
+ class DisplayReuseError < RuntimeError
+ end
+
# The display number
attr_reader :display
@@ -120,30 +123,16 @@ def take_screenshot(file_path)
def attach_xvfb
# TODO this loop isn't elegant enough
- success = false
- while !success && @display<10000
- begin
- if !xvfb_running?
- launch_xvfb
- success=true
- else
- success = @reuse_display
- end
- rescue Errno::EPERM
- # No permission to read pid file
- success = false
- end
-
- # TODO this is crufty
- if @autopick_display
- @display += 1 unless success
+ begin
+ if xvfb_running?
+ raise DisplayReuseError unless @reuse_display
else
- break
+ launch_xvfb
end
- end
-
- unless success
- raise Headless::Exception.new("Display :#{display} is already taken and reuse=false")
+ rescue Errno::EPERM, DisplayReuseError
+ # No permission to read pid file
+ raise Headless::Exception.new("Display :#{display} is already taken and reuse=false") unless @autopick_display
+ retry if (@display += 1) < 10000
end
end

0 comments on commit 257e1b5

Please sign in to comment.