use std::sync::{
  Arc,
  atomic::{AtomicU32, Ordering},
};

use aok::{OK, Void};
use log::info;
use msgq::{Kv, ReadGroup};

#[static_init::constructor(0)]
extern "C" fn _log_init() {
  log_init::init();
}

async fn on_error(mail: Arc<Kv>, error: String) -> Void {
  info!("on_error {error} {:?}", mail);
  OK
}

static N: AtomicU32 = AtomicU32::new(0);

async fn print_mail(mail: Arc<Kv>) -> Void {
  info!("print_mail {:?}", mail);
  if N.fetch_add(1, Ordering::SeqCst) % 10 == 9 {
    Err(aok::anyhow!("test error"))
  } else {
    OK
  }
}

#[tokio::test]
async fn test_async() -> Void {
  xboot::init().await?;
  let read_group = ReadGroup::new("smtp", "send", "test", 1, 3, 2, 3);
  read_group.run(print_mail, on_error).await?;
  OK
}
