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

103 lines
3.0 KiB
Rust
Raw Normal View History

use futures::{Future, Stream};
use std::time::{Duration, Instant};
2018-12-18 05:25:21 +00:00
use tokio::runtime::current_thread::Runtime;
use tokio::timer::Delay;
2019-03-05 05:26:10 +00:00
use tokio_postgres::error::SqlState;
use tokio_postgres::NoTls;
2018-12-18 05:25:21 +00:00
fn smoke_test(s: &str) {
2018-12-18 05:25:21 +00:00
let mut runtime = Runtime::new().unwrap();
let connect = tokio_postgres::connect(s, NoTls);
2018-12-18 05:25:21 +00:00
let (mut client, connection) = runtime.block_on(connect).unwrap();
let connection = connection.map_err(|e| panic!("{}", e));
runtime.spawn(connection);
let execute = client.simple_query("SELECT 1").for_each(|_| Ok(()));
2018-12-18 05:25:21 +00:00
runtime.block_on(execute).unwrap();
}
#[test]
#[ignore] // FIXME doesn't work with our docker-based tests :(
fn unix_socket() {
smoke_test("host=/var/run/postgresql port=5433 user=postgres");
}
2018-12-18 05:25:21 +00:00
#[test]
fn tcp() {
smoke_test("host=localhost port=5433 user=postgres")
}
2018-12-18 05:25:21 +00:00
#[test]
fn multiple_hosts_one_port() {
smoke_test("host=foobar.invalid,localhost port=5433 user=postgres");
}
2018-12-18 05:25:21 +00:00
#[test]
fn multiple_hosts_multiple_ports() {
smoke_test("host=foobar.invalid,localhost port=5432,5433 user=postgres");
}
#[test]
fn wrong_port_count() {
let mut runtime = Runtime::new().unwrap();
let f = tokio_postgres::connect("host=localhost port=5433,5433 user=postgres", NoTls);
runtime.block_on(f).err().unwrap();
let f = tokio_postgres::connect(
"host=localhost,localhost,localhost port=5433,5433 user=postgres",
NoTls,
);
runtime.block_on(f).err().unwrap();
2018-12-18 05:25:21 +00:00
}
#[test]
fn target_session_attrs_ok() {
let mut runtime = Runtime::new().unwrap();
let f = tokio_postgres::connect(
"host=localhost port=5433 user=postgres target_session_attrs=read-write",
NoTls,
);
runtime.block_on(f).unwrap();
}
#[test]
fn target_session_attrs_err() {
let mut runtime = Runtime::new().unwrap();
let f = tokio_postgres::connect(
"host=localhost port=5433 user=postgres target_session_attrs=read-write
options='-c default_transaction_read_only=on'",
NoTls,
);
runtime.block_on(f).err().unwrap();
}
#[test]
fn cancel_query() {
let mut runtime = Runtime::new().unwrap();
let connect = tokio_postgres::connect("host=localhost port=5433 user=postgres", NoTls);
let (mut client, connection) = runtime.block_on(connect).unwrap();
let connection = connection.map_err(|e| panic!("{}", e));
runtime.spawn(connection);
let sleep = client
.simple_query("SELECT pg_sleep(100)")
.for_each(|_| Ok(()))
.then(|r| match r {
Ok(_) => panic!("unexpected success"),
Err(ref e) if e.code() == Some(&SqlState::QUERY_CANCELED) => Ok::<(), ()>(()),
Err(e) => panic!("unexpected error {}", e),
});
let cancel = Delay::new(Instant::now() + Duration::from_millis(100))
.then(|r| {
r.unwrap();
client.cancel_query(NoTls)
})
.then(|r| {
r.unwrap();
Ok::<(), ()>(())
});
let ((), ()) = runtime.block_on(sleep.join(cancel)).unwrap();
}