rust-postgres/tokio-postgres/tests/test/runtime.rs

84 lines
2.3 KiB
Rust
Raw Normal View History

use futures::{join, FutureExt};
2019-11-27 00:32:36 +00:00
use std::time::Duration;
use tokio::time;
2019-03-05 05:26:10 +00:00
use tokio_postgres::error::SqlState;
2019-07-31 04:25:30 +00:00
use tokio_postgres::{Client, NoTls};
2018-12-18 05:25:21 +00:00
2019-07-30 04:36:07 +00:00
async fn connect(s: &str) -> Client {
let (client, connection) = tokio_postgres::connect(s, NoTls).await.unwrap();
2019-07-23 04:27:21 +00:00
let connection = connection.map(|e| e.unwrap());
tokio::spawn(connection);
2018-12-18 05:25:21 +00:00
2019-07-30 04:36:07 +00:00
client
}
async fn smoke_test(s: &str) {
let client = connect(s).await;
2019-07-30 04:36:07 +00:00
2019-07-27 03:11:34 +00:00
let stmt = client.prepare("SELECT $1::INT").await.unwrap();
2019-10-09 00:22:56 +00:00
let rows = client.query(&stmt, &[&1i32]).await.unwrap();
2019-07-27 03:11:34 +00:00
assert_eq!(rows[0].get::<_, i32>(0), 1i32);
2018-12-18 05:25:21 +00:00
}
2019-07-23 03:17:29 +00:00
#[tokio::test]
#[ignore] // FIXME doesn't work with our docker-based tests :(
2019-07-23 03:17:29 +00:00
async fn unix_socket() {
smoke_test("host=/var/run/postgresql port=5433 user=postgres").await;
}
2019-07-23 03:17:29 +00:00
#[tokio::test]
async fn tcp() {
smoke_test("host=localhost port=5433 user=postgres").await;
}
2018-12-18 05:25:21 +00:00
2019-07-23 03:17:29 +00:00
#[tokio::test]
async fn multiple_hosts_one_port() {
smoke_test("host=foobar.invalid,localhost port=5433 user=postgres").await;
}
2018-12-18 05:25:21 +00:00
2019-07-23 03:17:29 +00:00
#[tokio::test]
async fn multiple_hosts_multiple_ports() {
smoke_test("host=foobar.invalid,localhost port=5432,5433 user=postgres").await;
}
2019-07-23 03:17:29 +00:00
#[tokio::test]
async fn wrong_port_count() {
tokio_postgres::connect("host=localhost port=5433,5433 user=postgres", NoTls)
.await
.err()
.unwrap();
2018-12-18 05:25:21 +00:00
}
2019-07-23 03:17:29 +00:00
#[tokio::test]
async fn target_session_attrs_ok() {
smoke_test("host=localhost port=5433 user=postgres target_session_attrs=read-write").await;
}
2019-07-23 03:17:29 +00:00
#[tokio::test]
async fn target_session_attrs_err() {
tokio_postgres::connect(
"host=localhost port=5433 user=postgres target_session_attrs=read-write
options='-c default_transaction_read_only=on'",
NoTls,
2019-07-28 23:02:34 +00:00
)
.await
.err()
.unwrap();
}
2019-07-30 04:36:07 +00:00
#[tokio::test]
async fn cancel_query() {
let client = connect("host=localhost port=5433 user=postgres").await;
let cancel_token = client.cancel_token();
let cancel = cancel_token.cancel_query(NoTls);
2019-11-27 00:32:36 +00:00
let cancel = time::delay_for(Duration::from_millis(100)).then(|()| cancel);
2019-07-30 04:36:07 +00:00
let sleep = client.batch_execute("SELECT pg_sleep(100)");
2019-07-30 04:36:07 +00:00
match join!(sleep, cancel) {
(Err(ref e), Ok(())) if e.code() == Some(&SqlState::QUERY_CANCELED) => {}
t => panic!("unexpected return: {:?}", t),
}
}