diff --git a/src/pool.rs b/src/pool.rs index e8c133e..36f8e43 100644 --- a/src/pool.rs +++ b/src/pool.rs @@ -78,9 +78,16 @@ impl PoolBuilder { /// Specify the number of sqlite connections to open as part of the pool. /// - /// Defaults to the number of logical CPUs of the current system. + /// Defaults to the number of logical CPUs of the current system. Values + /// less than `1` are clamped to `1`. + /// + /// ``` + /// use async_sqlite::PoolBuilder; + /// + /// let builder = PoolBuilder::new().num_conns(2); + /// ``` pub fn num_conns(mut self, num_conns: usize) -> Self { - self.num_conns = Some(num_conns); + self.num_conns = Some(num_conns.max(1)); self } diff --git a/tests/tests.rs b/tests/tests.rs index f96195f..0027220 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -84,6 +84,7 @@ async_test!(test_journal_mode); async_test!(test_concurrency); async_test!(test_pool); async_test!(test_pool_conn_for_each); +async_test!(test_pool_num_conns_zero_clamps); async fn test_journal_mode() { let tmp_dir = tempfile::tempdir().unwrap(); @@ -227,3 +228,15 @@ async fn test_pool_conn_for_each() { // cleanup pool.close().await.expect("closing client conn"); } + +async fn test_pool_num_conns_zero_clamps() { + let tmp_dir = tempfile::tempdir().unwrap(); + let pool = PoolBuilder::new() + .path(tmp_dir.path().join("clamp.db")) + .num_conns(0) + .open() + .await + .expect("pool unable to be opened"); + let results = pool.conn_for_each(|_| Ok(())).await; + assert_eq!(results.len(), 1); +}