use bytes::Bytes;
use office_convert_client::{OfficeConvertClient, OfficeConvertLoadBalancer};
use std::sync::Arc;
use testcontainers::{
GenericImage, ImageExt,
core::{
IntoContainerPort, WaitFor, logs::consumer::logging_consumer::LoggingConsumer,
wait::HttpWaitStrategy,
},
runners::AsyncRunner,
};
use tokio::sync::Barrier;
#[tokio::test(flavor = "multi_thread")]
async fn attempt_spam() {
unsafe { std::env::set_var("RUST_LOG", "debug") };
tracing_subscriber::fmt().init();
let container = GenericImage::new("jacobtread/office-convert-server", "0.2.2")
.with_exposed_port(3000.tcp())
.with_wait_for(WaitFor::http(
HttpWaitStrategy::new("/status").with_expected_status_code(200u16),
))
.with_env_var("RUST_LOG", "debug")
.with_log_consumer(LoggingConsumer::new())
.start()
.await
.unwrap();
let host = container.get_host().await.unwrap();
let host_port = container.get_host_port_ipv4(3000).await.unwrap();
let client_url = format!("http://{host}:{host_port}");
let sets = 5;
let tasks = 10;
let barrier = Arc::new(Barrier::new((tasks * sets) + 1));
let file = Bytes::from_static(include_bytes!("samples/sample.docx"));
for set in 0..sets {
let client = OfficeConvertClient::new(client_url.as_str()).unwrap();
let lb = OfficeConvertLoadBalancer::new([client]);
let lb = Arc::new(lb);
for task in 0..tasks {
let lb = lb.clone();
let barrier = barrier.clone();
let file = file.clone();
tokio::spawn(async move {
println!("start job set = {set}, task = {task}");
if let Err(err) = lb.convert(file).await {
eprintln!("Failed conversion: {err:#?}")
}
println!("end job set = {set}, task = {task}");
barrier.wait().await;
});
}
}
barrier.wait().await;
}