Skip to content

Commit 97e7b66

Browse files
committed
chore: update integration test code
Signed-off-by: Xin Liu <[email protected]>
1 parent 1d60c12 commit 97e7b66

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

tests/integration.rs

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ fn setup_test_server() {
5252
// Start the server in background
5353
let port = get_test_port();
5454
println!("🔧 Starting server on port {}", port);
55-
let server = Command::new("./target/release/ss-proxy")
55+
56+
// Build server command with environment variables
57+
let mut server_cmd = Command::new("./target/release/ss-proxy");
58+
server_cmd
5659
.args(&["--port", &port.to_string(), "--log-level", "debug"])
57-
.spawn()
58-
.expect("Failed to start server");
60+
.env("TEST_PORT", port.to_string()); // Ensure child process knows the port
61+
62+
let server = server_cmd.spawn().expect("Failed to start server");
5963

6064
SERVER_PROCESS
6165
.set(Mutex::new(Some(server)))
@@ -69,17 +73,11 @@ fn setup_test_server() {
6973
}
7074

7175
/// Cleanup function (called when tests complete)
76+
/// Note: The server will run for all tests and will be cleaned up when the test process exits
7277
impl Drop for ServerGuard {
7378
fn drop(&mut self) {
74-
if let Some(mutex) = SERVER_PROCESS.get() {
75-
if let Ok(mut guard) = mutex.lock() {
76-
if let Some(mut process) = guard.take() {
77-
println!("🧹 Cleaning up: stopping server...");
78-
let _ = process.kill();
79-
let _ = process.wait();
80-
}
81-
}
82-
}
79+
// Don't kill the server on each test - it should persist across all tests
80+
// The server process will be cleaned up when the test process exits
8381
}
8482
}
8583

@@ -240,10 +238,13 @@ async fn test_websocket_echo() {
240238

241239
let (mut write, mut read) = ws_stream.split();
242240

241+
// Skip the initial greeting message from echo.websocket.org
242+
let _ = tokio::time::timeout(Duration::from_secs(2), read.next()).await;
243+
243244
// Send a text message
244245
let test_message = "Hello WebSocket!";
245246
write
246-
.send(Message::Text(test_message.to_string()))
247+
.send(Message::Text(test_message.to_string().into()))
247248
.await
248249
.expect("Failed to send message");
249250

@@ -256,7 +257,7 @@ async fn test_websocket_echo() {
256257

257258
match received {
258259
Message::Text(text) => {
259-
assert_eq!(text, test_message);
260+
assert_eq!(text.to_string(), test_message);
260261
}
261262
_ => panic!("Expected text message, got: {:?}", received),
262263
}
@@ -277,10 +278,13 @@ async fn test_websocket_binary_message() {
277278

278279
let (mut write, mut read) = ws_stream.split();
279280

281+
// Skip the initial greeting message from echo.websocket.org
282+
let _ = tokio::time::timeout(Duration::from_secs(2), read.next()).await;
283+
280284
// Send binary data
281285
let test_data = vec![1u8, 2, 3, 4, 5];
282286
write
283-
.send(Message::Binary(test_data.clone()))
287+
.send(Message::Binary(test_data.clone().into()))
284288
.await
285289
.expect("Failed to send binary message");
286290

@@ -293,7 +297,7 @@ async fn test_websocket_binary_message() {
293297

294298
match received {
295299
Message::Binary(data) => {
296-
assert_eq!(data, test_data);
300+
assert_eq!(data.to_vec(), test_data);
297301
}
298302
_ => panic!("Expected binary message, got: {:?}", received),
299303
}
@@ -330,26 +334,38 @@ async fn test_websocket_multiple_messages() {
330334

331335
let (mut write, mut read) = ws_stream.split();
332336

337+
// Skip the initial greeting message from echo.websocket.org
338+
let _ = tokio::time::timeout(Duration::from_secs(2), read.next()).await;
339+
333340
// Send multiple messages
334341
let messages = vec!["Message 1", "Message 2", "Message 3"];
335342

336343
for msg in &messages {
337344
write
338-
.send(Message::Text(msg.to_string()))
345+
.send(Message::Text(msg.to_string().into()))
339346
.await
340347
.expect("Failed to send message");
341348

342-
let received = tokio::time::timeout(Duration::from_secs(5), read.next())
343-
.await
344-
.expect("Timeout waiting for message")
345-
.expect("No message received")
346-
.expect("Error receiving message");
347-
348-
match received {
349-
Message::Text(text) => {
350-
assert_eq!(&text, msg);
349+
// Receive messages, skipping any greeting messages
350+
loop {
351+
let received = tokio::time::timeout(Duration::from_secs(5), read.next())
352+
.await
353+
.expect("Timeout waiting for message")
354+
.expect("No message received")
355+
.expect("Error receiving message");
356+
357+
match received {
358+
Message::Text(text) => {
359+
let text_str = text.to_string();
360+
// Skip greeting messages from echo.websocket.org
361+
if text_str.starts_with("Request served by") {
362+
continue;
363+
}
364+
assert_eq!(&text_str, msg);
365+
break;
366+
}
367+
_ => panic!("Expected text message"),
351368
}
352-
_ => panic!("Expected text message"),
353369
}
354370
}
355371
}

0 commit comments

Comments
 (0)