diff --git a/contrib/ruby/ext/trilogy-ruby/cext.c b/contrib/ruby/ext/trilogy-ruby/cext.c index fea84a99..89f1a552 100644 --- a/contrib/ruby/ext/trilogy-ruby/cext.c +++ b/contrib/ruby/ext/trilogy-ruby/cext.c @@ -913,7 +913,11 @@ static VALUE rb_trilogy_query(VALUE self, VALUE query) static VALUE rb_trilogy_ping(VALUE self) { - struct trilogy_ctx *ctx = get_open_ctx(self); + struct trilogy_ctx *ctx = get_ctx(self); + + if (ctx->conn.socket == NULL) { + return Qfalse; + } int rc = trilogy_ping_send(&ctx->conn); @@ -922,7 +926,7 @@ static VALUE rb_trilogy_ping(VALUE self) } if (rc < 0) { - handle_trilogy_error(ctx, rc, "trilogy_ping_send"); + return Qfalse; } while (1) { @@ -933,12 +937,12 @@ static VALUE rb_trilogy_ping(VALUE self) } if (rc != TRILOGY_AGAIN) { - handle_trilogy_error(ctx, rc, "trilogy_ping_recv"); + return Qfalse; } rc = trilogy_sock_wait_read(ctx->conn.socket); if (rc != TRILOGY_OK) { - handle_trilogy_error(ctx, rc, "trilogy_ping_recv"); + return Qfalse; } } diff --git a/contrib/ruby/test/client_test.rb b/contrib/ruby/test/client_test.rb index e25811cd..79ab2511 100644 --- a/contrib/ruby/test/client_test.rb +++ b/contrib/ruby/test/client_test.rb @@ -80,11 +80,20 @@ def test_trilogy_ping def test_trilogy_ping_after_close_returns_false client = new_tcp_client - assert client.ping + assert_equal client.ping, true client.close - assert_raises Trilogy::ConnectionClosed do - client.ping + assert_equal client.ping, false + ensure + ensure_closed client + end + + def test_trilogy_ping_after_connection_closed_returns_false + client = new_tcp_client(read_timeout: 1) + assert_equal client.ping, true + assert_raises Trilogy::TimeoutError do + client.query("SELECT SLEEP (2)") end + assert_equal client.ping, false ensure ensure_closed client end @@ -571,7 +580,7 @@ def test_cast_exception_during_query_does_not_close_the_connection assert_equal "Invalid date: 1234-00-00 00:00:00", err.message assert_raises_connection_error do - client.ping + client.query("SELECT 1") end end @@ -683,7 +692,7 @@ def test_releases_gvl end assert_raises_connection_error do - client.ping + client.query("SELECT 1") end end @@ -841,7 +850,7 @@ def test_configured_max_packet_above_server refute_match(/TRILOGY_MAX_PACKET_EXCEEDED/, exception.message) assert_raises_connection_error do - client.ping + client.query("SELECT 1") end ensure ensure_closed client