Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 28 additions & 4 deletions stdlib/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,20 @@ impl StdlibRegistry {
return Ok(RuntimeValue::Str("Server already running".to_string()));
}
SERVER_RUNNING.store(true, Ordering::SeqCst);
let server =
Mutex::new(tiny_http::Server::http(format!("0.0.0.0:{}", port)).unwrap());
let server_result = tiny_http::Server::http(format!("0.0.0.0:{}", port));
let server = match server_result {
Ok(s) => Mutex::new(s),
Err(e) => {
SERVER_RUNNING.store(false, Ordering::SeqCst);
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
format!("Failed to bind server to port {}: {}", port, e),
),
None,
None,
));
}
};
thread::spawn(move || {
while SERVER_RUNNING.load(Ordering::SeqCst) {
let page = PAGE_CONTENT.lock().unwrap().clone();
Expand Down Expand Up @@ -462,8 +474,20 @@ impl StdlibRegistry {
return Ok(RuntimeValue::Str("Server already running".to_string()));
}
SERVER_RUNNING.store(true, Ordering::SeqCst);
let server =
Mutex::new(tiny_http::Server::http(format!("0.0.0.0:{}", port)).unwrap());
let server_result = tiny_http::Server::http(format!("0.0.0.0:{}", port));
let server = match server_result {
Ok(s) => Mutex::new(s),
Err(e) => {
SERVER_RUNNING.store(false, Ordering::SeqCst);
return Err(RuntimeError::new(
techscript_runtime::error::RuntimeErrorKind::InvalidOperation(
format!("Failed to bind server to port {}: {}", port, e),
),
None,
None,
));
}
};
thread::spawn(move || {
while SERVER_RUNNING.load(Ordering::SeqCst) {
let page = PAGE_CONTENT.lock().unwrap().clone();
Expand Down
50 changes: 15 additions & 35 deletions stdlib/tests/stdlib_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ fn test_regex_operations() {
#[test]
fn test_web_module() {
use std::net::TcpListener;
use std::panic;


let registry = StdlibRegistry::new();
let web = registry.get_module("std.web").unwrap();
Expand All @@ -433,50 +433,30 @@ fn test_web_module() {
let listener = TcpListener::bind("0.0.0.0:0").unwrap();
let port = listener.local_addr().unwrap().port();

// Test panic on `start` when port is in use
let start_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
let mut caps = HashSet::new();
caps.insert(Capability::Network);
let mut ctx2 = RuntimeContext::new(RuntimeConfig {
strict_mode: false,
max_recursion_depth: 1000,
enable_assertions: true,
capabilities: caps,
});
let _ = start.call(
&mut ctx2,
vec![
RuntimeValue::Int(port as i64),
RuntimeValue::Str("<h1>Test</h1>".to_string()),
],
);
}));
// Test error on `start` when port is in use
let result = start.call(
&mut ctx,
vec![
RuntimeValue::Int(port as i64),
RuntimeValue::Str("<h1>Test</h1>".to_string()),
],
);

assert!(
start_result.is_err(),
"Expected `start` to panic due to port already in use"
result.is_err(),
"Expected `start` to return error due to port already in use"
);

// Make sure we stop the server and cleanup the global bool in case of weirdness,
// although the panic meant it wasn't started fully, but `SERVER_RUNNING` is true.
let _ = stop.call(&mut ctx, vec![]).unwrap();

// Test panic on `serve` when port is in use
let serve_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
let mut caps = HashSet::new();
caps.insert(Capability::Network);
let mut ctx3 = RuntimeContext::new(RuntimeConfig {
strict_mode: false,
max_recursion_depth: 1000,
enable_assertions: true,
capabilities: caps,
});
let _ = serve.call(&mut ctx3, vec![RuntimeValue::Int(port as i64)]);
}));
// Test error on `serve` when port is in use
let result = serve.call(&mut ctx, vec![RuntimeValue::Int(port as i64)]);

assert!(
serve_result.is_err(),
"Expected `serve` to panic due to port already in use"
result.is_err(),
"Expected `serve` to return error due to port already in use"
);

// Reset `SERVER_RUNNING` so other tests aren't affected
Expand Down
Loading