Skip to content

Commit ccbf047

Browse files
authored
fix cancellation test on windows by auto assigning port (eclipse-zenoh#2419)
* automatically assign empty port * fixed bulld * locator-based port assign * unused import removed
1 parent df77d0f commit ccbf047

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

zenoh/tests/cancellation.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,31 @@ use zenoh_core::ztimeout;
2222

2323
const TIMEOUT: Duration = Duration::from_secs(60);
2424

25-
async fn create_peer_client_pair(locator: &str) -> (Session, Session) {
25+
async fn create_peer_client_pair() -> (Session, Session) {
26+
// Create a session that listens on an auto assigned TCP port
2627
let config1 = {
2728
let mut config = zenoh::Config::default();
2829
config.scouting.multicast.set_enabled(Some(false)).unwrap();
2930
config
3031
.listen
3132
.endpoints
32-
.set(vec![locator.parse().unwrap()])
33+
.set(vec!["tcp/127.0.0.1:0".parse().unwrap()])
3334
.unwrap();
3435
config
3536
};
37+
let session1 = zenoh::open(config1).await.unwrap();
38+
39+
// Extract the actual TCP endpoint that session1 is listening on
40+
let locator = session1
41+
.info()
42+
.locators()
43+
.await
44+
.into_iter()
45+
.find(|l| l.protocol().as_str() == "tcp")
46+
.expect("Expected at least one TCP locator")
47+
.to_string();
48+
49+
// Create a session that connects to the first session
3650
let mut config2 = zenoh::Config::default();
3751
config2.set_mode(Some(WhatAmI::Client)).unwrap();
3852
config2.scouting.multicast.set_enabled(Some(false)).unwrap();
@@ -41,15 +55,14 @@ async fn create_peer_client_pair(locator: &str) -> (Session, Session) {
4155
.set_endpoints(ModeDependentValue::Unique(vec![locator.parse().unwrap()]))
4256
.unwrap();
4357

44-
let session1 = zenoh::open(config1).await.unwrap();
4558
let session2 = zenoh::open(config2).await.unwrap();
4659
(session1, session2)
4760
}
4861

4962
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
5063
async fn test_cancellation_get() {
5164
zenoh::init_log_from_env_or("error");
52-
let (session1, session2) = ztimeout!(create_peer_client_pair("tcp/127.0.0.1:50001"));
65+
let (session1, session2) = ztimeout!(create_peer_client_pair());
5366
let queryable = ztimeout!(session1.declare_queryable("test/query_cancellation")).unwrap();
5467

5568
tokio::time::sleep(Duration::from_secs(1)).await;
@@ -112,7 +125,7 @@ async fn test_cancellation_get() {
112125
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
113126
async fn test_cancellation_liveliness_get() {
114127
zenoh::init_log_from_env_or("error");
115-
let (session1, session2) = ztimeout!(create_peer_client_pair("tcp/127.0.0.1:50002"));
128+
let (session1, session2) = ztimeout!(create_peer_client_pair());
116129
let _token = ztimeout!(session1
117130
.liveliness()
118131
.declare_token("test/liveliness_query_cancellation"))
@@ -159,7 +172,7 @@ async fn test_cancellation_liveliness_get() {
159172
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
160173
async fn test_cancellation_querier_get() {
161174
zenoh::init_log_from_env_or("error");
162-
let (session1, session2) = ztimeout!(create_peer_client_pair("tcp/127.0.0.1:50003"));
175+
let (session1, session2) = ztimeout!(create_peer_client_pair());
163176
let queryable = ztimeout!(session1.declare_queryable("test/querier_cancellation")).unwrap();
164177

165178
let querier = ztimeout!(session2.declare_querier("test/querier_cancellation")).unwrap();
@@ -221,7 +234,7 @@ async fn test_cancellation_querier_get() {
221234
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
222235
async fn test_cancellation_does_not_prevent_session_from_close() {
223236
zenoh::init_log_from_env_or("error");
224-
let (session1, session2) = ztimeout!(create_peer_client_pair("tcp/127.0.0.1:50004"));
237+
let (session1, session2) = ztimeout!(create_peer_client_pair());
225238
let cancellation_token = zenoh::cancellation::CancellationToken::default();
226239

227240
let ke = "test/query_cancellation_does_not_prevent_session_from_close";

0 commit comments

Comments
 (0)