Add webhook already handled check
Fix all tests Add woodpecker ci (tests + release)
This commit is contained in:
+87
-4
@@ -4,7 +4,8 @@ use crate::{
|
||||
open_router::OpenRouterClient,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use std::{sync::Arc, time::Duration};
|
||||
use std::{collections::HashSet, sync::Arc, time::Duration};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tracing::{error, info, instrument};
|
||||
|
||||
@@ -30,6 +31,7 @@ pub struct Bot {
|
||||
open_router_client: OpenRouterClient,
|
||||
http_client: reqwest::Client,
|
||||
max_concurrent: usize,
|
||||
actions_handled: Arc<Mutex<HashSet<u64>>>,
|
||||
}
|
||||
|
||||
impl Bot {
|
||||
@@ -46,6 +48,7 @@ impl Bot {
|
||||
)?,
|
||||
max_concurrent: config.bot_max_concurrent,
|
||||
config,
|
||||
actions_handled: Arc::new(Mutex::new(HashSet::new())),
|
||||
http_client: reqwest::Client::builder()
|
||||
.timeout(Duration::from_secs(gitea_timeout))
|
||||
.build()?,
|
||||
@@ -72,7 +75,6 @@ impl Bot {
|
||||
},
|
||||
};
|
||||
|
||||
// Drain completed tasks to avoid the JoinSet growing unbounded
|
||||
while let Some(res) = tasks.try_join_next() {
|
||||
if let Err(e) = res {
|
||||
error!("Task panicked: {e}");
|
||||
@@ -89,8 +91,6 @@ impl Bot {
|
||||
});
|
||||
}
|
||||
|
||||
// When all webhook tasks have completed, we can safely exit
|
||||
// properly before returning
|
||||
tasks.join_all().await;
|
||||
|
||||
info!("Bot shutting down complete");
|
||||
@@ -119,4 +119,87 @@ impl Bot {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check_and_mark(&self, event_id: u64) -> bool {
|
||||
let mut action_handled_lock = self.actions_handled.lock().await;
|
||||
|
||||
!action_handled_lock.insert(event_id)
|
||||
}
|
||||
|
||||
pub async fn unmark(&self, event_id: u64) {
|
||||
let mut action_handled_lock = self.actions_handled.lock().await;
|
||||
|
||||
action_handled_lock.remove(&event_id);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn make_actions_handled() -> Arc<Mutex<HashSet<u64>>> {
|
||||
Arc::new(Mutex::new(HashSet::new()))
|
||||
}
|
||||
|
||||
async fn check_and_mark(actions_handled: &Arc<Mutex<HashSet<u64>>>, event_id: u64) -> bool {
|
||||
let mut lock = actions_handled.lock().await;
|
||||
!lock.insert(event_id)
|
||||
}
|
||||
|
||||
async fn unmark(actions_handled: &Arc<Mutex<HashSet<u64>>>, event_id: u64) {
|
||||
let mut lock = actions_handled.lock().await;
|
||||
lock.remove(&event_id);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_check_and_mark_first_call_returns_false() {
|
||||
let actions_handled = make_actions_handled();
|
||||
assert!(!check_and_mark(&actions_handled, 1).await);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_check_and_mark_second_call_returns_true() {
|
||||
let actions_handled = make_actions_handled();
|
||||
check_and_mark(&actions_handled, 1).await;
|
||||
assert!(check_and_mark(&actions_handled, 1).await);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_check_and_mark_different_ids_are_independent() {
|
||||
let actions_handled = make_actions_handled();
|
||||
check_and_mark(&actions_handled, 1).await;
|
||||
assert!(!check_and_mark(&actions_handled, 2).await);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_unmark_allows_reprocessing() {
|
||||
let actions_handled = make_actions_handled();
|
||||
check_and_mark(&actions_handled, 1).await;
|
||||
unmark(&actions_handled, 1).await;
|
||||
assert!(!check_and_mark(&actions_handled, 1).await);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_unmark_nonexistent_id_is_noop() {
|
||||
let actions_handled = make_actions_handled();
|
||||
unmark(&actions_handled, 99).await;
|
||||
assert!(!check_and_mark(&actions_handled, 99).await);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_check_and_mark_concurrent_same_id() {
|
||||
let actions_handled = make_actions_handled();
|
||||
let actions_handled2 = Arc::clone(&actions_handled);
|
||||
|
||||
let t1 = tokio::spawn(async move { check_and_mark(&actions_handled, 42).await });
|
||||
let t2 = tokio::spawn(async move { check_and_mark(&actions_handled2, 42).await });
|
||||
|
||||
let (r1, r2) = tokio::join!(t1, t2);
|
||||
let results = [r1.unwrap(), r2.unwrap()];
|
||||
|
||||
// exactement un seul des deux doit retourner false (non traité)
|
||||
assert_eq!(results.iter().filter(|&&r| !r).count(), 1);
|
||||
// l'autre doit retourner true (déjà traité)
|
||||
assert_eq!(results.iter().filter(|&&r| r).count(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user