Skip to content

Commit

Permalink
Fixed test and take ownership of vars in ctx
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Dec 3, 2024
1 parent e0ae4f6 commit 7cc4ff4
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 67 deletions.
2 changes: 1 addition & 1 deletion limitador-server/src/envoy_rls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl RateLimitService for MyRateLimiter {
req.hits_addend
};

let ctx = (&values).into();
let ctx = values.into();

let rate_limited_resp = match &*self.limiter {
Limiter::Blocking(limiter) => limiter.check_rate_limited_and_update(
Expand Down
6 changes: 3 additions & 3 deletions limitador-server/src/http_api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async fn check(
response_headers: _,
} = request.into_inner();
let namespace = namespace.into();
let ctx = (&values).into();
let ctx = values.into();
let is_rate_limited_result = match state.get_ref().limiter() {
Limiter::Blocking(limiter) => limiter.is_rate_limited(&namespace, &ctx, delta),
Limiter::Async(limiter) => limiter.is_rate_limited(&namespace, &ctx, delta).await,
Expand Down Expand Up @@ -153,7 +153,7 @@ async fn report(
response_headers: _,
} = request.into_inner();
let namespace = namespace.into();
let ctx = (&values).into();
let ctx = values.into();
let update_counters_result = match data.get_ref().limiter() {
Limiter::Blocking(limiter) => limiter.update_counters(&namespace, &ctx, delta),
Limiter::Async(limiter) => limiter.update_counters(&namespace, &ctx, delta).await,
Expand All @@ -178,7 +178,7 @@ async fn check_and_report(
response_headers,
} = request.into_inner();
let namespace = namespace.into();
let ctx = (&values).into();
let ctx = values.into();
let rate_limit_data = data.get_ref();
let rate_limited_and_update_result = match rate_limit_data.limiter() {
Limiter::Blocking(limiter) => limiter.check_rate_limited_and_update(
Expand Down
20 changes: 10 additions & 10 deletions limitador/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use criterion::{black_box, criterion_group, criterion_main, Bencher, BenchmarkId
use rand::seq::SliceRandom;
use rand::SeedableRng;

use limitador::limit::Limit;
use limitador::limit::{Context, Limit};
#[cfg(feature = "disk_storage")]
use limitador::storage::disk::{DiskStorage, OptimizeFor};
#[cfg(feature = "distributed_storage")]
Expand Down Expand Up @@ -89,9 +89,9 @@ const TEST_SCENARIOS: &[&TestScenario] = &[
},
];

struct TestCallParams {
struct TestCallParams<'a> {
namespace: String,
values: HashMap<String, String>,
ctx: Context<'a>,
delta: u64,
}

Expand Down Expand Up @@ -329,7 +329,7 @@ fn bench_is_rate_limited(
rate_limiter
.is_rate_limited(
&params.namespace.to_owned().into(),
&(&params.values).into(),
&params.ctx,
params.delta,
)
.unwrap(),
Expand Down Expand Up @@ -357,7 +357,7 @@ fn async_bench_is_rate_limited<F>(
rate_limiter
.is_rate_limited(
&params.namespace.to_owned().into(),
&(&params.values).into(),
&params.ctx,
params.delta,
)
.await
Expand All @@ -383,7 +383,7 @@ fn bench_update_counters(
rate_limiter
.update_counters(
&params.namespace.to_owned().into(),
&(&params.values).into(),
&params.ctx,
params.delta,
)
.unwrap();
Expand All @@ -410,7 +410,7 @@ fn async_bench_update_counters<F>(
rate_limiter
.update_counters(
&params.namespace.to_owned().into(),
&(&params.values).into(),
&params.ctx,
params.delta,
)
.await
Expand All @@ -437,7 +437,7 @@ fn bench_check_rate_limited_and_update(
rate_limiter
.check_rate_limited_and_update(
&params.namespace.to_owned().into(),
&(&params.values).into(),
&params.ctx,
params.delta,
false,
)
Expand Down Expand Up @@ -467,7 +467,7 @@ fn async_bench_check_rate_limited_and_update<F>(
rate_limiter
.check_rate_limited_and_update(
&params.namespace.to_owned().into(),
&(&params.values).into(),
&params.ctx,
params.delta,
false,
)
Expand Down Expand Up @@ -562,7 +562,7 @@ fn generate_test_limits(scenario: &TestScenario) -> (Vec<Limit>, Vec<TestCallPar

call_params.push(TestCallParams {
namespace,
values: test_values.clone(),
ctx: test_values.clone().into(),
delta: 1,
});
}
Expand Down
2 changes: 1 addition & 1 deletion limitador/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ mod tests {
[var.try_into().expect("failed parsing!")],
);
let map = HashMap::from([("ts".to_string(), "2019-10-12T13:20:50.52Z".to_string())]);
let ctx = (&map).into();
let ctx = map.into();
let counter = Counter::new(limit, &ctx).expect("failed creating counter");
assert_eq!(
counter.unwrap().set_variables.get(var),
Expand Down
16 changes: 8 additions & 8 deletions limitador/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ mod tests {
values.insert("x".into(), "5".into());
values.insert("y".into(), "1".into());

assert!(limit.applies(&(&values).into()))
assert!(limit.applies(&values.into()))
}

#[test]
Expand All @@ -277,7 +277,7 @@ mod tests {
values.insert("x".into(), "1".into());
values.insert("y".into(), "1".into());

assert!(!limit.applies(&(&values).into()))
assert!(!limit.applies(&values.into()))
}

#[test]
Expand All @@ -295,7 +295,7 @@ mod tests {
values.insert("a".into(), "1".into());
values.insert("y".into(), "1".into());

assert!(!limit.applies(&(&values).into()))
assert!(!limit.applies(&values.into()))
}

#[test]
Expand All @@ -312,7 +312,7 @@ mod tests {
let mut values: HashMap<String, String> = HashMap::new();
values.insert("x".into(), "5".into());

assert!(!limit.applies(&(&values).into()))
assert!(!limit.applies(&values.into()))
}

#[test]
Expand All @@ -333,7 +333,7 @@ mod tests {
values.insert("y".into(), "2".into());
values.insert("z".into(), "1".into());

assert!(limit.applies(&(&values).into()))
assert!(limit.applies(&values.into()))
}

#[test]
Expand All @@ -354,7 +354,7 @@ mod tests {
values.insert("y".into(), "2".into());
values.insert("z".into(), "1".into());

assert!(!limit.applies(&(&values).into()))
assert!(!limit.applies(&values.into()))
}

#[test]
Expand Down Expand Up @@ -434,7 +434,7 @@ mod tests {
],
Vec::default(),
);
assert!(limit.applies(&(&HashMap::default()).into()));
assert!(limit.applies(&Context::default()));

let limit = Limit::with_id(
"my_id",
Expand Down Expand Up @@ -462,7 +462,7 @@ mod tests {
("foo".to_string(), "nice bar!".to_string()),
("bar".to_string(), "foo,baz".to_string()),
]);
let ctx = Context::new(String::default(), &map);
let ctx = map.into();
assert!(limit.applies(&ctx));
assert_eq!(
Counter::new(limit, &ctx)
Expand Down
19 changes: 9 additions & 10 deletions limitador/src/limit/cel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,21 @@ pub struct Context<'a> {
}

impl<'a> Context<'a> {
pub(crate) fn new(root: String, values: &HashMap<String, String>) -> Self {
pub(crate) fn new(root: String, values: HashMap<String, String>) -> Self {
let mut ctx = cel_interpreter::Context::default();
let mut variables = HashSet::new();

if root.is_empty() {
for (binding, value) in values {
ctx.add_variable_from_value(binding, value.clone())
ctx.add_variable_from_value(binding.clone(), value.clone());
variables.insert(binding);
}
} else {
let map = cel_interpreter::objects::Map::from(values.clone());
ctx.add_variable_from_value(root, Value::Map(map));
}

Self {
variables: values.keys().cloned().collect(),
ctx,
}
Self { variables, ctx }
}

pub fn list_binding(&mut self, name: String, value: Vec<HashMap<String, String>>) {
Expand Down Expand Up @@ -146,12 +145,12 @@ impl<'a> Context<'a> {

impl Default for Context<'_> {
fn default() -> Self {
Self::new(String::default(), &HashMap::default())
Self::new(String::default(), HashMap::default())
}
}

impl From<&HashMap<String, String>> for Context<'_> {
fn from(value: &HashMap<String, String>) -> Self {
impl From<HashMap<String, String>> for Context<'_> {
fn from(value: HashMap<String, String>) -> Self {
Self::new(String::default(), value)
}
}
Expand Down Expand Up @@ -411,7 +410,7 @@ mod tests {
fn predicate_no_key() {
let pred = Predicate::parse("there.not == 42").expect("failed to parse");
assert_eq!(
pred.test(&(&HashMap::from([("there".to_string(), String::default())])).into()),
pred.test(&HashMap::from([("there".to_string(), String::default())]).into()),
Ok(false)
);
}
Expand Down
2 changes: 1 addition & 1 deletion limitador/src/storage/disk/rocksdb_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ mod tests {
vec!["app_id".try_into().expect("failed parsing!")],
);
let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
let ctx = (&map).into();
let ctx = map.into();
let counter = Counter::new(limit, &ctx)
.unwrap()
.expect("must have a counter");
Expand Down
2 changes: 1 addition & 1 deletion limitador/src/storage/distributed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ fn encode_limit_to_key(limit: &Limit) -> Vec<u8> {
.into_iter()
.map(|k| (k, "".to_string()))
.collect();
let ctx = (&vars).into();
let ctx = vars.into();
let counter = Counter::new(limit.clone(), &ctx)
.expect("counter creation can't fail! faked vars!")
.expect("must have a counter");
Expand Down
2 changes: 1 addition & 1 deletion limitador/src/storage/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ mod tests {
vec!["app_id".try_into().expect("failed parsing!")],
);
let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
let ctx = (&map).into();
let ctx = map.into();
let counter_1 = Counter::new(limit_1, &ctx)
.expect("counter creation failed!")
.expect("Should have a counter");
Expand Down
12 changes: 6 additions & 6 deletions limitador/src/storage/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ mod tests {
vec!["app_id".try_into().expect("failed parsing!")],
);
let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
let ctx = (&map).into();
let ctx = map.into();
let counter = Counter::new(limit.clone(), &ctx)
.expect("counter creation failed!")
.expect("must have a counter");
Expand All @@ -173,7 +173,7 @@ mod tests {
vec!["app_id".try_into().expect("failed parsing!")],
);
let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
let ctx = (&map).into();
let ctx = map.into();
let counter = Counter::new(limit.clone(), &ctx)
.expect("counter creation failed!")
.expect("must have a counter");
Expand Down Expand Up @@ -387,7 +387,7 @@ pub mod bin {
vars.insert("role".to_string(), "admin".to_string());
vars.insert("app_id".to_string(), "123".to_string());
vars.insert("wat".to_string(), "dunno".to_string());
let ctx = (&vars).into();
let ctx = vars.into();
let counter = Counter::new(limit.clone(), &ctx)
.expect("counter creation failed!")
.expect("must have a counter");
Expand All @@ -411,7 +411,7 @@ pub mod bin {
);
let mut variables = HashMap::default();
variables.insert("app_id".to_string(), "123".to_string());
let ctx = (&variables).into();
let ctx = variables.into();
let counter = Counter::new(limit.clone(), &ctx)
.expect("counter creation failed!")
.expect("must have a counter");
Expand All @@ -430,7 +430,7 @@ pub mod bin {
vec!["app_id".try_into().expect("failed parsing!")],
);
let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
let ctx = (&map).into();
let ctx = map.into();
let counter = Counter::new(limit, &ctx)
.expect("counter creation failed!")
.expect("must have a counter");
Expand Down Expand Up @@ -460,7 +460,7 @@ pub mod bin {
);

let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
let ctx = (&map).into();
let ctx = map.into();
let counter_with_id = Counter::new(limit_with_id, &ctx)
.expect("counter creation failed!")
.expect("must have a counter");
Expand Down
3 changes: 1 addition & 2 deletions limitador/src/storage/redis/counters_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,6 @@ mod tests {
if let Some(overrides) = other_values {
values.extend(overrides);
}
let ctx = (&values).into();
Counter::new(
Limit::new(
"test_namespace",
Expand All @@ -685,7 +684,7 @@ mod tests {
vec!["req_method == 'POST'".try_into().expect("failed parsing!")],
vec!["app_id".try_into().expect("failed parsing!")],
),
&ctx,
&values.into(),
)
.expect("failed creating counter")
.expect("Should have a counter")
Expand Down
6 changes: 3 additions & 3 deletions limitador/src/storage/redis/redis_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ mod tests {

let mut counters_and_deltas = HashMap::new();
let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
let ctx = (&map).into();
let ctx = map.into();
let counter = Counter::new(
Limit::new(
"test_namespace",
Expand Down Expand Up @@ -506,7 +506,7 @@ mod tests {
#[tokio::test]
async fn flush_batcher_and_update_counters_test() {
let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
let ctx = (&map).into();
let ctx = map.into();
let counter = Counter::new(
Limit::new(
"test_namespace",
Expand Down Expand Up @@ -569,7 +569,7 @@ mod tests {
#[tokio::test]
async fn flush_batcher_reverts_on_err() {
let map = HashMap::from([("app_id".to_string(), "foo".to_string())]);
let ctx = (&map).into();
let ctx = map.into();
let counter = Counter::new(
Limit::new(
"test_namespace",
Expand Down
Loading

0 comments on commit 7cc4ff4

Please sign in to comment.