titan_client/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
mod http_client;
mod tcp_client;
pub use http_client::Client as TitanClient;
pub use titan_types::*;
#[cfg(feature = "tcp_client")]
pub use tcp_client::subscribe as subscribe_to_titan;
#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;
use titan_types::{Event, TcpSubscriptionRequest};
use tokio::{
sync::watch,
time::{sleep, Duration, Instant},
};
// Import the HTTP and TCP client functions.
use crate::http_client::Client as HttpClient;
#[cfg(feature = "tcp_client")]
use crate::tcp_client::subscribe;
/// End-to-end test for the TCP subscription client.
///
/// This test:
/// 1. Connects to the TCP subscription server at localhost:8080,
/// subscribing to "TransactionsAdded", "TransactionsReplaced", and "NewBlock".
/// 2. Listens for events for 10 seconds, printing each received event.
/// 3. Then signals shutdown.
#[tokio::test]
#[cfg(feature = "tcp_client")]
async fn test_tcp_subscription_e2e() -> Result<(), Box<dyn Error>> {
use tokio::time::timeout;
let tcp_addr = "127.0.0.1:8080";
let subscription_request = TcpSubscriptionRequest {
subscribe: vec![
"TransactionsAdded".to_string(),
"TransactionsReplaced".to_string(),
"NewBlock".to_string(),
],
};
// Create a watch channel to signal shutdown.
let (shutdown_tx, shutdown_rx) = watch::channel(());
// Connect to the TCP server and subscribe.
let mut rx = subscribe(tcp_addr, subscription_request, shutdown_rx).await?;
println!("Connected to TCP subscription server at {}.", tcp_addr);
// Listen for events for 10 seconds.
let listen_duration = Duration::from_secs(10);
let start = Instant::now();
let mut events = Vec::new();
while Instant::now().duration_since(start) < listen_duration {
match timeout(Duration::from_millis(500), rx.recv()).await {
Ok(Some(event)) => {
// We got an event
println!("Received TCP event: {:?}", event);
events.push(event);
}
Ok(None) => {
// The sender side or the connection closed
println!("TCP subscription channel closed. Stopping early.");
break;
}
Err(_) => {
// Timed out waiting for an event
// This means no event arrived in the last 500 ms, but we can keep waiting until 10s is up
}
}
}
println!("Total events received in 10 seconds: {}", events.len());
// Signal shutdown to the subscription task.
let _ = shutdown_tx.send(());
println!("Shutdown signal sent to TCP subscription task.");
Ok(())
}
/// End-to-end test for the HTTP client.
///
/// This test:
/// 1. Connects to the HTTP server at http://localhost:3030.
/// 2. Retrieves and prints the block status and tip.
#[tokio::test]
async fn test_http_status_tip_e2e() -> Result<(), Box<dyn Error>> {
let base_url = "http://localhost:3030";
let client = HttpClient::new(base_url);
println!("Fetching HTTP status from {}...", base_url);
match client.get_status().await {
Ok(status) => {
println!("HTTP Status: {:?}", status);
}
Err(e) => {
eprintln!("Failed to get HTTP status: {}", e);
}
}
println!("Fetching block tip from {}...", base_url);
match client.get_tip().await {
Ok(tip) => {
println!("Block Tip: {:?}", tip);
}
Err(e) => {
eprintln!("Failed to get block tip: {}", e);
}
}
Ok(())
}
}