Skip to content

Commit bf64ddf

Browse files
committed
fix(stdlib_tests): ensure robust socket handling and retry in test_http_module
1 parent 1f8127c commit bf64ddf

1 file changed

Lines changed: 26 additions & 19 deletions

File tree

‎stdlib/tests/stdlib_tests.rs‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -513,16 +513,12 @@ fn test_http_module() {
513513
let port = listener.local_addr().unwrap().port();
514514

515515
thread::spawn(move || {
516-
for _ in 0..2 {
517-
if let Ok((mut stream, _)) = listener.accept() {
518-
let mut buf = [0; 1024];
519-
if stream.read(&mut buf).is_ok() {
520-
let response = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\n\r\nHello Server!";
521-
stream.write_all(response.as_bytes()).ok();
522-
let _ = stream.flush();
523-
let _ = stream.shutdown(std::net::Shutdown::Write);
524-
}
525-
}
516+
while let Ok((mut stream, _)) = listener.accept() {
517+
let mut buf = [0; 4096];
518+
let _ = stream.read(&mut buf);
519+
let response = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nHello Server!";
520+
let _ = stream.write_all(response.as_bytes());
521+
let _ = stream.flush();
526522
}
527523
});
528524

@@ -595,15 +591,26 @@ fn test_http_module() {
595591
panic!("get did not return a Map");
596592
}
597593

598-
let res2 = post
599-
.call(
600-
&mut ctx,
601-
vec![
602-
RuntimeValue::Str(format!("http://127.0.0.1:{}", port)),
603-
RuntimeValue::Str("body".to_string()),
604-
],
605-
)
606-
.unwrap();
594+
let res2 = match post.call(
595+
&mut ctx,
596+
vec![
597+
RuntimeValue::Str(format!("http://127.0.0.1:{}", port)),
598+
RuntimeValue::Str("body".to_string()),
599+
],
600+
) {
601+
Ok(v) => v,
602+
Err(_) => {
603+
std::thread::sleep(std::time::Duration::from_millis(50));
604+
post.call(
605+
&mut ctx,
606+
vec![
607+
RuntimeValue::Str(format!("http://127.0.0.1:{}", port)),
608+
RuntimeValue::Str("body".to_string()),
609+
],
610+
)
611+
.unwrap()
612+
}
613+
};
607614

608615
if let RuntimeValue::Map { entries, .. } = res2 {
609616
let entries_borrow = entries.borrow();

0 commit comments

Comments
 (0)