Skip to content

Commit f6e248e

Browse files
committed
feat: add is_really_closed feature to non-async postgres lib
1 parent 09497b0 commit f6e248e

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

postgres/src/client.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ impl Client {
422422
self.connection.block_on(self.client.batch_execute(query))
423423
}
424424

425+
/// Check the connection is alive and wait for the confirmation.
426+
pub fn check_connection(&mut self) -> Result<(), Error> {
427+
self.connection.block_on(self.client.check_connection())
428+
}
429+
425430
/// Begins a new database transaction.
426431
///
427432
/// The transaction will roll back by default - use the `commit` method to commit it.

postgres/src/test.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,24 @@ fn check_send() {
508508
is_send::<Statement>();
509509
is_send::<Transaction<'_>>();
510510
}
511+
512+
#[test]
513+
fn is_closed() {
514+
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
515+
assert!(!client.is_closed());
516+
client.check_connection().unwrap();
517+
518+
let row = client.query_one("select pg_backend_pid()", &[]).unwrap();
519+
let pid: i32 = row.get(0);
520+
521+
{
522+
let mut client2 = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
523+
client2
524+
.query("SELECT pg_terminate_backend($1)", &[&pid])
525+
.unwrap();
526+
}
527+
528+
assert!(!client.is_closed());
529+
client.check_connection().unwrap_err();
530+
assert!(client.is_closed());
531+
}

0 commit comments

Comments
 (0)