mod common; use common::run_test; use maplit::hashmap; use std::collections::HashMap; use uuid::Uuid; use launchdarkly_server_sdk::{AllData, PersistentDataStoreFactory, SerializedItem}; use launchdarkly_server_sdk_redis::RedisPersistentDataStoreFactory; #[test] fn client_is_not_initialized_by_default() { let prefix = Uuid::new_v4().hyphenated().to_string(); run_test(&prefix, || { let mut factory = RedisPersistentDataStoreFactory::new(); factory.prefix(&prefix); let store = factory.create_persistent_data_store().expect("FAIL"); assert!(!store.is_initialized()); }) } #[test] fn calling_init_marks_client_as_initialized() { let prefix = Uuid::new_v4().hyphenated().to_string(); run_test(&prefix, || { let mut factory = RedisPersistentDataStoreFactory::new(); factory.prefix(&prefix); let mut store = factory.create_persistent_data_store().expect("FAIL"); let all_data = AllData { flags: HashMap::new(), segments: HashMap::new(), }; let result = store.init(all_data); assert!(result.is_ok()); assert!(store.is_initialized()); }) } #[test] fn client_sees_initialization_status_from_another_client() { let prefix = Uuid::new_v4().hyphenated().to_string(); run_test(&prefix, || { let mut factory = RedisPersistentDataStoreFactory::new(); factory.prefix(&prefix); let mut store1 = factory.create_persistent_data_store().expect("FAIL"); let all_data = AllData { flags: HashMap::new(), segments: HashMap::new(), }; let result = store1.init(all_data); assert!(result.is_ok()); let store2 = factory.create_persistent_data_store().expect("FAIL"); assert!(store1.is_initialized()); assert!(store2.is_initialized()); }) } #[test] fn different_prefixes_are_independent_of_each_other() { let prefix1 = Uuid::new_v4().hyphenated().to_string(); let prefix2 = Uuid::new_v4().hyphenated().to_string(); run_test(&prefix1, || { run_test(&prefix2, || { let mut factory1 = RedisPersistentDataStoreFactory::new(); factory1.prefix(&prefix1); let mut factory2 = RedisPersistentDataStoreFactory::new(); factory2.prefix(&prefix2); let mut store1 = factory1.create_persistent_data_store().expect("FAIL"); let store2 = factory2.create_persistent_data_store().expect("FAIL"); let all_data = AllData { flags: HashMap::new(), segments: HashMap::new(), }; let result = store1.init(all_data); assert!(result.is_ok()); assert!(store1.is_initialized()); assert!(!store2.is_initialized()); }) }) } #[test] fn init_replaces_all_previous_data() { let prefix = Uuid::new_v4().hyphenated().to_string(); run_test(&prefix, || { let mut factory = RedisPersistentDataStoreFactory::new(); factory.prefix(&prefix); let mut store = factory.create_persistent_data_store().expect("FAIL"); let initial_data = AllData { flags: hashmap! { "first-flag".into() => SerializedItem { version: 1, deleted: false, serialized_item: "flag".into() } }, segments: hashmap! { "first-segment".into() => SerializedItem { version: 1, deleted: false, serialized_item: "segment".into() } }, }; let result = store.init(initial_data); assert!(result.is_ok()); assert_eq!(store.all_flags().unwrap().values().count(), 1); assert!(store.segment("first-segment").unwrap().is_some()); let all_data = AllData { flags: HashMap::new(), segments: HashMap::new(), }; let result = store.init(all_data); assert!(result.is_ok()); assert_eq!(store.all_flags().unwrap().values().count(), 0); assert!(store.segment("first-segment").unwrap().is_none()); }) }