From 447899fba2876bdacf47008b698ec21189959310 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Fri, 29 Sep 2023 15:03:56 +0200 Subject: [PATCH] add test --- src/middlewares/methods/cache.rs | 80 ++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/middlewares/methods/cache.rs b/src/middlewares/methods/cache.rs index ea0a696..300f7cb 100644 --- a/src/middlewares/methods/cache.rs +++ b/src/middlewares/methods/cache.rs @@ -46,6 +46,11 @@ impl MiddlewareBuilder 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) } @@ -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"); + } }