extern crate r2d2; extern crate r2d2_duckdb; extern crate duckdb; extern crate tempdir; use std::sync::mpsc; use std::thread; use r2d2::ManageConnection; use tempdir::TempDir; use r2d2_duckdb::DuckDBConnectionManager; use duckdb::params; #[test] fn test_basic() { let _ = std::fs::remove_file("file.db"); let manager = DuckDBConnectionManager::file("file.db"); let pool = r2d2::Pool::builder().max_size(2).build(manager).unwrap(); let (s1, r1) = mpsc::channel(); let (s2, r2) = mpsc::channel(); let pool1 = pool.clone(); let t1 = thread::spawn(move || { let conn = pool1.get().unwrap(); s1.send(()).unwrap(); r2.recv().unwrap(); drop(conn); }); let pool2 = pool.clone(); let t2 = thread::spawn(move || { let conn = pool2.get().unwrap(); s2.send(()).unwrap(); r1.recv().unwrap(); drop(conn); }); t1.join().unwrap(); t2.join().unwrap(); pool.get().unwrap(); } #[test] fn test_file() { let _ = std::fs::remove_file("file.db"); let manager = DuckDBConnectionManager::file("file.db"); let pool = r2d2::Pool::builder().max_size(2).build(manager).unwrap(); let (s1, r1) = mpsc::channel(); let (s2, r2) = mpsc::channel(); let pool1 = pool.clone(); let t1 = thread::spawn(move || { let _conn = pool1.get().unwrap(); s1.send(()).unwrap(); r2.recv().unwrap(); }); let pool2 = pool.clone(); let t2 = thread::spawn(move || { let conn = pool2.get().unwrap(); s2.send(()).unwrap(); r1.recv().unwrap(); drop(conn); }); t1.join().unwrap(); t2.join().unwrap(); pool.get().unwrap(); } #[test] fn test_non_existent_file() { let manager = DuckDBConnectionManager::file("/tmp/file.db"); let pool = r2d2::Pool::builder().max_size(2).build(manager).unwrap(); let (s1, r1) = mpsc::channel(); let (s2, r2) = mpsc::channel(); let pool1 = pool.clone(); let t1 = thread::spawn(move || { let conn = pool1.get().unwrap(); s1.send(()).unwrap(); let _result = conn.execute("CREATE TABLE users(id int, name text, email text)", params![]).unwrap(); r2.recv().unwrap(); }); let pool2 = pool.clone(); let t2 = thread::spawn(move || { let conn = pool2.get().unwrap(); s2.send(()).unwrap(); r1.recv().unwrap(); drop(conn); }); t1.join().unwrap(); t2.join().unwrap(); pool.get().unwrap(); } #[test] fn test_is_valid() { let _ = std::fs::remove_file("file.db"); let manager = DuckDBConnectionManager::file("file.db"); let pool = r2d2::Pool::builder() .max_size(1) .test_on_check_out(true) .build(manager) .unwrap(); pool.get().unwrap(); } #[test] fn test_error_handling() { //! We specify a directory as a database. This is bound to fail. let dir = TempDir::new("r2d2-duckdb").expect("Could not create temporary directory"); let dirpath = dir.path().to_str().unwrap(); let manager = DuckDBConnectionManager::file(dirpath); assert!(manager.connect().is_err()); }