diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 11aee30..5615252 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -329,13 +329,22 @@ module Mongo self["admin"].command(oh) end - # Get the build information for the current connection. + # Checks if a server is alive. This command will return immediately + # even if the server is in a lock. + # + # @return [Hash] + def ping + self["admin"].command({:ping => 1}) + end + + # Get the build information for the current connection. # # @return [Hash] def server_info self["admin"].command({:buildinfo => 1}) end + # Get the build version of the current server. # # @return [Mongo::ServerVersion] @@ -491,7 +500,7 @@ module Mongo def active? return false unless connected? - server_info + ping true rescue ConnectionFailure diff --git a/test/connection_test.rb b/test/connection_test.rb index 9b6dc0f..37abbfd 100644 --- a/test/connection_test.rb +++ b/test/connection_test.rb @@ -28,6 +28,11 @@ class TestConnection < Test::Unit::TestCase assert Mongo::Support.ok?(server_info) end + def test_ping + ping = @conn.ping + assert ping['ok'] + end + def test_connection_uri con = Connection.from_uri("mongodb://#{host_port}") assert_equal mongo_host, con.primary_pool.host