update test code

This commit is contained in:
glyph 2021-10-30 14:46:40 +02:00
parent aeb1cbcdd4
commit f781ca86d0
2 changed files with 16 additions and 14 deletions

View File

@ -28,13 +28,13 @@ travis-ci = { repository = "peachcloud/peach-stats", branch = "master" }
maintenance = { status = "actively-developed" }
[dependencies]
env_logger = "0.6"
jsonrpc-core = "11"
jsonrpc-http-server = "11"
env_logger = "0.9"
jsonrpc-core = "18"
jsonrpc-http-server = "18"
log = "0.4"
miniserde = "0.1.15"
probes = "0.4.1"
systemstat = "0.1.10"
[dev-dependencies]
jsonrpc-test = "11"
jsonrpc-test = "18"

View File

@ -16,44 +16,46 @@ pub fn run() -> Result<(), StatError> {
info!("Creating JSON-RPC I/O handler.");
let mut io = IoHandler::default();
io.add_method("cpu_stats", move |_| {
io.add_method("cpu_stats", move |_| async {
info!("Fetching CPU statistics.");
let stats = stats::cpu_stats()?;
Ok(Value::String(stats))
});
io.add_method("cpu_stats_percent", move |_| {
io.add_method("cpu_stats_percent", move |_| async {
info!("Fetching CPU statistics as percentages.");
let stats = stats::cpu_stats_percent()?;
Ok(Value::String(stats))
});
io.add_method("disk_usage", move |_| {
io.add_method("disk_usage", move |_| async {
info!("Fetching disk usage statistics.");
let disks = stats::disk_usage()?;
Ok(Value::String(disks))
});
io.add_method("load_average", move |_| {
io.add_method("load_average", move |_| async {
info!("Fetching system load average statistics.");
let avg = stats::load_average()?;
Ok(Value::String(avg))
});
io.add_method("mem_stats", move |_| {
io.add_method("mem_stats", move |_| async {
info!("Fetching current memory statistics.");
let mem = stats::mem_stats()?;
Ok(Value::String(mem))
});
io.add_method("ping", |_| Ok(Value::String("success".to_string())));
io.add_method("ping", |_| async {
Ok(Value::String("success".to_string()))
});
io.add_method("uptime", move |_| {
io.add_method("uptime", move |_| async {
info!("Fetching system uptime.");
let uptime = stats::uptime()?;
@ -83,17 +85,17 @@ pub fn run() -> Result<(), StatError> {
#[cfg(test)]
mod tests {
use super::*;
use jsonrpc_test as test;
use jsonrpc_test as test_rpc;
// test to ensure correct success response
#[test]
fn rpc_success() {
let rpc = {
let mut io = IoHandler::new();
io.add_method("rpc_success_response", |_| {
io.add_method("rpc_success_response", |_| async {
Ok(Value::String("success".into()))
});
test::Rpc::from(io)
test_rpc::Rpc::from(io)
};
assert_eq!(rpc.request("rpc_success_response", &()), r#""success""#);