Permalink
Browse files

Create better wrapper for mongd.

  • Loading branch information...
1 parent 8053650 commit a85368fc45369880fe5cbcc404695cf4eb3cb07e @lucassus committed Nov 19, 2012
View
@@ -2,3 +2,4 @@
results.html
pkg
html
+log/*.log
View
No changes.
@@ -1,7 +1,7 @@
require "spec_helper"
describe "Documents list", type: :request do
- let(:connection) { MongoTestServer.connection }
+ let(:connection) { Mongod.instance.connection }
before do
visit "/"
View
@@ -14,16 +14,6 @@
require "capybara/rspec"
require "socket"
-def find_available_port
- server = TCPServer.new("127.0.0.1", 0)
- server.addr[1]
-ensure
- server.close if server
-end
-
-MongoBrowser.mongodb_host = "localhost"
-MongoBrowser.mongodb_port = find_available_port
-
require "capybara/webkit"
Capybara.javascript_driver = :webkit
@@ -38,8 +28,15 @@ def find_available_port
config.include Integration
config.before do
- MongoTestServer.ensure_test_server_is_running
- MongoTestServer.load_fixtures
+ # TODO do it only for request specs
+
+ test_server = Mongod.instance
+ test_server.run! do |port|
+ MongoBrowser.mongodb_host = "localhost"
+ MongoBrowser.mongodb_port = port
+ end
+
+ test_server.load_fixtures!
end
config.after do
@@ -52,5 +49,6 @@ def find_available_port
end
at_exit do
- MongoTestServer.clean_up
+ test_server = Mongod.instance
+ test_server.shutdown!
end
@@ -1,18 +1,43 @@
-module MongoTestServer
- extend self
-
+class Mongod
+ include Singleton
+
MONGODB_DBPATH = "/tmp/mongo_browser/db"
- def ensure_test_server_is_running
- if `lsof -i :#{MongoBrowser.mongodb_port}`.size == 0
- clean_up
+ attr_reader :port
+ attr_reader :pid
- FileUtils.mkdir_p(MONGODB_DBPATH)
- `mongod --port #{MongoBrowser.mongodb_port} --dbpath #{MONGODB_DBPATH} --fork --logpath #{MONGODB_DBPATH}/../db.log`
- end
+ def initialize
+ @port = find_available_port
+ end
+
+ def run!
+ return if running?
+
+ FileUtils.mkdir_p(MONGODB_DBPATH)
+
+ log_file = File.open(File.expand_path("log/test_mongod.log"), "w+")
+ @pid = spawn("mongod --port #{port} --dbpath #{MONGODB_DBPATH} --nojournal", out: log_file)
+
+ # TODO wait for the connection (waiting for connections on port xxxx)
+ sleep 2
+
+ yield port if block_given?
end
- def load_fixtures
+ def shutdown!
+ return unless running?
+
+ Process.kill('HUP', pid)
+ FileUtils.rm_rf("#{MONGODB_DBPATH}/..")
+
+ @pid = nil
+ end
+
+ def running?
+ not pid.nil?
+ end
+
+ def load_fixtures!
# Delete all databases
connection.database_names.each do |db_name|
connection.drop_database(db_name)
@@ -37,14 +62,12 @@ def connection
@connection ||= Mongo::Connection.new(MongoBrowser.mongodb_host, MongoBrowser.mongodb_port)
end
- def clean_up
- out = `ps aux | grep "mongod"`
- out.lines.each do |line|
- if line.include?("mongod") and line.include?("--dbpath #{MONGODB_DBPATH}")
- pid = line.split[1]
- `kill -9 #{pid}`
- end
- end
- FileUtils.rm_rf("#{MONGODB_DBPATH}/..")
+ private
+
+ def find_available_port
+ server = TCPServer.new("127.0.0.1", 0)
+ server.addr[1]
+ ensure
+ server.close if server
end
end

0 comments on commit a85368f

Please sign in to comment.