Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions contrib/ruby/ext/trilogy-ruby/cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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) {
Expand All @@ -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;
}
}

Expand Down
21 changes: 15 additions & 6 deletions contrib/ruby/test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -683,7 +692,7 @@ def test_releases_gvl
end

assert_raises_connection_error do
client.ping
client.query("SELECT 1")
end
end

Expand Down Expand Up @@ -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
Expand Down