Permalink
Browse files

Wait for mongd, closes #18.

  • Loading branch information...
lucassus committed Nov 19, 2012
1 parent 4a7942c commit cc6ed556e3cb2b2510c027ae0791828b6501b5c9
Showing with 23 additions and 2 deletions.
  1. +23 −2 spec/support/mongod.rb
View
@@ -19,8 +19,7 @@ def run!
log_file = File.open(File.expand_path("log/test_mongod.log"), "w+")
@pid = Process.spawn(command, out: log_file)
- # TODO wait for the connection (waiting for connections on port xxxx)
- sleep 2
+ wait_until_responsive
yield port if block_given?
end
@@ -34,6 +33,14 @@ def shutdown!
@pid = nil
end
+ # Returns true is mongodb server is ready to use
+ def responsive?
+ Mongo::Connection.new(MongoBrowser.mongodb_host, port)
+ true
+ rescue Mongo::ConnectionFailure
+ false
+ end
+
def running?
not pid.nil?
end
@@ -46,4 +53,18 @@ def find_available_port
ensure
server.close if server
end
+
+ # Uses exponential back-off technique for waiting for the mongodb server
+ def wait_until_responsive
+ wait_time = 0.01
+ timeout = 10
+ start_time = Time.now
+
+ until responsive?
+ raise "Could not start mongd" if Time.now - start_time >= timeout
+
+ sleep wait_time
+ wait_time *= 2
+ end
+ end
end

0 comments on commit cc6ed55

Please sign in to comment.