use std::time::Duration;
use memseek_test::{
Report, RunMode, TaskIndex,
ctxlibs::http_client::{Client, HttpError, reqwest},
error::ScenarioError,
manager::{ManagerConfig, ScenarioManager},
register_scenario,
scenario::Scenario,
};
struct Ctx {
client: Client,
}
#[derive(Default)]
struct HealthScenario;
impl Scenario for HealthScenario {
type Ctx = Ctx;
type Error = HttpError;
type Output = ();
type Setup = ();
async fn setup(_ctx: &Self::Ctx, _task: &TaskIndex) -> Result<(), Self::Error> {
Ok(())
}
async fn run(ctx: &Ctx, _task: &TaskIndex, _setup: &()) -> Result<(), HttpError> {
ctx.client
.request(reqwest::Method::GET, "/")
.send_checked()
.await?;
Ok(())
}
}
register_scenario!(HealthScenario);
#[derive(Default)]
struct NotFoundScenario;
impl Scenario for NotFoundScenario {
type Ctx = Ctx;
type Error = HttpError;
type Output = Option<HttpError>;
type Setup = ();
async fn setup(_ctx: &Self::Ctx, _task: &TaskIndex) -> Result<(), Self::Error> {
Ok(())
}
async fn run(
ctx: &Ctx,
_task: &TaskIndex,
_setup: &(),
) -> Result<Option<HttpError>, HttpError> {
let outcome = ctx
.client
.request(reqwest::Method::GET, "/missing")
.send_checked()
.await
.err();
Ok(outcome)
}
async fn validate(
_ctx: &Ctx,
_task: &TaskIndex,
_setup: &(),
out: &Option<HttpError>,
) -> Result<bool, HttpError> {
Ok(out.as_ref().is_some_and(|e| e.kind() == "http_status_404"))
}
}
register_scenario!(NotFoundScenario);
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let base = std::env::args()
.nth(1)
.unwrap_or_else(|| "http://localhost:7985".to_string());
let client = Client::from_reqwest(
reqwest::Client::builder()
.timeout(Duration::from_secs(3))
.build()?,
base.as_str(),
)?;
let ctx = Ctx { client };
let manager = ScenarioManager::new(ManagerConfig::new(8).with_run_mode(RunMode::Times(50)));
let reports = manager.run_all(&ctx).await;
println!("{}", reports.report());
let errors: u64 = reports
.iter()
.map(|r| r.error_map.values().sum::<u64>())
.sum();
if errors == 0 {
println!("本次运行没有错误。(若服务没起,这里会列出 connect_refused 等分类计数)");
} else {
for report in &reports {
for (kind, count) in &report.error_map {
println!(" {} -> {kind} x{count}", report.name);
}
}
}
Ok(())
}