use std::{thread, time::Duration};
use log::info;
use fast_able::fast_thread_pool::TaskExecutor;
use core_affinity::{get_core_ids, CoreId};
fn main() {
let cores = get_core_ids().unwrap();
let first_core = cores[0];
println!("Testing TaskExecutor with thread_task_bounded feature");
#[cfg(feature = "thread_task_bounded")]
{
println!("Creating bounded TaskExecutor with default capacity");
let executor = TaskExecutor::new(first_core, -1);
for i in 0..10 {
executor.spawn(move |core_id| {
println!("Bounded task {} executed on core {}", i, core_id);
thread::sleep(Duration::from_millis(100));
});
}
thread::sleep(Duration::from_secs(2));
println!("Bounded TaskExecutor default test completed");
println!("Creating bounded TaskExecutor with custom capacity (5)");
let custom_executor = TaskExecutor::new_with_capacity(first_core, 5, -1);
for i in 0..8 {
custom_executor.spawn(move |core_id| {
println!("Custom bounded task {} executed on core {}", i, core_id);
thread::sleep(Duration::from_millis(50));
});
}
for i in 0..3 {
let success = custom_executor.try_spawn(move |core_id| {
println!("Try spawn task {} executed on core {}", i, core_id);
});
println!("Try spawn task {}: {}", i, if success { "Success" } else { "Failed (queue full)" });
}
thread::sleep(Duration::from_secs(2));
println!("Custom bounded TaskExecutor test completed");
}
#[cfg(not(feature = "thread_task_bounded"))]
{
println!("Creating unbounded TaskExecutor (default behavior)");
let executor = TaskExecutor::new(first_core, -1);
for i in 0..10 {
executor.spawn(move |core_id| {
println!("Unbounded task {} executed on core {}", i, core_id);
thread::sleep(Duration::from_millis(100));
});
}
thread::sleep(Duration::from_secs(2));
println!("Unbounded TaskExecutor test completed");
}
println!("All tests completed");
}