use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use crate::context::InvocationContext;
use crate::error::AgentError;
use crate::run_config::run_limits::{
DEFAULT_MAX_LLM_CALLS, LlmCallLimiter, resolve_max_llm_calls_with,
};
#[test]
fn test_resolve_max_llm_calls() {
let cases: [(&str, i32, Option<&str>, i32); 6] = [
("zero takes the default", 0, None, DEFAULT_MAX_LLM_CALLS),
("zero takes the environment override", 0, Some("7"), 7),
(
"zero ignores an unparsable override",
0,
Some("lots"),
DEFAULT_MAX_LLM_CALLS,
),
("zero honors a negative override", 0, Some("-1"), -1),
(
"an explicit value wins over the environment",
5,
Some("7"),
5,
),
("a negative value is passed through", -1, None, -1),
];
for (name, v, env, want) in cases {
assert_eq!(resolve_max_llm_calls_with(v, env), want, "{name}");
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_increment_and_enforce_llm_calls_limit() {
let c = LlmCallLimiter::new(2);
c.increment_and_enforce().unwrap();
c.increment_and_enforce().unwrap();
let err = c.increment_and_enforce().unwrap_err();
assert!(
matches!(err, AgentError::LlmCallsLimitExceeded { limit: 2 }),
"{err:?}"
);
assert_eq!(
err.to_string(),
"max number of llm calls exceeded: limit is 2"
);
for limit in [0, -1] {
let c = LlmCallLimiter::new(limit);
for i in 0..100 {
assert!(
c.increment_and_enforce().is_ok(),
"limit {limit} bounded the run at call {i}"
);
}
}
assert!(
InvocationContext::builder()
.build_invocation()
.llm_call_limiter()
.is_none()
);
let c = Arc::new(LlmCallLimiter::new(50));
let allowed = Arc::new(AtomicUsize::new(0));
let mut tasks = Vec::new();
for _ in 0..100 {
let (c, allowed) = (c.clone(), allowed.clone());
tasks.push(tokio::spawn(async move {
if c.increment_and_enforce().is_ok() {
allowed.fetch_add(1, Ordering::SeqCst);
}
}));
}
for t in tasks {
t.await.unwrap();
}
assert_eq!(allowed.load(Ordering::SeqCst), 50);
}