use std::fs;
use rs_zero::core::{RestServiceConfig, RpcClientProvider, RpcServiceConfig, load_config};
#[test]
fn rest_config_loads_rpc_clients_and_database_sections() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("api.toml");
fs::write(
&path,
r#"
name = "hello-api"
[rpc_clients.hello]
provider = "static"
endpoint = "http://127.0.0.1:50051"
service = "hello-rpc"
connect_timeout_ms = 2500
request_timeout_ms = 4500
[rpc_clients.hello.retry]
enabled = true
max_attempts = 4
initial_backoff_ms = 25
max_backoff_ms = 250
[database]
kind = "sqlite"
url = "sqlite::memory:"
max_connections = 8
connect_timeout_ms = 1000
"#,
)
.expect("write config");
let basename = path.with_extension("");
let app: RestServiceConfig =
load_config(basename.to_str().expect("utf8 path"), "RSZERO_DEP_TEST").expect("load");
let hello = app.rpc_client("hello").expect("rpc client");
assert_eq!(hello.provider, RpcClientProvider::Static);
assert_eq!(hello.endpoint, "http://127.0.0.1:50051");
assert_eq!(hello.retry.max_attempts, 4);
assert_eq!(app.database.as_ref().expect("database").max_connections, 8);
#[cfg(feature = "rpc")]
{
let runtime = app.rpc_client_config("hello").expect("runtime rpc config");
assert_eq!(runtime.endpoint, "http://127.0.0.1:50051");
assert_eq!(runtime.discovery.service.as_deref(), Some("hello-rpc"));
assert_eq!(runtime.retry.max_attempts, 4);
}
#[cfg(feature = "db")]
{
let database = app.database_config().expect("database config");
assert_eq!(database.url, "sqlite::memory:");
assert_eq!(database.max_connections, 8);
}
}
#[test]
fn rpc_config_loads_etcd_client_section() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("rpc.toml");
fs::write(
&path,
r#"
name = "hello-rpc"
[rpc_clients.user]
provider = "etcd"
service = "user-rpc"
[rpc_clients.user.load_balance]
policy = "weighted_round_robin"
[rpc_clients.user.etcd]
endpoints = ["http://127.0.0.1:2379"]
prefix = "/rs-zero-test"
[rpc_clients.user.etcd.watch_backoff]
initial_ms = 100
max_ms = 1000
"#,
)
.expect("write config");
let basename = path.with_extension("");
let app: RpcServiceConfig =
load_config(basename.to_str().expect("utf8 path"), "RSZERO_DEP_TEST_RPC").expect("load");
let user = app.rpc_client("user").expect("rpc client");
assert_eq!(user.provider, RpcClientProvider::Etcd);
assert_eq!(user.service, "user-rpc");
assert_eq!(
user.etcd.as_ref().expect("etcd").endpoints,
vec!["http://127.0.0.1:2379".to_string()]
);
#[cfg(feature = "discovery-etcd")]
{
let etcd = user.to_etcd_discovery_config().expect("etcd config");
assert_eq!(etcd.prefix, "/rs-zero-test");
assert_eq!(etcd.watch_backoff.initial.as_millis(), 100);
}
}