Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add PostgresPoolManager
  • Loading branch information
sfackler committed Aug 6, 2014
1 parent ac7ccf8 commit 6909703
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
target/
Cargo.lock
21 changes: 21 additions & 0 deletions Cargo.toml
@@ -0,0 +1,21 @@
[package]
name = "r2d2_postgres"
version = "0.0.0"
authors = ["Steven Fackler <sfackler@gmail.com>"]

[[lib]]
name = "r2d2_postgres"
path = "src/lib.rs"
test = false

[[test]]
name = "test"
path = "tests/test.rs"

[dependencies.rust-postgres]
git = "https://github.com/sfackler/rust-postgres"
rev = "7d8424418c52e148cba4bcfd89eeb9b5abdf7573"

[dependencies.r2d2]
git = "https://github.com/sfackler/r2d2"
rev = "150f55e4f5cc73a554be72e18c5ed61184673043"
26 changes: 26 additions & 0 deletions src/lib.rs
@@ -0,0 +1,26 @@
extern crate r2d2;
extern crate postgres;

use postgres::{PostgresConnection, PostgresConnectParams, IntoConnectParams, SslMode};
use postgres::error::PostgresConnectError;

pub struct PostgresPoolManager {
params: PostgresConnectParams,
ssl_mode: SslMode,
}

impl PostgresPoolManager {
pub fn new<T: IntoConnectParams>(params: T, ssl_mode: SslMode)
-> Result<PostgresPoolManager, PostgresConnectError> {
Ok(PostgresPoolManager {
params: try!(params.into_connect_params()),
ssl_mode: ssl_mode,
})
}
}

impl r2d2::PoolManager<PostgresConnection, PostgresConnectError> for PostgresPoolManager {
fn connect(&self) -> Result<PostgresConnection, PostgresConnectError> {
PostgresConnection::connect(self.params.clone(), &self.ssl_mode)
}
}
56 changes: 56 additions & 0 deletions tests/test.rs
@@ -0,0 +1,56 @@
extern crate postgres;
extern crate r2d2;
extern crate r2d2_postgres;

use std::default::Default;
use std::sync::{Arc, Future};
use std::comm;

use postgres::NoSsl;
use postgres::error::SocketError;
use r2d2_postgres::PostgresPoolManager;

#[test]
fn test_bad_url_error() {
let manager = PostgresPoolManager::new("postgres://bogushost", NoSsl).unwrap();
let config = Default::default();
match r2d2::Pool::new(config, manager) {
Err(r2d2::ConnectionError(SocketError(_))) => {}
Err(err) => fail!("Unexpected error {}", err),
_ => fail!("Unexpected success")
}
}

#[test]
fn test_basic() {
let manager = PostgresPoolManager::new("postgres://postgres@localhost", NoSsl).unwrap();
let config = r2d2::Config {
initial_size: 2,
..Default::default()
};
let pool = Arc::new(r2d2::Pool::new(config, manager).unwrap());

let (s1, r1) = comm::channel();
let (s2, r2) = comm::channel();

let pool1 = pool.clone();
let mut fut1 = Future::spawn(proc() {
let conn = pool1.get().unwrap();
s1.send(());
r2.recv();
conn.replace();
});

let pool2 = pool.clone();
let mut fut2 = Future::spawn(proc() {
let conn = pool2.get().unwrap();
s2.send(());
r1.recv();
conn.replace();
});

fut1.get();
fut2.get();

pool.get().unwrap().replace();
}

0 comments on commit 6909703

Please sign in to comment.