forked from sfackler/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.rs
83 lines (68 loc) · 2.25 KB
/
runtime.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use futures_util::{join, FutureExt};
use std::time::Duration;
use tokio::time;
use tokio_postgres::error::SqlState;
use tokio_postgres::{Client, NoTls};
async fn connect(s: &str) -> Client {
let (client, connection) = tokio_postgres::connect(s, NoTls).await.unwrap();
let connection = connection.map(|e| e.unwrap());
tokio::spawn(connection);
client
}
async fn smoke_test(s: &str) {
let client = connect(s).await;
let stmt = client.prepare("SELECT $1::INT").await.unwrap();
let rows = client.query(&stmt, &[&1i32]).await.unwrap();
assert_eq!(rows[0].get::<_, i32>(0), 1i32);
}
#[tokio::test]
#[ignore] // FIXME doesn't work with our docker-based tests :(
async fn unix_socket() {
smoke_test("host=/var/run/postgresql port=5433 user=postgres").await;
}
#[tokio::test]
async fn tcp() {
smoke_test("host=localhost port=5433 user=postgres").await;
}
#[tokio::test]
async fn multiple_hosts_one_port() {
smoke_test("host=foobar.invalid,localhost port=5433 user=postgres").await;
}
#[tokio::test]
async fn multiple_hosts_multiple_ports() {
smoke_test("host=foobar.invalid,localhost port=5432,5433 user=postgres").await;
}
#[tokio::test]
async fn wrong_port_count() {
tokio_postgres::connect("host=localhost port=5433,5433 user=postgres", NoTls)
.await
.err()
.unwrap();
}
#[tokio::test]
async fn target_session_attrs_ok() {
smoke_test("host=localhost port=5433 user=postgres target_session_attrs=read-write").await;
}
#[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,
)
.await
.err()
.unwrap();
}
#[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);
let cancel = time::sleep(Duration::from_millis(100)).then(|()| cancel);
let sleep = client.batch_execute("SELECT pg_sleep(100)");
match join!(sleep, cancel) {
(Err(ref e), Ok(())) if e.code() == Some(&SqlState::QUERY_CANCELED) => {}
t => panic!("unexpected return: {:?}", t),
}
}