...
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
use std::time::Duration;
|
||||
use std::thread;
|
||||
use std::io::{Read, Write};
|
||||
use std::os::unix::net::{UnixListener, UnixStream};
|
||||
use std::fs;
|
||||
|
||||
// Import directly from the lib.rs
|
||||
use rustclients::FakeHandlerClient;
|
||||
use rustclients::Result;
|
||||
|
||||
// Simple mock server that handles Unix socket connections
|
||||
fn start_mock_server(socket_path: &str) -> std::thread::JoinHandle<()> {
|
||||
let socket_path = socket_path.to_string();
|
||||
thread::spawn(move || {
|
||||
// Remove the socket file if it exists
|
||||
let _ = fs::remove_file(&socket_path);
|
||||
|
||||
// Create a Unix socket listener
|
||||
let listener = match UnixListener::bind(&socket_path) {
|
||||
Ok(listener) => listener,
|
||||
Err(e) => {
|
||||
println!("Failed to bind to socket {}: {}", socket_path, e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
println!("Mock server listening on {}", socket_path);
|
||||
|
||||
// Accept connections and handle them
|
||||
for stream in listener.incoming() {
|
||||
match stream {
|
||||
Ok(mut stream) => {
|
||||
println!("Mock server: Accepted new connection");
|
||||
|
||||
// Read from the stream
|
||||
let mut buffer = [0; 1024];
|
||||
match stream.read(&mut buffer) {
|
||||
Ok(n) => {
|
||||
let request = String::from_utf8_lossy(&buffer[0..n]);
|
||||
println!("Mock server received: {}", request);
|
||||
|
||||
// Send a welcome message first
|
||||
let welcome = "Welcome to the mock server\n";
|
||||
let _ = stream.write_all(welcome.as_bytes());
|
||||
|
||||
// Send a response
|
||||
let response = "OK: Command processed\n> ";
|
||||
let _ = stream.write_all(response.as_bytes());
|
||||
},
|
||||
Err(e) => println!("Mock server error reading from stream: {}", e),
|
||||
}
|
||||
},
|
||||
Err(e) => println!("Mock server error accepting connection: {}", e),
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
// Define the socket path
|
||||
let socket_path = "/tmp/heroagent/test.sock";
|
||||
|
||||
// Start the mock server
|
||||
println!("Starting mock server...");
|
||||
let server_handle = start_mock_server(socket_path);
|
||||
|
||||
// Give the server time to start
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
|
||||
// Initialize the client
|
||||
let client = FakeHandlerClient::new(socket_path)
|
||||
.with_timeout(Duration::from_secs(5));
|
||||
|
||||
println!("\n--- Test 1: Making first request ---");
|
||||
// This should open a new connection
|
||||
match client.return_success(Some("Test 1")) {
|
||||
Ok(response) => println!("Response: {}", response),
|
||||
Err(e) => println!("Error: {}", e),
|
||||
}
|
||||
|
||||
// Wait a moment
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
|
||||
println!("\n--- Test 2: Making second request ---");
|
||||
// This should open another new connection
|
||||
match client.return_success(Some("Test 2")) {
|
||||
Ok(response) => println!("Response: {}", response),
|
||||
Err(e) => println!("Error: {}", e),
|
||||
}
|
||||
|
||||
// Wait a moment
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
|
||||
println!("\n--- Test 3: Making third request ---");
|
||||
// This should open yet another new connection
|
||||
match client.return_success(Some("Test 3")) {
|
||||
Ok(response) => println!("Response: {}", response),
|
||||
Err(e) => println!("Error: {}", e),
|
||||
}
|
||||
|
||||
println!("\nTest completed. Check the debug output to verify that a new connection was opened for each request.");
|
||||
|
||||
// Clean up
|
||||
let _ = fs::remove_file(socket_path);
|
||||
|
||||
// Wait for the server to finish (in a real application, you might want to signal it to stop)
|
||||
println!("Waiting for server to finish...");
|
||||
// In a real application, we would join the server thread here
|
||||
|
||||
Ok(())
|
||||
}
|
@@ -0,0 +1,100 @@
|
||||
use std::time::Duration;
|
||||
|
||||
// Import directly from the lib.rs
|
||||
use rustclients::FakeHandlerClient;
|
||||
use rustclients::Result;
|
||||
|
||||
|
||||
fn main() -> Result<()> {
|
||||
// Create a new fake handler client
|
||||
// Replace with the actual socket path used in your environment
|
||||
let socket_path = "/tmp/heroagent/fakehandler.sock";
|
||||
|
||||
// Initialize the client with a timeout
|
||||
let client = FakeHandlerClient::new(socket_path)
|
||||
.with_timeout(Duration::from_secs(5));
|
||||
|
||||
println!("Connecting to fake handler at {}", socket_path);
|
||||
|
||||
// Connect to the server
|
||||
match client.connect() {
|
||||
Ok(_) => println!("Successfully connected to fake handler"),
|
||||
Err(e) => {
|
||||
eprintln!("Failed to connect: {}", e);
|
||||
eprintln!("Make sure the fake handler server is running and the socket path is correct");
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Test various commands
|
||||
|
||||
// 1. Get help information
|
||||
println!("\n--- Help Information ---");
|
||||
match client.help() {
|
||||
Ok(help) => println!("{}", help),
|
||||
Err(e) => eprintln!("Error getting help: {}", e),
|
||||
}
|
||||
|
||||
// 2. Return success message
|
||||
println!("\n--- Success Message ---");
|
||||
match client.return_success(Some("Custom success message")) {
|
||||
Ok(response) => println!("Success response: {}", response),
|
||||
Err(e) => eprintln!("Error getting success: {}", e),
|
||||
}
|
||||
|
||||
// 3. Return JSON response
|
||||
println!("\n--- JSON Response ---");
|
||||
match client.return_json(Some("JSON message"), Some("success"), Some(200)) {
|
||||
Ok(response) => println!("JSON response: {:?}", response),
|
||||
Err(e) => eprintln!("Error getting JSON: {}", e),
|
||||
}
|
||||
|
||||
// 4. Return error message (this will return a ClientError)
|
||||
println!("\n--- Error Message ---");
|
||||
match client.return_error(Some("Custom error message")) {
|
||||
Ok(response) => println!("Error response (unexpected success): {}", response),
|
||||
Err(e) => eprintln!("Expected error received: {}", e),
|
||||
}
|
||||
|
||||
// 5. Return empty response
|
||||
println!("\n--- Empty Response ---");
|
||||
match client.return_empty() {
|
||||
Ok(response) => println!("Empty response (length: {})", response.len()),
|
||||
Err(e) => eprintln!("Error getting empty response: {}", e),
|
||||
}
|
||||
|
||||
// 6. Return large response
|
||||
println!("\n--- Large Response ---");
|
||||
match client.return_large(Some(10)) {
|
||||
Ok(response) => {
|
||||
let lines: Vec<&str> = response.lines().collect();
|
||||
println!("Large response (first 3 lines of {} total):", lines.len());
|
||||
for i in 0..std::cmp::min(3, lines.len()) {
|
||||
println!(" {}", lines[i]);
|
||||
}
|
||||
println!(" ...");
|
||||
},
|
||||
Err(e) => eprintln!("Error getting large response: {}", e),
|
||||
}
|
||||
|
||||
// 7. Return invalid JSON (will cause a JSON parsing error)
|
||||
println!("\n--- Invalid JSON ---");
|
||||
match client.return_invalid_json() {
|
||||
Ok(response) => println!("Invalid JSON response (unexpected success): {:?}", response),
|
||||
Err(e) => eprintln!("Expected JSON error received: {}", e),
|
||||
}
|
||||
|
||||
// 8. Return malformed error
|
||||
println!("\n--- Malformed Error ---");
|
||||
match client.return_malformed_error() {
|
||||
Ok(response) => println!("Malformed error response: {}", response),
|
||||
Err(e) => eprintln!("Error with malformed error: {}", e),
|
||||
}
|
||||
|
||||
// Close the connection
|
||||
println!("\nClosing connection");
|
||||
client.close()?;
|
||||
|
||||
println!("Example completed successfully");
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user