Skip to content

Commit

Permalink
clippy: static_mut_refs (solana-labs#4401)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored Jan 10, 2025
1 parent b650e17 commit f07a570
Showing 1 changed file with 33 additions and 58 deletions.
91 changes: 33 additions & 58 deletions metrics/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,14 @@ mod tests {
serial_test::serial,
std::{
env,
sync::{atomic::Ordering, Once, RwLock},
sync::{atomic::Ordering, LazyLock, RwLock},
},
};

fn get_env_lock() -> &'static RwLock<()> {
static mut ENV_LOCK: Option<RwLock<()>> = None;
static INIT_HOOK: Once = Once::new();
static ENV_LOCK: LazyLock<RwLock<()>> = LazyLock::new(|| RwLock::new(()));

unsafe {
INIT_HOOK.call_once(|| {
ENV_LOCK = Some(RwLock::new(()));
});
ENV_LOCK.as_ref().unwrap()
}
&ENV_LOCK
}

/// Try to initialize the logger with a filter level of INFO.
Expand All @@ -251,29 +245,20 @@ mod tests {
fn test_counter() {
try_init_logger_at_level_info().ok();
let _readlock = get_env_lock().read();
static mut COUNTER: Counter = create_counter!("test", 1000, 1);
unsafe {
COUNTER.init();
}
let count = 1;
inc_counter!(COUNTER, Level::Info, count);
unsafe {
assert_eq!(COUNTER.counts.load(Ordering::Relaxed), 1);
assert_eq!(COUNTER.times.load(Ordering::Relaxed), 1);
assert_eq!(COUNTER.lograte.load(Ordering::Relaxed), 1000);
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 0);
assert_eq!(COUNTER.name, "test");
}
let mut counter = create_counter!("test", 1000, 1);
counter.init();
counter.inc(Level::Info, 1);
assert_eq!(counter.counts.load(Ordering::Relaxed), 1);
assert_eq!(counter.times.load(Ordering::Relaxed), 1);
assert_eq!(counter.lograte.load(Ordering::Relaxed), 1000);
assert_eq!(counter.lastlog.load(Ordering::Relaxed), 0);
assert_eq!(counter.name, "test");
for _ in 0..199 {
inc_counter!(COUNTER, Level::Info, 2);
}
unsafe {
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 397);
}
inc_counter!(COUNTER, Level::Info, 2);
unsafe {
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 399);
counter.inc(Level::Info, 2);
}
assert_eq!(counter.lastlog.load(Ordering::Relaxed), 397);
counter.inc(Level::Info, 2);
assert_eq!(counter.lastlog.load(Ordering::Relaxed), 399);
}

#[test]
Expand All @@ -282,14 +267,12 @@ mod tests {
try_init_logger_at_level_info().ok();
let _readlock = get_env_lock().read();
env::remove_var("SOLANA_DEFAULT_METRICS_RATE");
static mut COUNTER: Counter = create_counter!("test", 1000, 0);
unsafe {
COUNTER.init();
assert_eq!(
COUNTER.metricsrate.load(Ordering::Relaxed),
DEFAULT_METRICS_RATE
);
}
let mut counter = create_counter!("test", 1000, 0);
counter.init();
assert_eq!(
counter.metricsrate.load(Ordering::Relaxed),
DEFAULT_METRICS_RATE
);
}

#[test]
Expand All @@ -298,11 +281,9 @@ mod tests {
try_init_logger_at_level_info().ok();
let _writelock = get_env_lock().write();
env::set_var("SOLANA_DEFAULT_METRICS_RATE", "50");
static mut COUNTER: Counter = create_counter!("test", 1000, 0);
unsafe {
COUNTER.init();
assert_eq!(COUNTER.metricsrate.load(Ordering::Relaxed), 50);
}
let mut counter = create_counter!("test", 1000, 0);
counter.init();
assert_eq!(counter.metricsrate.load(Ordering::Relaxed), 50);
}

#[test]
Expand All @@ -328,11 +309,9 @@ mod tests {
Counter::default_log_rate(),
DEFAULT_LOG_RATE,
);
static mut COUNTER: Counter = create_counter!("test_lograte", 0, 1);
unsafe {
COUNTER.init();
assert_eq!(COUNTER.lograte.load(Ordering::Relaxed), DEFAULT_LOG_RATE);
}
let mut counter = create_counter!("test_lograte", 0, 1);
counter.init();
assert_eq!(counter.lograte.load(Ordering::Relaxed), DEFAULT_LOG_RATE);
}

#[test]
Expand All @@ -341,18 +320,14 @@ mod tests {
try_init_logger_at_level_info().ok();
assert_ne!(DEFAULT_LOG_RATE, 0);
let _writelock = get_env_lock().write();
static mut COUNTER: Counter = create_counter!("test_lograte_env", 0, 1);
let mut counter = create_counter!("test_lograte_env", 0, 1);
env::set_var("SOLANA_DEFAULT_LOG_RATE", "50");
unsafe {
COUNTER.init();
assert_eq!(COUNTER.lograte.load(Ordering::Relaxed), 50);
}
counter.init();
assert_eq!(counter.lograte.load(Ordering::Relaxed), 50);

static mut COUNTER2: Counter = create_counter!("test_lograte_env", 0, 1);
let mut counter2 = create_counter!("test_lograte_env", 0, 1);
env::set_var("SOLANA_DEFAULT_LOG_RATE", "0");
unsafe {
COUNTER2.init();
assert_eq!(COUNTER2.lograte.load(Ordering::Relaxed), DEFAULT_LOG_RATE);
}
counter2.init();
assert_eq!(counter2.lograte.load(Ordering::Relaxed), DEFAULT_LOG_RATE);
}
}

0 comments on commit f07a570

Please sign in to comment.