Skip to content

RUST-911: Add srvServiceName URI option #1235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
update test to align more closely with spec description
  • Loading branch information
jadalilleboe committed Nov 7, 2024
commit 7947fbe15f7204fc2472531e0455565f1e7231eb
6 changes: 5 additions & 1 deletion src/sdam/srv_polling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ impl SrvPollingMonitor {
}

fn rescan_interval(&self) -> Duration {
std::cmp::max(self.rescan_interval, MIN_RESCAN_SRV_INTERVAL)
if cfg!(test) {
self.rescan_interval
} else {
std::cmp::max(self.rescan_interval, MIN_RESCAN_SRV_INTERVAL)
}
}

async fn execute(mut self) {
Expand Down
62 changes: 25 additions & 37 deletions src/sdam/srv_polling/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,26 @@ static DEFAULT_HOSTS: Lazy<Vec<ServerAddress>> = Lazy::new(|| {
});

async fn run_test(new_hosts: Result<Vec<ServerAddress>>, expected_hosts: HashSet<ServerAddress>) {
run_test_srv(None, new_hosts, expected_hosts, None).await
run_test_srv(None, new_hosts, expected_hosts).await
}

async fn run_test_srv(
max_hosts: Option<u32>,
new_hosts: Result<Vec<ServerAddress>>,
expected_hosts: HashSet<ServerAddress>,
srv_service_name: Option<String>,
) {
let actual = run_test_extra(max_hosts, new_hosts, srv_service_name).await;
let actual = run_test_extra(max_hosts, new_hosts).await;
assert_eq!(expected_hosts, actual);
}

async fn run_test_extra(
max_hosts: Option<u32>,
new_hosts: Result<Vec<ServerAddress>>,
srv_service_name: Option<String>,
) -> HashSet<ServerAddress> {
let mut options = ClientOptions::new_srv();
options.hosts.clone_from(&DEFAULT_HOSTS);
options.test_options_mut().disable_monitoring_threads = true;
options.srv_max_hosts = max_hosts;
options.srv_service_name = srv_service_name;
let mut topology = Topology::new(options.clone()).unwrap();
topology.watch().wait_until_initialized().await;
let mut monitor =
Expand Down Expand Up @@ -159,20 +156,8 @@ async fn srv_max_hosts_zero() {
localhost_test_build_10gen(27020),
];

run_test_srv(
None,
Ok(hosts.clone()),
hosts.clone().into_iter().collect(),
None,
)
.await;
run_test_srv(
Some(0),
Ok(hosts.clone()),
hosts.into_iter().collect(),
None,
)
.await;
run_test_srv(None, Ok(hosts.clone()), hosts.clone().into_iter().collect()).await;
run_test_srv(Some(0), Ok(hosts.clone()), hosts.into_iter().collect()).await;
}

// SRV polling with srvMaxHosts MongoClient option: All DNS records are selected (srvMaxHosts >=
Expand All @@ -184,13 +169,7 @@ async fn srv_max_hosts_gt_actual() {
localhost_test_build_10gen(27020),
];

run_test_srv(
Some(2),
Ok(hosts.clone()),
hosts.into_iter().collect(),
None,
)
.await;
run_test_srv(Some(2), Ok(hosts.clone()), hosts.into_iter().collect()).await;
}

// SRV polling with srvMaxHosts MongoClient option: New DNS records are randomly selected
Expand All @@ -203,22 +182,31 @@ async fn srv_max_hosts_random() {
localhost_test_build_10gen(27020),
];

let actual = run_test_extra(Some(2), Ok(hosts), None).await;
let actual = run_test_extra(Some(2), Ok(hosts)).await;
assert_eq!(2, actual.len());
assert!(actual.contains(&localhost_test_build_10gen(27017)));
}

#[tokio::test]
async fn srv_service_name() {
let hosts = vec![
localhost_test_build_10gen(27019),
localhost_test_build_10gen(27020),
let rescan_interval = Duration::from_secs(1);
let new_hosts = vec![
ServerAddress::Tcp {
host: "localhost.test.build.10gen.cc".to_string(),
port: Some(27019),
},
ServerAddress::Tcp {
host: "localhost.test.build.10gen.cc".to_string(),
port: Some(27020),
},
];
run_test_srv(
None,
Ok(hosts.clone()),
hosts.into_iter().collect(),
Some("customname".to_string()),
)
.await;
let uri = "mongodb+srv://test22.test.build.10gen.cc/?srvServiceName=customname";
let mut options = ClientOptions::parse(uri).await.unwrap();
// override the min_ttl to speed up lookup interval
options.original_srv_info.as_mut().unwrap().min_ttl = rescan_interval;
options.test_options_mut().mock_lookup_hosts = Some(make_lookup_hosts(new_hosts.clone()));
let mut topology = Topology::new(options).unwrap();
topology.watch().wait_until_initialized().await;
tokio::time::sleep(rescan_interval * 2).await;
assert_eq!(topology.server_addresses(), new_hosts.into_iter().collect());
}