Skip to content

Commit 351a2a6

Browse files
committed
allow tests to specify backgroundThreadIntervalMS
1 parent 2bdf4f9 commit 351a2a6

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

src/cmap/options.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use std::{sync::Arc, time::Duration};
2+
#[cfg(test)]
3+
use std::cmp::Ordering;
24

35
use derivative::Derivative;
4-
use serde::Deserialize;
6+
use serde::{Deserialize};
7+
#[cfg(test)]
8+
use serde::de::{Deserializer, Error};
59
use typed_builder::TypedBuilder;
610

711
use crate::{
@@ -40,10 +44,10 @@ pub(crate) struct ConnectionPoolOptions {
4044
#[serde(skip)]
4145
pub(crate) event_handler: Option<Arc<dyn CmapEventHandler>>,
4246

43-
/// How often the background thread performs its maintenance (e.g. ensure minPoolSize).
47+
/// Interval between background thread maintenance runs (e.g. ensure minPoolSize).
4448
#[cfg(test)]
45-
#[serde(skip)]
46-
pub(crate) maintenance_frequency: Option<Duration>,
49+
#[serde(default, rename = "backgroundThreadIntervalMS", deserialize_with = "BackgroundThreadInterval::deserialize_from_i64_millis")]
50+
pub(crate) background_thread_interval: Option<BackgroundThreadInterval>,
4751

4852
/// Connections that have been ready for usage in the pool for longer than `max_idle_time` will
4953
/// not be used.
@@ -101,7 +105,7 @@ impl ConnectionPoolOptions {
101105
credential: options.credential.clone(),
102106
event_handler: options.cmap_event_handler.clone(),
103107
#[cfg(test)]
104-
maintenance_frequency: None,
108+
background_thread_interval: None,
105109
#[cfg(test)]
106110
ready: None,
107111
}
@@ -116,6 +120,31 @@ impl ConnectionPoolOptions {
116120
}
117121
}
118122

123+
#[cfg(test)]
124+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
125+
pub(crate) enum BackgroundThreadInterval {
126+
Never,
127+
Every(Duration),
128+
}
129+
130+
#[cfg(test)]
131+
impl BackgroundThreadInterval {
132+
pub(crate) fn deserialize_from_i64_millis<'de, D>(
133+
deserializer: D,
134+
) -> std::result::Result<Option<Self>, D::Error>
135+
where
136+
D: Deserializer<'de>,
137+
{
138+
let millis = Option::<i64>::deserialize(deserializer)?;
139+
let millis = if let Some(m) = millis { m } else { return Ok(None) };
140+
Ok(Some(match millis.cmp(&0) {
141+
Ordering::Less => BackgroundThreadInterval::Never,
142+
Ordering::Equal => return Err(D::Error::custom("zero is not allowed")),
143+
Ordering::Greater => BackgroundThreadInterval::Every(Duration::from_millis(millis as u64)),
144+
}))
145+
}
146+
}
147+
119148
/// Options used for constructing a `Connection`.
120149
#[derive(Derivative)]
121150
#[derivative(Debug)]

src/cmap/test/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,13 @@ impl Executor {
150150
async fn execute_test(self) {
151151
let mut subscriber = self.state.handler.subscribe();
152152

153-
// CMAP spec requires setting this to 50ms.
154-
let mut options = self.pool_options;
155-
options.maintenance_frequency = Some(Duration::from_millis(50));
156-
157153
let (update_sender, mut update_receiver) = ServerUpdateSender::channel();
158154

159155
let pool = ConnectionPool::new(
160156
CLIENT_OPTIONS.hosts[0].clone(),
161157
Default::default(),
162158
update_sender,
163-
Some(options),
159+
Some(self.pool_options),
164160
);
165161

166162
// Mock a monitoring task responding to errors reported by the pool.

src/cmap/worker.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use super::{
1818
Connection,
1919
DEFAULT_MAX_POOL_SIZE,
2020
};
21+
#[cfg(test)]
22+
use super::options::BackgroundThreadInterval;
2123
use crate::{
2224
error::{Error, ErrorKind, Result},
2325
event::cmap::{
@@ -177,7 +179,11 @@ impl ConnectionPoolWorker {
177179
#[cfg(test)]
178180
let maintenance_frequency = options
179181
.as_ref()
180-
.and_then(|opts| opts.maintenance_frequency)
182+
.and_then(|opts| opts.background_thread_interval)
183+
.map(|i| match i {
184+
BackgroundThreadInterval::Never => Duration::MAX,
185+
BackgroundThreadInterval::Every(d) => d,
186+
})
181187
.unwrap_or(MAINTENACE_FREQUENCY);
182188

183189
#[cfg(not(test))]

0 commit comments

Comments
 (0)