| Crates.io | binary-options-tools-core-pre |
| lib.rs | binary-options-tools-core-pre |
| version | 0.1.1 |
| created_at | 2025-11-03 15:56:06.215372+00 |
| updated_at | 2025-11-03 15:56:06.215372+00 |
| description | Low-level WebSocket client and protocol handler for binary options trading. Core foundation providing connection management, message routing, and async communication primitives. |
| homepage | https://chipadevteam.github.io/BinaryOptionsTools-v2/ |
| repository | https://github.com/ChipaDevTeam/BinaryOptionsTools-v2 |
| max_upload_size | |
| id | 1914883 |
| size | 319,914 |
A comprehensive WebSocket testing and monitoring framework for the binary-options-tools-core-pre crate.
This framework provides advanced statistics tracking, connection monitoring, and testing capabilities for WebSocket-based applications. It wraps around the existing Client and ClientRunner architecture to provide detailed insights into connection performance and reliability.
use binary_options_tools_core_pre::testing::{TestingWrapper, TestingWrapperBuilder};
use binary_options_tools_core_pre::builder::ClientBuilder;
use std::time::Duration;
// Create your client and runner as usual
let (client, runner) = ClientBuilder::new(connector, state)
.with_module::<YourModule>()
.build()
.await?;
// Wrap with testing capabilities
let mut testing_wrapper = TestingWrapperBuilder::new()
.with_stats_interval(Duration::from_secs(30))
.with_log_stats(true)
.with_connection_timeout(Duration::from_secs(10))
.build(client, runner);
// Start the wrapper (this will run the ClientRunner and begin collecting statistics)
testing_wrapper.start().await?;
// Use the client through the wrapper
let client = testing_wrapper.client();
// ... use client as normal ...
// Get statistics
let stats = testing_wrapper.get_stats().await;
println!("Connection success rate: {:.1}%",
stats.successful_connections as f64 / stats.connection_attempts as f64 * 100.0);
// Stop the wrapper (graceful shutdown)
testing_wrapper.stop_and_shutdown().await?;
cargo run --example testing_echo_client
cargo test testing_wrapper
The main wrapper class that provides testing capabilities:
pub struct TestingWrapper<S: AppState> {
// Internal fields
}
impl<S: AppState> TestingWrapper<S> {
pub async fn start(&mut self) -> CoreResult<()>
pub async fn stop(&mut self) -> CoreResult<()>
pub async fn stop_and_shutdown(self) -> CoreResult<()>
pub async fn get_stats(&self) -> ConnectionStats
pub async fn export_stats_json(&self) -> CoreResult<String>
pub async fn export_stats_csv(&self) -> CoreResult<String>
pub fn client(&self) -> &Client<S>
pub fn client_mut(&mut self) -> &mut Client<S>
}
Builder pattern for creating testing wrappers:
pub struct TestingWrapperBuilder<S: AppState> {
// Internal fields
}
impl<S: AppState> TestingWrapperBuilder<S> {
pub fn new() -> Self
pub fn with_stats_interval(self, interval: Duration) -> Self
pub fn with_log_stats(self, log_stats: bool) -> Self
pub fn with_track_events(self, track_events: bool) -> Self
pub fn with_max_reconnect_attempts(self, max_attempts: Option<u32>) -> Self
pub fn with_reconnect_delay(self, delay: Duration) -> Self
pub fn with_connection_timeout(self, timeout: Duration) -> Self
pub fn with_auto_reconnect(self, auto_reconnect: bool) -> Self
pub fn build(self, client: Client<S>, runner: ClientRunner<S>) -> TestingWrapper<S>
}
Statistics structure with comprehensive metrics:
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionStats {
pub connection_attempts: u64,
pub successful_connections: u64,
pub failed_connections: u64,
pub disconnections: u64,
pub reconnections: u64,
pub avg_connection_latency_ms: f64,
pub last_connection_latency_ms: f64,
pub total_uptime_seconds: f64,
pub current_uptime_seconds: f64,
pub time_since_last_disconnection_seconds: f64,
pub messages_sent: u64,
pub messages_received: u64,
pub bytes_sent: u64,
pub bytes_received: u64,
pub avg_messages_sent_per_second: f64,
pub avg_messages_received_per_second: f64,
pub avg_bytes_sent_per_second: f64,
pub avg_bytes_received_per_second: f64,
pub is_connected: bool,
pub connection_history: Vec<ConnectionEvent>,
}
pub struct TestingEchoPlatform {
testing_wrapper: TestingWrapper<()>,
}
impl TestingEchoPlatform {
pub async fn new(url: String) -> CoreResult<Self> {
let connector = DummyConnector::new(url);
let (client, runner) = ClientBuilder::new(connector, ())
.with_module::<EchoModule>()
.build()
.await?;
let testing_wrapper = TestingWrapperBuilder::new()
.with_stats_interval(Duration::from_secs(10))
.with_log_stats(true)
.with_max_reconnect_attempts(Some(3))
.build(client, runner);
Ok(Self { testing_wrapper })
}
pub async fn run_performance_test(&self, num_messages: usize, delay_ms: u64) -> CoreResult<()> {
for i in 0..num_messages {
let msg = format!("Test message {}", i);
let response = self.echo(msg).await?;
if delay_ms > 0 {
tokio::time::sleep(Duration::from_millis(delay_ms)).await;
}
}
let stats = self.get_stats().await;
println!("Test completed. Messages sent: {}, received: {}",
stats.messages_sent, stats.messages_received);
Ok(())
}
}
// Export to JSON
let json_stats = testing_wrapper.export_stats_json().await?;
println!("JSON Stats:\n{}", json_stats);
// Export to CSV
let csv_stats = testing_wrapper.export_stats_csv().await?;
println!("CSV Stats:\n{}", csv_stats);
examples/testing_echo_client.rs - Complete example with performance testingtests/testing_wrapper_tests.rs - Unit tests demonstrating usageSee docs/testing-framework.md for planned features including:
When adding new features:
StatisticsTrackerThis framework is part of the binary-options-tools-core-pre crate and follows the same license.