pub struct TestServerConfig {
pub api_client: Option<ApiClientBuilder>,
pub min_backoff_delay: Duration,
pub max_backoff_delay: Duration,
pub backoff_jitter: bool,
pub max_retry_attempts: usize,
}
Expand description
Configuration for test server behavior and client setup.
This struct allows customizing how the TestClient interacts with the test server, including the ApiClient configuration and exponential backoff timing for health checks.
§Fields
api_client
- Optional pre-configured ApiClient builder for custom client setupmin_backoff_delay
- Minimum delay for exponential backoff between health check retriesmax_backoff_delay
- Maximum delay for exponential backoff between health check retriesbackoff_jitter
- Whether to add jitter to exponential backoff delaysmax_retry_attempts
- Maximum number of health check retry attempts
§Examples
§Default Configuration
use clawspec_core::test_client::TestServerConfig;
use std::time::Duration;
let config = TestServerConfig::default();
assert!(config.api_client.is_none());
assert_eq!(config.min_backoff_delay, Duration::from_millis(10));
assert_eq!(config.max_backoff_delay, Duration::from_secs(1));
assert_eq!(config.backoff_jitter, true);
assert_eq!(config.max_retry_attempts, 10);
§Custom Configuration
use clawspec_core::{test_client::TestServerConfig, ApiClient};
use std::time::Duration;
let config = TestServerConfig {
api_client: Some(
ApiClient::builder()
.with_host("test-server.local")
.with_port(3000)
.with_base_path("/api/v1").unwrap()
),
min_backoff_delay: Duration::from_millis(50),
max_backoff_delay: Duration::from_secs(5),
backoff_jitter: false,
max_retry_attempts: 3,
};
§Using with TestServer
use clawspec_core::test_client::{TestServer, TestServerConfig};
use std::{net::TcpListener, time::Duration};
#[derive(Debug)]
struct MyTestServer;
impl TestServer for MyTestServer {
type Error = std::io::Error;
async fn launch(&self, listener: TcpListener) -> Result<(), Self::Error> {
// Server implementation
listener.set_nonblocking(true)?;
let _tokio_listener = tokio::net::TcpListener::from_std(listener)?;
Ok(())
}
fn config(&self) -> TestServerConfig {
TestServerConfig {
api_client: Some(
clawspec_core::ApiClient::builder()
.with_host("localhost")
.with_base_path("/api").unwrap()
),
min_backoff_delay: Duration::from_millis(25),
max_backoff_delay: Duration::from_secs(2),
backoff_jitter: true,
max_retry_attempts: 15,
}
}
}
Fields§
§api_client: Option<ApiClientBuilder>
Optional pre-configured ApiClient builder.
If provided, this builder will be used as the base for creating the ApiClient. If None, a default builder will be used. The TestClient will automatically configure the port based on the bound server address.
§Example
use clawspec_core::{test_client::TestServerConfig, ApiClient};
let config = TestServerConfig {
api_client: Some(
ApiClient::builder()
.with_host("api.example.com")
.with_base_path("/v1").unwrap()
),
min_backoff_delay: std::time::Duration::from_millis(10),
max_backoff_delay: std::time::Duration::from_secs(1),
backoff_jitter: true,
max_retry_attempts: 10,
};
min_backoff_delay: Duration
Minimum delay for exponential backoff between health check retries.
This is the initial delay used when the server is unhealthy and needs
to be retried. The delay will increase exponentially up to max_backoff_delay
.
§Default
10 milliseconds
§Example
use clawspec_core::test_client::TestServerConfig;
use std::time::Duration;
let config = TestServerConfig {
min_backoff_delay: Duration::from_millis(50), // Start with 50ms
..Default::default()
};
max_backoff_delay: Duration
Maximum delay for exponential backoff between health check retries.
This is the upper bound for the exponential backoff delay. Once the delay reaches this value, it will not increase further.
§Default
1 second
§Example
use clawspec_core::test_client::TestServerConfig;
use std::time::Duration;
let config = TestServerConfig {
max_backoff_delay: Duration::from_secs(5), // Max 5 seconds
..Default::default()
};
backoff_jitter: bool
Whether to add jitter to the exponential backoff delays.
Jitter adds randomization to retry delays to prevent the “thundering herd” problem when multiple clients retry simultaneously. This is generally recommended for production use.
§Default
true
(jitter enabled)
§Example
use clawspec_core::test_client::TestServerConfig;
let config = TestServerConfig {
backoff_jitter: false, // Disable jitter for predictable timing
..Default::default()
};
max_retry_attempts: usize
Maximum number of health check retry attempts.
This limits the total number of health check attempts before giving up. The health check will stop retrying once this number of attempts is reached, preventing infinite loops when a server never becomes healthy.
§Default
10 attempts
§Example
use clawspec_core::test_client::TestServerConfig;
let config = TestServerConfig {
max_retry_attempts: 5, // Only try 5 times before giving up
..Default::default()
};
Trait Implementations§
Source§impl Clone for TestServerConfig
impl Clone for TestServerConfig
Source§fn clone(&self) -> TestServerConfig
fn clone(&self) -> TestServerConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more