Skip to content

Commit 5067ab1

Browse files
authored
Fix api::start_stop::test_start_stop test (#136)
1 parent b4c57a0 commit 5067ab1

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

polybase/tests/api/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ impl PortPool {
103103
#[derive(Debug, Default)]
104104
struct ServerConfig {
105105
whitelist: Option<Vec<String>>,
106+
keep_port_after_drop: bool,
106107
}
107108

108109
#[derive(Debug)]
@@ -111,14 +112,17 @@ struct Server {
111112
// Keep the root dir alive so that polybase can use it
112113
_root_dir: tempfile::TempDir,
113114
api_port: u16,
115+
keep_port_after_drop: bool,
114116
client: reqwest::Client,
115117
base_url: reqwest::Url,
116118
}
117119

118120
impl Drop for Server {
119121
fn drop(&mut self) {
120122
self.process.kill().expect("Failed to stop polybase");
121-
API_PORT_POOL.lock().unwrap().release(self.api_port);
123+
if !self.keep_port_after_drop {
124+
API_PORT_POOL.lock().unwrap().release(self.api_port);
125+
}
122126
}
123127
}
124128

@@ -130,8 +134,10 @@ impl Server {
130134

131135
let mut command = Command::new(find_binary());
132136

133-
if let Some(whitelist) = config.and_then(|c| c.whitelist) {
134-
command.arg("--whitelist").arg(whitelist.join(","));
137+
if let Some(ref config) = config {
138+
if let Some(ref whitelist) = config.whitelist {
139+
command.arg("--whitelist").arg(whitelist.join(","));
140+
}
135141
}
136142

137143
command.arg("--root-dir").arg(root_dir.path());
@@ -155,6 +161,7 @@ impl Server {
155161
_root_dir: root_dir,
156162
client: reqwest::Client::new(),
157163
base_url: format!("http://localhost:{api_port}").parse().unwrap(),
164+
keep_port_after_drop: config.map(|c| c.keep_port_after_drop).unwrap_or(false),
158165
api_port,
159166
})
160167
}

polybase/tests/api/start_stop.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1+
use crate::api::ServerConfig;
2+
13
#[tokio::test]
24
async fn test_start_stop() {
35
let api_port;
46
{
5-
let server = super::Server::setup_and_wait(None).await;
7+
let server = super::Server::setup_and_wait(Some(ServerConfig {
8+
keep_port_after_drop: true,
9+
..Default::default()
10+
}))
11+
.await;
612
api_port = server.api_port;
713
}
814

9-
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
15+
for i in 0..10 {
16+
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
1017

11-
reqwest::get(format!("http://localhost:{api_port}/v0/health"))
12-
.await
13-
.expect_err("Server should be stopped");
18+
if reqwest::get(format!("http://localhost:{api_port}/v0/health"))
19+
.await
20+
.is_ok()
21+
{
22+
if i == 9 {
23+
panic!("Server should be stopped");
24+
}
25+
} else {
26+
break;
27+
}
28+
}
1429
}

polybase/tests/api/whitelist.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ collection Account {
2828
signature
2929
});
3030

31-
let server = Server::setup_and_wait(Some(ServerConfig { whitelist })).await;
31+
let server = Server::setup_and_wait(Some(ServerConfig {
32+
whitelist,
33+
..Default::default()
34+
}))
35+
.await;
3236

3337
let res = server
3438
.create_collection::<Account>("test/Account", schema, Some(&signer))
@@ -62,7 +66,11 @@ collection Account {
6266
signature
6367
});
6468

65-
let server = Server::setup_and_wait(Some(ServerConfig { whitelist })).await;
69+
let server = Server::setup_and_wait(Some(ServerConfig {
70+
whitelist,
71+
..Default::default()
72+
}))
73+
.await;
6674

6775
let res = server
6876
.create_collection::<Account>("test/Account", schema, Some(&signer))
@@ -95,7 +103,11 @@ collection Account {
95103
let public_key = indexer::PublicKey::from_secp256k1_key(&public_key).unwrap();
96104
let whitelist = Some(vec![public_key.to_hex().unwrap()]);
97105

98-
let server = Server::setup_and_wait(Some(ServerConfig { whitelist })).await;
106+
let server = Server::setup_and_wait(Some(ServerConfig {
107+
whitelist,
108+
..Default::default()
109+
}))
110+
.await;
99111

100112
let res = server
101113
.create_collection::<Account>("test/Account", schema, None)

0 commit comments

Comments
 (0)