More robust pool test

This commit is contained in:
Steven Fackler
2013-09-16 23:38:27 -07:00
parent 65aabf5147
commit 3c7c5dce51
2 changed files with 40 additions and 5 deletions

View File

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

View File

@@ -1,14 +1,47 @@
extern mod extra;
extern mod postgres;
use postgres::pool;
use postgres::pool::{PostgresConnectionPool};
use std::cell::Cell;
use extra::comm::DuplexStream;
use extra::future;
use postgres::pool::{PostgresConnectionPool, PostgresConnectionPoolConfig};
#[test]
// Make sure we can take both connections at once and can still get one after
fn test_pool() {
let config = PostgresConnectionPoolConfig {
initial_size: 2,
min_size: 2,
max_size: 2
};
let mut pool = PostgresConnectionPool::new("postgres://postgres@localhost",
pool::DEFAULT_CONFIG).unwrap();
config).unwrap();
do pool.with_connection |_conn| {
let (stream1, stream2) = DuplexStream::<(), ()>();
}
let pool1 = Cell::new(pool.clone());
let mut fut1 = do future::spawn {
let mut pool = pool1.take();
do pool.with_connection |_conn| {
stream1.send(());
stream1.recv();
}
};
let pool2 = Cell::new(pool.clone());
let mut fut2 = do future::spawn {
let mut pool = pool2.take();
do pool.with_connection |_conn| {
stream2.send(());
stream2.recv();
}
};
fut1.get();
fut2.get();
do pool.with_connection |_conn| { }
}