2019-02-01 04:34:49 +00:00
|
|
|
use futures::{Future, Stream};
|
2019-01-07 02:03:51 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2018-12-18 05:25:21 +00:00
|
|
|
use tokio::runtime::current_thread::Runtime;
|
2019-01-07 02:03:51 +00:00
|
|
|
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
|
|
|
|
2018-12-20 04:18:48 +00:00
|
|
|
fn smoke_test(s: &str) {
|
2018-12-18 05:25:21 +00:00
|
|
|
let mut runtime = Runtime::new().unwrap();
|
2018-12-29 21:28:38 +00:00
|
|
|
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);
|
|
|
|
|
2019-02-01 04:34:49 +00:00
|
|
|
let execute = client.simple_query("SELECT 1").for_each(|_| Ok(()));
|
2018-12-18 05:25:21 +00:00
|
|
|
runtime.block_on(execute).unwrap();
|
|
|
|
}
|
|
|
|
|
2018-12-20 04:18:48 +00:00
|
|
|
#[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() {
|
2018-12-20 04:18:48 +00:00
|
|
|
smoke_test("host=localhost port=5433 user=postgres")
|
|
|
|
}
|
2018-12-18 05:25:21 +00:00
|
|
|
|
2018-12-20 04:18:48 +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
|
|
|
|
2018-12-20 04:18:48 +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();
|
2018-12-29 21:28:38 +00:00
|
|
|
let f = tokio_postgres::connect("host=localhost port=5433,5433 user=postgres", NoTls);
|
2018-12-20 04:18:48 +00:00
|
|
|
runtime.block_on(f).err().unwrap();
|
|
|
|
|
2018-12-29 21:28:38 +00:00
|
|
|
let f = tokio_postgres::connect(
|
|
|
|
"host=localhost,localhost,localhost port=5433,5433 user=postgres",
|
|
|
|
NoTls,
|
|
|
|
);
|
2018-12-20 04:18:48 +00:00
|
|
|
runtime.block_on(f).err().unwrap();
|
2018-12-18 05:25:21 +00:00
|
|
|
}
|
2018-12-30 19:50:15 +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(
|
2018-12-31 04:07:03 +00:00
|
|
|
"host=localhost port=5433 user=postgres target_session_attrs=read-write
|
|
|
|
options='-c default_transaction_read_only=on'",
|
2018-12-30 19:50:15 +00:00
|
|
|
NoTls,
|
|
|
|
);
|
|
|
|
runtime.block_on(f).err().unwrap();
|
|
|
|
}
|
2019-01-07 02:03:51 +00:00
|
|
|
|
|
|
|
#[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
|
2019-02-01 04:34:49 +00:00
|
|
|
.simple_query("SELECT pg_sleep(100)")
|
|
|
|
.for_each(|_| Ok(()))
|
2019-01-07 02:03:51 +00:00
|
|
|
.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();
|
|
|
|
}
|