cargo fix and fmt
This commit is contained in:
@@ -7,9 +7,9 @@
|
||||
// Ensure circle_server_ws is compiled (cargo build --bin circle_server_ws).
|
||||
|
||||
use circle_client_ws::CircleWsClientBuilder;
|
||||
use tokio::time::{sleep, Duration};
|
||||
use std::process::{Command, Child, Stdio};
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Child, Command, Stdio};
|
||||
use tokio::time::{sleep, Duration};
|
||||
|
||||
const EXAMPLE_SERVER_PORT: u16 = 8089; // Using a specific port for this example
|
||||
const WS_URL: &str = "ws://127.0.0.1:8089/ws";
|
||||
@@ -32,26 +32,56 @@ impl ChildProcessGuard {
|
||||
|
||||
impl Drop for ChildProcessGuard {
|
||||
fn drop(&mut self) {
|
||||
log::info!("Cleaning up {} process (PID: {})...", self.name, self.child.id());
|
||||
log::info!(
|
||||
"Cleaning up {} process (PID: {})...",
|
||||
self.name,
|
||||
self.child.id()
|
||||
);
|
||||
match self.child.kill() {
|
||||
Ok(_) => {
|
||||
log::info!("Successfully sent kill signal to {} (PID: {}).", self.name, self.child.id());
|
||||
log::info!(
|
||||
"Successfully sent kill signal to {} (PID: {}).",
|
||||
self.name,
|
||||
self.child.id()
|
||||
);
|
||||
match self.child.wait() {
|
||||
Ok(status) => log::info!("{} (PID: {}) exited with status: {}", self.name, self.child.id(), status),
|
||||
Err(e) => log::warn!("Error waiting for {} (PID: {}): {}", self.name, self.child.id(), e),
|
||||
Ok(status) => log::info!(
|
||||
"{} (PID: {}) exited with status: {}",
|
||||
self.name,
|
||||
self.child.id(),
|
||||
status
|
||||
),
|
||||
Err(e) => log::warn!(
|
||||
"Error waiting for {} (PID: {}): {}",
|
||||
self.name,
|
||||
self.child.id(),
|
||||
e
|
||||
),
|
||||
}
|
||||
}
|
||||
Err(e) => log::error!("Failed to kill {} (PID: {}): {}", self.name, self.child.id(), e),
|
||||
Err(e) => log::error!(
|
||||
"Failed to kill {} (PID: {}): {}",
|
||||
self.name,
|
||||
self.child.id(),
|
||||
e
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn find_target_bin_path(bin_name: &str) -> Result<PathBuf, String> {
|
||||
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").map_err(|_| "CARGO_MANIFEST_DIR not set".to_string())?;
|
||||
let workspace_root = PathBuf::from(manifest_dir).parent().ok_or("Failed to get workspace root")?.to_path_buf();
|
||||
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
|
||||
.map_err(|_| "CARGO_MANIFEST_DIR not set".to_string())?;
|
||||
let workspace_root = PathBuf::from(manifest_dir)
|
||||
.parent()
|
||||
.ok_or("Failed to get workspace root")?
|
||||
.to_path_buf();
|
||||
let bin_path = workspace_root.join("target").join("debug").join(bin_name);
|
||||
if !bin_path.exists() {
|
||||
return Err(format!("Binary '{}' not found at {:?}. Ensure it's built.", bin_name, bin_path));
|
||||
return Err(format!(
|
||||
"Binary '{}' not found at {:?}. Ensure it's built.",
|
||||
bin_name, bin_path
|
||||
));
|
||||
}
|
||||
Ok(bin_path)
|
||||
}
|
||||
@@ -63,18 +93,31 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let server_bin_path = find_target_bin_path(CIRCLE_SERVER_WS_BIN_NAME)?;
|
||||
log::info!("Found server binary at: {:?}", server_bin_path);
|
||||
|
||||
log::info!("Starting {} for circle '{}' on port {}...", CIRCLE_SERVER_WS_BIN_NAME, CIRCLE_NAME_FOR_EXAMPLE, EXAMPLE_SERVER_PORT);
|
||||
log::info!(
|
||||
"Starting {} for circle '{}' on port {}...",
|
||||
CIRCLE_SERVER_WS_BIN_NAME,
|
||||
CIRCLE_NAME_FOR_EXAMPLE,
|
||||
EXAMPLE_SERVER_PORT
|
||||
);
|
||||
let server_process = Command::new(&server_bin_path)
|
||||
.args([
|
||||
"--port", &EXAMPLE_SERVER_PORT.to_string(),
|
||||
"--circle-name", CIRCLE_NAME_FOR_EXAMPLE
|
||||
"--port",
|
||||
&EXAMPLE_SERVER_PORT.to_string(),
|
||||
"--circle-name",
|
||||
CIRCLE_NAME_FOR_EXAMPLE,
|
||||
])
|
||||
.stdout(Stdio::piped()) // Pipe stdout to keep terminal clean, or Stdio::inherit() to see server logs
|
||||
.stderr(Stdio::piped()) // Pipe stderr as well
|
||||
.spawn()
|
||||
.map_err(|e| format!("Failed to start {}: {}. Ensure it is built.", CIRCLE_SERVER_WS_BIN_NAME, e))?;
|
||||
|
||||
let _server_guard = ChildProcessGuard::new(server_process, CIRCLE_SERVER_WS_BIN_NAME.to_string());
|
||||
.map_err(|e| {
|
||||
format!(
|
||||
"Failed to start {}: {}. Ensure it is built.",
|
||||
CIRCLE_SERVER_WS_BIN_NAME, e
|
||||
)
|
||||
})?;
|
||||
|
||||
let _server_guard =
|
||||
ChildProcessGuard::new(server_process, CIRCLE_SERVER_WS_BIN_NAME.to_string());
|
||||
|
||||
log::info!("Giving the server a moment to start up...");
|
||||
sleep(Duration::from_secs(3)).await; // Wait for server to initialize
|
||||
@@ -99,13 +142,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// This part should not be reached if timeout works correctly.
|
||||
print(x);
|
||||
x
|
||||
".to_string();
|
||||
"
|
||||
.to_string();
|
||||
|
||||
log::info!("Sending long-running script (expected to time out on server after ~{}s)...", SCRIPT_TIMEOUT_SECONDS);
|
||||
log::info!(
|
||||
"Sending long-running script (expected to time out on server after ~{}s)...",
|
||||
SCRIPT_TIMEOUT_SECONDS
|
||||
);
|
||||
|
||||
match client.play(long_running_script).await {
|
||||
Ok(play_result) => {
|
||||
log::warn!("Received unexpected success from play request: {:?}", play_result);
|
||||
log::warn!(
|
||||
"Received unexpected success from play request: {:?}",
|
||||
play_result
|
||||
);
|
||||
log::warn!("This might indicate the script finished faster than expected, or the timeout didn't trigger.");
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -116,7 +166,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if e.to_string().contains("timed out") || e.to_string().contains("-32002") {
|
||||
log::info!("Successfully received timeout error from the server!");
|
||||
} else {
|
||||
log::warn!("Received an error, but it might not be the expected timeout error: {}", e);
|
||||
log::warn!(
|
||||
"Received an error, but it might not be the expected timeout error: {}",
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -127,4 +180,4 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
log::info!("Timeout demonstration example finished.");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user