From e64f91e0131d7fd6c74cfa26d90c4a31672e9a8a Mon Sep 17 00:00:00 2001 From: Mauro Pompilio Date: Thu, 3 Mar 2011 19:22:32 +0000 Subject: [PATCH] Implementation of the 'ping' command. Probably better suited for the 'active?' method. --- lib/mongo/connection.rb | 13 +++++++++++-- test/connection_test.rb | 5 +++++ 2 files changed, 16 insertions(+), 2 deletions(-) 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