Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(runtime-config): Add some tests to make sure data is cached #6586

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Comment on lines +80 to +93
Copy link
Member Author

@john-z-yang john-z-yang Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to set this deadline to maybe 1 sec from now, and sleep for 1 sec to retrieve it, like so

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

But for some reason this fails, and the debugger shows that coarsetime::Instant::recent() does not appear to be advancing

}
}
Loading