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(config-worker): test client_worker add cache_datda #30

Merged
merged 2 commits into from
Oct 19, 2022
Merged
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
135 changes: 135 additions & 0 deletions src/config/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,138 @@ impl ListenerWrapper {
}
}
}

#[cfg(test)]
mod tests {
use crate::api::config::{ConfigChangeListener, ConfigResponse};
use crate::api::props::ClientProps;
use crate::config::util;
use crate::config::worker::{CacheData, ConfigWorker};
use std::sync::Arc;

#[test]
fn test_client_worker_add_listener() {
let mut client_worker = ConfigWorker::new(ClientProps::new());

let (d, g, t) = ("D".to_string(), "G".to_string(), "N".to_string());

// test add listener1
let lis1_arc = Arc::new(TestConfigChangeListener1 {});
let _listen = client_worker.add_listener(d.clone(), g.clone(), t.clone(), lis1_arc);

// test add listener2
let lis2_arc = Arc::new(TestConfigChangeListener2 {});
let _listen = client_worker.add_listener(d.clone(), g.clone(), t.clone(), lis2_arc.clone());
// test add a listener2 again
let _listen = client_worker.add_listener(d.clone(), g.clone(), t.clone(), lis2_arc);

let group_key = util::group_key(&d, &g, &t);
{
let cache_data_map_mutex = client_worker.cache_data_map.lock().unwrap();
let cache_data = cache_data_map_mutex.get(group_key.as_str()).unwrap();
let listen_mutex = cache_data.listeners.lock().unwrap();
assert_eq!(2, listen_mutex.len());
}
}

#[test]
fn test_client_worker_add_listener_then_remove() {
let mut client_worker = ConfigWorker::new(ClientProps::new());

let (d, g, t) = ("D".to_string(), "G".to_string(), "N".to_string());

// test add listener1
let lis1_arc = Arc::new(TestConfigChangeListener1 {});
let lis1_arc2 = Arc::clone(&lis1_arc);
let _listen = client_worker.add_listener(d.clone(), g.clone(), t.clone(), lis1_arc);

let group_key = util::group_key(&d, &g, &t);
{
let cache_data_map_mutex = client_worker.cache_data_map.lock().unwrap();
let cache_data = cache_data_map_mutex.get(group_key.as_str()).unwrap();
let listen_mutex = cache_data.listeners.lock().unwrap();
assert_eq!(1, listen_mutex.len());
}

client_worker.remove_listener(d.clone(), g.clone(), t.clone(), lis1_arc2);
{
let cache_data_map_mutex = client_worker.cache_data_map.lock().unwrap();
let cache_data = cache_data_map_mutex.get(group_key.as_str()).unwrap();
let listen_mutex = cache_data.listeners.lock().unwrap();
assert_eq!(0, listen_mutex.len());
}
}

#[test]
fn test_cache_data_add_listener() {
let (d, g, t) = ("D".to_string(), "G".to_string(), "N".to_string());

let mut cache_data = CacheData::new(d, g, t);

// test add listener1
let lis1_arc = Arc::new(TestConfigChangeListener1 {});
let _listen = cache_data.add_listener(lis1_arc);

// test add listener2
let lis2_arc = Arc::new(TestConfigChangeListener2 {});
let _listen = cache_data.add_listener(lis2_arc.clone());
// test add a listener2 again
let _listen = cache_data.add_listener(lis2_arc);

let listen_mutex = cache_data.listeners.lock().unwrap();
assert_eq!(2, listen_mutex.len());
}

#[test]
fn test_cache_data_add_listener_then_remove() {
let (d, g, t) = ("D".to_string(), "G".to_string(), "N".to_string());

let mut cache_data = CacheData::new(d, g, t);

// test add listener1
let lis1_arc = Arc::new(TestConfigChangeListener1 {});
let lis1_arc2 = Arc::clone(&lis1_arc);
let _listen = cache_data.add_listener(lis1_arc);

// test add listener2
let lis2_arc = Arc::new(TestConfigChangeListener2 {});
let lis2_arc2 = Arc::clone(&lis2_arc);
let _listen = cache_data.add_listener(lis2_arc);
{
let listen_mutex = cache_data.listeners.lock().unwrap();
assert_eq!(2, listen_mutex.len());
}

cache_data.remove_listener(lis1_arc2);
{
let listen_mutex = cache_data.listeners.lock().unwrap();
assert_eq!(1, listen_mutex.len());
}
cache_data.remove_listener(lis2_arc2);
{
let listen_mutex = cache_data.listeners.lock().unwrap();
assert_eq!(0, listen_mutex.len());
}
}

struct TestConfigChangeListener1;
struct TestConfigChangeListener2;

impl ConfigChangeListener for TestConfigChangeListener1 {
fn notify(&self, config_resp: ConfigResponse) {
tracing::info!(
"TestConfigChangeListener1 listen the config={:?}",
config_resp
);
}
}

impl ConfigChangeListener for TestConfigChangeListener2 {
fn notify(&self, config_resp: ConfigResponse) {
tracing::info!(
"TestConfigChangeListener2 listen the config={:?}",
config_resp
);
}
}
}