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 b4e64ff
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion rust_snuba/src/runtime_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,56 @@ pub fn get_str_config(key: &str) -> Result<Option<String>, Error> {

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn test_runtime_config() {
crate::testutils::initialize_python();
let config = get_str_config("test");
let config = get_str_config("key0");
assert_eq!(config.unwrap(), None);
CONFIG.write().clear();
}

#[test]
fn test_runtime_config_is_stored_in_cache() {
crate::testutils::initialize_python();
assert!(!CONFIG.read().contains_key("key1"));
let config = get_str_config("key1");
assert_eq!(config.unwrap(), None);
assert!(CONFIG.read().contains_key("key1"));
CONFIG.write().clear();
}

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

#[test]
fn test_runtime_config_invalidates_cache_outside_deadline() {
{
CONFIG.write().insert(
"key3".to_string(),
(
Some("value3".to_string()),
Deadline::new(Duration::from_secs(0)),
),
);
}
let config = get_str_config("key3");
assert_eq!(config.unwrap(), None);
CONFIG.write().clear();
}
}

0 comments on commit b4e64ff

Please sign in to comment.