Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Sep 29, 2023
1 parent becd23c commit 447899f
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/middlewares/methods/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ impl MiddlewareBuilder<RpcMethod, CallRequest, CallResult> for CacheMiddleware {
};

let ttl_seconds = match method.cache {
// ttl zero means cache forever
Some(CacheParams {
ttl_seconds: Some(0),
..
}) => None,
Some(CacheParams { ttl_seconds, .. }) => {
ttl_seconds.or(cache_ext.config.default_ttl_seconds)
}
Expand Down Expand Up @@ -324,4 +329,79 @@ mod tests {
assert_eq!(res.unwrap(), json!(1));
assert_eq!(res2.unwrap(), json!(1));
}

#[tokio::test]
async fn cache_builder_works() {
let ext = crate::extensions::ExtensionsConfig {
cache: Some(crate::extensions::cache::CacheConfig {
default_size: 100,
default_ttl_seconds: Some(10),
}),
..Default::default()
}
.create_registry()
.await
.expect("Failed to create registry");

// disable cache with size = 0
let cache_middleware = CacheMiddleware::build(
&RpcMethod {
method: "foo".to_string(),
cache: Some(CacheParams {
size: Some(0),
ttl_seconds: None,
}),
params: vec![],
response: None,
},
&ext,
)
.await;
assert!(cache_middleware.is_none(), "Cache should be disabled");

// size none, use default size
let cache_middleware = CacheMiddleware::build(
&RpcMethod {
method: "foo".to_string(),
cache: Some(CacheParams {
size: None,
ttl_seconds: None,
}),
params: vec![],
response: None,
},
&ext,
)
.await;
assert!(cache_middleware.is_some(), "Cache should be enabled");

// custom size
let cache_middleware = CacheMiddleware::build(
&RpcMethod {
method: "foo".to_string(),
cache: Some(CacheParams {
size: Some(1),
ttl_seconds: None,
}),
params: vec![],
response: None,
},
&ext,
)
.await;
assert!(cache_middleware.is_some(), "Cache should be enabled");

// no cache params
let cache_middleware = CacheMiddleware::build(
&RpcMethod {
method: "foo".to_string(),
cache: None,
params: vec![],
response: None,
},
&ext,
)
.await;
assert!(cache_middleware.is_some(), "Cache should be enabled");
}
}

0 comments on commit 447899f

Please sign in to comment.