Repository: tikv/tikv
File: src/server/config.rs (Rust)

Code before:
    if LABEL_VALUE_FORMAT.is_match(s) {
        Ok(())
    } else {
        Err(box_err!(
            "store label value: {:?} not match {}",
            s,
            *LABEL_VALUE_FORMAT
        ))
    }
}

#[cfg(test)]
mod tests {
    use tikv_util::config::ReadableDuration;

    use super::*;

    #[test]
    fn test_config_validate() {
        let mut cfg = Config::default();
        assert!(cfg.advertise_addr.is_empty());
        assert!(cfg.advertise_status_addr.is_empty());
        cfg.validate().unwrap();
        assert_eq!(cfg.addr, cfg.advertise_addr);
        assert_eq!(cfg.status_addr, cfg.advertise_status_addr);
        let base_num = (SysQuota::cpu_cores_quota().div(8.0).floor() as usize).clamp(1, 4);
        assert_eq!(cfg.grpc_raft_conn_num, base_num);
        assert_eq!(cfg.grpc_concurrency, base_num * 3 + 2);

        let mut invalid_cfg = cfg.clone();
        invalid_cfg.concurrent_send_snap_limit = 0;
        invalid_cfg.validate().unwrap_err();

        let mut invalid_cfg = cfg.clone();
        invalid_cfg.concurrent_recv_snap_limit = 0;
        invalid_cfg.validate().unwrap_err();

        let mut invalid_cfg = cfg.clone();
        invalid_cfg.end_point_recursion_limit = 0;
        invalid_cfg.validate().unwrap_err();

        let mut invalid_cfg = cfg.clone();
        invalid_cfg.grpc_memory_pool_quota = ReadableSize::mb(0);
        invalid_cfg.validate().unwrap_err();

        let mut invalid_cfg = cfg.clone();
        invalid_cfg.end_point_request_max_handle_duration = Some(ReadableDuration::secs(0));
        invalid_cfg.validate().unwrap_err();

        invalid_cfg = Config::default();
        invalid_cfg.addr = "0.0.0.0:1000".to_owned();

Review comment (refactor):
This calculation duplicates the logic from the lazy_static block. Consider extracting a helper function to avoid code duplication and ensure consistency.
```suggestion
        let base_num = calculate_base_num();
```

Code after:
        Regex::new("^[$]?[A-Za-z0-9]([-A-Za-z0-9_./]*[A-Za-z0-9])?$").unwrap();
    static ref LABEL_VALUE_FORMAT: Regex = Regex::new("^[-A-Za-z0-9_./]*$").unwrap();
}

fn validate_label_key(s: &str) -> Result<()> {
    if LABEL_KEY_FORMAT.is_match(s) {
        Ok(())
    } else {
        Err(box_err!(
            "store label key: {:?} not match {}",
            s,
            *LABEL_KEY_FORMAT
        ))
    }
}

fn validate_label_value(s: &str) -> Result<()> {
    if LABEL_VALUE_FORMAT.is_match(s) {
        Ok(())
    } else {
        Err(box_err!(
            "store label value: {:?} not match {}",
            s,
            *LABEL_VALUE_FORMAT
        ))
    }
}

#[cfg(test)]
mod tests {
    use tikv_util::config::ReadableDuration;

    use super::*;

    #[test]
    fn test_config_validate() {
        let mut cfg = Config::default();
        assert!(cfg.advertise_addr.is_empty());
        assert!(cfg.advertise_status_addr.is_empty());
        cfg.validate().unwrap();
        assert_eq!(cfg.addr, cfg.advertise_addr);
        assert_eq!(cfg.status_addr, cfg.advertise_status_addr);
        let base_num = calculate_cpu_quota_base_num().clamp(1, 4);
        assert_eq!(cfg.grpc_raft_conn_num, base_num);
        assert_eq!(cfg.grpc_concurrency, base_num * 3 + 2);

        let mut invalid_cfg = cfg.clone();
        invalid_cfg.concurrent_send_snap_limit = 0;
        invalid_cfg.validate().unwrap_err();

        let mut invalid_cfg = cfg.clone();