mirror of
https://github.com/eycorsican/leaf.git
synced 2026-12-05 16:02:32 +00:00
test: tests start/shutdown
This commit is contained in:
1 parent
9dcbc6e50a
commit
3773a029e8
7 files changed
+158
-8
No files matched your search
@@ -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>,
|
||||
|
||||
@@ -1008,9 +1008,13 @@ 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>
|
||||
@@ -1018,6 +1022,6 @@ 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)
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
@@ -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()];
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
+126
@@ -323,6 +323,7 @@ pub enum RuntimeOption {
|
||||
#[derive(Debug)]
|
||||
pub enum Config {
|
||||
File(String),
|
||||
Str(String),
|
||||
Internal(config::Config),
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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")]
|
||||
|
||||
Reference in new issue
Block a user