use std::time::Duration;
use loates::error::Error;
use loates::prelude::*;
use loates::tracing::Message;
use loates::tracing::TracerLayer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Registry;
struct MyUser {}
impl User for MyUser {
async fn call(&mut self) -> UserResult {
let res = loates::client::reqwest::Client::new()
.post("https://httpbin.org/anything")
.body("abc")
.send()
.await?;
if !res.status().is_success() {
let body = res
.bytes()
.await
.map_err(|err| Error::TerminationError(err.into()))?;
let err = String::from_utf8_lossy(&body).to_string();
return Err(Error::termination(err));
}
tokio::time::sleep(Duration::from_millis(500)).await;
Ok(())
}
}
async fn user_builder(_: &RuntimeDataStore) -> impl User {
MyUser {}
}
#[tokio::main]
async fn main() {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let tracer = TracerLayer::new(tx);
tracing::subscriber::set_global_default(Registry::default().with(tracer)).unwrap();
let execution = Execution::builder()
.with_user_builder(user_builder)
.with_executor(Executor::PerUser {
users: 1,
iterations: 8,
});
let scenario = Scenario::new("scene1".to_string(), execution);
let scenarios = vec![scenario];
let handler = tokio::spawn(async move {
while let Some(val) = rx.recv().await {
println!("{:?}", val);
if matches!(val, Message::End) {
break;
}
}
});
Runner::new(scenarios).run().await.unwrap();
handler.await.unwrap();
}