Skip to content

Commit

Permalink
test: tests start/shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
eycorsican committed Aug 27, 2021
1 parent 9dcbc6e commit 3773a02
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 8 deletions.
6 changes: 6 additions & 0 deletions leaf/src/config/conf/config.rs
Expand Up @@ -1170,6 +1170,12 @@ pub fn to_internal(conf: &mut Config) -> Result<internal::Config> {
Ok(config)
}

pub fn from_string(s: &str) -> Result<internal::Config> {
let lines = s.lines().map(|s| Ok(s.to_string())).collect();
let mut config = from_lines(lines)?;
to_internal(&mut config)
}

pub fn from_file<P>(path: P) -> Result<internal::Config>
where
P: AsRef<Path>,
Expand Down
12 changes: 8 additions & 4 deletions leaf/src/config/json/config.rs
Expand Up @@ -1008,16 +1008,20 @@ pub fn to_internal(json: &mut Config) -> Result<internal::Config> {
Ok(config)
}

pub fn from_string(config: String) -> Result<Config> {
serde_json::from_str(config.as_str())
.map_err(|e| anyhow!("deserialize json config failed: {}", e))
pub fn json_from_string(config: &str) -> Result<Config> {
serde_json::from_str(config).map_err(|e| anyhow!("deserialize json config failed: {}", e))
}

pub fn from_string(s: &str) -> Result<internal::Config> {
let mut config = json_from_string(s)?;
to_internal(&mut config)
}

pub fn from_file<P>(path: P) -> Result<internal::Config>
where
P: AsRef<Path>,
{
let config = std::fs::read_to_string(path)?;
let mut config = from_string(config)?;
let mut config = json_from_string(&config)?;
to_internal(&mut config)
}
2 changes: 1 addition & 1 deletion leaf/src/config/json/tests/test_config.rs
Expand Up @@ -88,5 +88,5 @@ fn test_config() {
}
"#;

assert!(crate::config::json::from_string(json_str.to_string()).is_ok());
assert!(crate::config::json::json_from_string(json_str).is_ok());
}
2 changes: 1 addition & 1 deletion leaf/src/config/json/tests/test_dns.rs
Expand Up @@ -12,7 +12,7 @@ fn test_dns_hosts() {
}
}
"#;
let mut config = crate::config::json::from_string(json_str.to_string()).unwrap();
let mut config = crate::config::json::json_from_string(json_str).unwrap();
let hosts = config.dns.as_ref().unwrap().hosts.as_ref().unwrap();
let ips = vec!["192.168.0.1".to_string(), "192.168.0.2".to_string()];

Expand Down
15 changes: 15 additions & 0 deletions leaf/src/config/mod.rs
Expand Up @@ -15,6 +15,21 @@ pub mod conf;

pub use internal::*;

pub fn from_string(s: &str) -> Result<internal::Config> {
#[cfg(feature = "config-json")]
{
if let Ok(c) = json::from_string(s) {
return Ok(c);
}
}
#[cfg(feature = "config-conf")]
{
return conf::from_string(s);
}
#[allow(unreachable_code)]
Err(anyhow!("could not load config from:\n{:?}", s))
}

