Skip to content

Commit 3c7c5dc

Browse files
committed
More robust pool test
1 parent 65aabf5 commit 3c7c5dc

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

src/pool/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl InnerConnectionPool {
4646
}
4747

4848
// Should be a newtype, but blocked by mozilla/rust#9155
49+
#[deriving(Clone)]
4950
pub struct PostgresConnectionPool {
5051
pool: MutexArc<InnerConnectionPool>
5152
}
@@ -77,6 +78,7 @@ impl PostgresConnectionPool {
7778
pub fn try_with_connection<T>(&mut self,
7879
blk: &fn(&PostgresConnection) -> T)
7980
-> Result<T, PostgresConnectError> {
81+
// Can't use access_cond here since PostgresConnection uses Cell
8082
let conn = unsafe {
8183
do self.pool.unsafe_access_cond |pool, cond| {
8284
while pool.pool.is_empty() {

src/pool/test.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,47 @@
1+
extern mod extra;
12
extern mod postgres;
23

3-
use postgres::pool;
4-
use postgres::pool::{PostgresConnectionPool};
4+
use std::cell::Cell;
5+
use extra::comm::DuplexStream;
6+
use extra::future;
7+
8+
use postgres::pool::{PostgresConnectionPool, PostgresConnectionPoolConfig};
59

610
#[test]
11+
// Make sure we can take both connections at once and can still get one after
712
fn test_pool() {
13+
let config = PostgresConnectionPoolConfig {
14+
initial_size: 2,
15+
min_size: 2,
16+
max_size: 2
17+
};
818
let mut pool = PostgresConnectionPool::new("postgres://postgres@localhost",
9-
pool::DEFAULT_CONFIG).unwrap();
19+
config).unwrap();
20+
21+
let (stream1, stream2) = DuplexStream::<(), ()>();
22+
23+
let pool1 = Cell::new(pool.clone());
24+
let mut fut1 = do future::spawn {
25+
let mut pool = pool1.take();
26+
27+
do pool.with_connection |_conn| {
28+
stream1.send(());
29+
stream1.recv();
30+
}
31+
};
32+
33+
let pool2 = Cell::new(pool.clone());
34+
let mut fut2 = do future::spawn {
35+
let mut pool = pool2.take();
36+
37+
do pool.with_connection |_conn| {
38+
stream2.send(());
39+
stream2.recv();
40+
}
41+
};
1042

11-
do pool.with_connection |_conn| {
43+
fut1.get();
44+
fut2.get();
1245

13-
}
46+
do pool.with_connection |_conn| { }
1447
}

0 commit comments

Comments
 (0)