diff --git a/limitador/src/lib.rs b/limitador/src/lib.rs index 3606730f..971a2b61 100644 --- a/limitador/src/lib.rs +++ b/limitador/src/lib.rs @@ -467,7 +467,7 @@ impl RateLimiter { self.add_limit(limit.clone()); } - for limit in limits_to_keep_in_ns.union(&limits_in_namespace) { + for limit in limits_in_namespace.union(&limits_to_keep_in_ns) { self.storage.update_limit(limit); } } @@ -649,7 +649,7 @@ impl AsyncRateLimiter { self.add_limit(limit.clone()); } - for limit in limits_to_keep_in_ns.union(&limits_in_namespace) { + for limit in limits_in_namespace.union(&limits_to_keep_in_ns) { self.storage.update_limit(limit); } } @@ -694,3 +694,42 @@ fn classify_limits_by_namespace( res } + +#[cfg(test)] +mod test { + use crate::limit::Limit; + use crate::RateLimiter; + use std::collections::HashMap; + + #[test] + fn properly_updates_existing_limits() { + let rl = RateLimiter::new(100); + let namespace = "foo"; + + let l = Limit::new::<_, String>( + namespace, + 42, + 100, + Vec::::default(), + Vec::::default(), + ); + rl.configure_with([l.clone()]).unwrap(); + let limits = rl.get_limits(&namespace.into()); + assert!(limits.contains(&l)); + + let _ = rl.check_rate_limited_and_update(&namespace.into(), &HashMap::default(), 1, false); + + let l = Limit::new( + namespace, + 50, + 100, + Vec::::default(), + Vec::::default(), + ); + rl.configure_with([l.clone()]).unwrap(); + let limits = rl.get_limits(&namespace.into()); + assert!(limits.contains(&l)); + + let _ = rl.check_rate_limited_and_update(&namespace.into(), &HashMap::default(), 1, false); + } +}