pub fn from_file(path: &str) -> Result<internal::Config> {
if let Some(ext) = Path::new(path).extension() {
if let Some(ext) = ext.to_str() {
Expand Down
126 changes: 126 additions & 0 deletions leaf/src/lib.rs
Expand Up @@ -323,6 +323,7 @@ pub enum RuntimeOption {
#[derive(Debug)]
pub enum Config {
File(String),
Str(String),
Internal(config::Config),
}

Expand Down Expand Up @@ -350,6 +351,7 @@ pub fn start(rt_id: RuntimeId, opts: StartOptions) -> Result<(), Error> {

let mut config = match opts.config {
Config::File(p) => config::from_file(&p).map_err(Error::Config)?,
Config::Str(s) => config::from_string(&s).map_err(Error::Config)?,
Config::Internal(c) => c,
};

Expand Down Expand Up @@ -532,3 +534,127 @@ pub fn start(rt_id: RuntimeId, opts: StartOptions) -> Result<(), Error> {

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use std::thread;

#[test]
fn test_restart() {
let conf = r#"
[General]
logllevel = trace
dns-server = 1.1.1.1
socks-interface = 127.0.0.1
socks-port = 1080
[Proxy]
Direct = direct
"#;

thread::spawn(move || {
let opts = StartOptions {
config: Config::Str(conf.to_string()),
#[cfg(feature = "auto-reload")]
auto_reload: false,
runtime_opt: RuntimeOption::SingleThread,
};
start(0, opts);
});
thread::sleep(std::time::Duration::from_secs(2));
shutdown(0);
thread::sleep(std::time::Duration::from_secs(2));

thread::spawn(move || {
let opts = StartOptions {
config: Config::Str(conf.to_string()),
#[cfg(feature = "auto-reload")]
auto_reload: false,
runtime_opt: RuntimeOption::SingleThread,
};
start(0, opts);
});
thread::sleep(std::time::Duration::from_secs(2));
shutdown(0);
thread::sleep(std::time::Duration::from_secs(2));

thread::spawn(move || {
let opts = StartOptions {
config: Config::Str(conf.to_string()),
#[cfg(feature = "auto-reload")]
auto_reload: false,
runtime_opt: RuntimeOption::SingleThread,
};
start(0, opts);
});
thread::sleep(std::time::Duration::from_secs(2));
shutdown(0);
thread::sleep(std::time::Duration::from_secs(2));

thread::spawn(move || {
let opts = StartOptions {
config: Config::Str(conf.to_string()),
#[cfg(feature = "auto-reload")]
auto_reload: false,
runtime_opt: RuntimeOption::SingleThread,
};
start(0, opts);
});
thread::sleep(std::time::Duration::from_secs(2));
shutdown(0);
thread::sleep(std::time::Duration::from_secs(2));

thread::spawn(move || {
let opts = StartOptions {
config: Config::Str(conf.to_string()),
#[cfg(feature = "auto-reload")]
auto_reload: false,
runtime_opt: RuntimeOption::SingleThread,
};
start(0, opts);
});
thread::sleep(std::time::Duration::from_secs(2));
shutdown(0);
thread::sleep(std::time::Duration::from_secs(2));

thread::spawn(move || {
let opts = StartOptions {
config: Config::Str(conf.to_string()),
#[cfg(feature = "auto-reload")]
auto_reload: false,
runtime_opt: RuntimeOption::SingleThread,
};
start(0, opts);
});
thread::sleep(std::time::Duration::from_secs(2));
shutdown(0);
thread::sleep(std::time::Duration::from_secs(2));

thread::spawn(move || {
let opts = StartOptions {
config: Config::Str(conf.to_string()),
#[cfg(feature = "auto-reload")]
auto_reload: false,
runtime_opt: RuntimeOption::SingleThread,
};
start(0, opts);
});
thread::sleep(std::time::Duration::from_secs(2));
shutdown(0);
thread::sleep(std::time::Duration::from_secs(2));

thread::spawn(move || {
let opts = StartOptions {
config: Config::Str(conf.to_string()),
#[cfg(feature = "auto-reload")]
auto_reload: false,
runtime_opt: RuntimeOption::SingleThread,
};
start(0, opts);
});
thread::sleep(std::time::Duration::from_secs(2));
shutdown(0);
thread::sleep(std::time::Duration::from_secs(2));
}
}
3 changes: 1 addition & 2 deletions leaf/tests/common.rs
Expand Up @@ -51,8 +51,7 @@ pub fn run_leaf_instances(
let mut leaf_rt_ids = Vec::new();
let mut rt_id = 0;
for config in configs {
let mut config = leaf::config::json::from_string(config).unwrap();
let config = leaf::config::json::to_internal(&mut config).unwrap();
let config = leaf::config::json::from_string(&config).unwrap();
let opts = leaf::StartOptions {
config: leaf::Config::Internal(config),
#[cfg(feature = "auto-reload")]
Expand Down

0 comments on commit 3773a02

Please sign in to comment.