Skip to content

Commit

Permalink
test(runtime-config): Add some tests to make sure data is cached
Browse files Browse the repository at this point in the history
  • Loading branch information
john-z-yang committed Nov 20, 2024
1 parent feffb16 commit c979a7d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions rust_snuba/src/runtime_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub fn get_str_config(key: &str) -> Result<Option<String>, Error> {

#[cfg(test)]
mod tests {
use std::thread;

use super::*;

#[test]
Expand All @@ -48,4 +50,32 @@ mod tests {
let config = get_str_config("test");
assert_eq!(config.unwrap(), None);
}

#[test]
fn test_runtime_config_retrieves_from_cache_within_deadline() {
CONFIG.write().insert(
"key".to_string(),
(
Some("value".to_string()),
Deadline::new(Duration::from_secs(10)),
),
);
let config = get_str_config("key");
assert_eq!(config.unwrap(), Some("value".to_string()));
CONFIG.write().clear();
}

#[test]
fn test_runtime_config_invalidates_cache_outside_deadline() {
CONFIG.write().insert(
"key".to_string(),
(
Some("value".to_string()),
Deadline::new(Duration::from_secs(1)),
),
);
thread::sleep(Duration::from_secs(60));
let config = get_str_config("key");
assert_eq!(config.unwrap(), None);
}
}

0 comments on commit c979a7d

Please sign in to comment.