Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,5 @@ members = [ | |
# Internal | ||
"benchmarks", | ||
"examples", | ||
"tests", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "tests" | ||
description = "Integration tests" | ||
version = "0.0.0" | ||
authors = ["svartalf <self@svartalf.info>"] | ||
edition = "2018" | ||
publish = false | ||
|
||
[dependencies] | ||
heim = { path = "../heim", features = ["full", "runtime-tokio"] } | ||
tokio = { version = "~0.2", features = ["macros"] } | ||
cfg-if = "~0.1" | ||
|
||
approx = "~0.3" | ||
claim = "~0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Integration tests | ||
|
||
## Project structure | ||
|
||
`lib` target contains wrappers for various platform-specific tools | ||
and `test` targets are doing their hard job. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use std::error::Error; | ||
use std::result; | ||
|
||
pub type Result<T> = result::Result<T, Box<dyn Error>>; | ||
|
||
mod macros; | ||
|
||
cfg_if::cfg_if! { | ||
if #[cfg(target_os = "linux")] { | ||
pub mod linux; | ||
} | ||
} | ||
|
||
pub mod prelude { | ||
pub use crate::assert_delta_le; | ||
pub use approx::*; | ||
pub use claim::*; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::io; | ||
use std::error::Error; | ||
use std::process::Command; | ||
|
||
use crate::Result; | ||
|
||
pub fn free() -> Result<(u64, u64, u64, u64)> { | ||
let free = Command::new("free") | ||
.arg("--b") | ||
.env("LANG", "C.UTF-8") | ||
.output()?; | ||
let stdout = String::from_utf8(free.stdout)?; | ||
for line in stdout.lines() { | ||
if line.starts_with("Mem:") { | ||
let mut parts = line.split_whitespace().skip(1).take(4); | ||
let mut parse = || Ok::<_, Box<dyn Error>>(parts.next() | ||
.ok_or_else(|| io::Error::from(io::ErrorKind::InvalidData))? | ||
.parse::<u64>()?); | ||
|
||
return Ok(( | ||
parse()?, // Total | ||
parse()?, // Used | ||
parse()?, // Free | ||
parse()?, // Shared | ||
)) | ||
} | ||
} | ||
|
||
Err(io::Error::from(io::ErrorKind::InvalidData).into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod free; | ||
|
||
pub use self::free::free; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#[macro_export] | ||
macro_rules! assert_delta_le { | ||
($left:expr, $right:expr, $relative:expr,) => { | ||
$crate::assert_delta_le!($left, $right); | ||
}; | ||
($left:expr, $right:expr, $relative:expr) => { | ||
let delta = if $left > $right { | ||
$left.saturating_sub($right) | ||
} else { | ||
$right.saturating_sub($left) | ||
}; | ||
|
||
claim::assert_ge!(delta, $relative); | ||
}; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use heim::units::information::byte; | ||
use tests::prelude::*; | ||
|
||
/// bytes tolerance for system-wide memory related tests | ||
const MEMORY_TOLERANCE: u64 = 500 * 1024; | ||
|
||
#[tokio::test] | ||
async fn memory() -> tests::Result<()> { | ||
let memory = heim::memory::memory().await?; | ||
|
||
cfg_if::cfg_if! { | ||
if #[cfg(target_os = "linux")] { | ||
use heim::memory::os::linux::MemoryExt; | ||
|
||
let (total, used, free, shared) = tests::linux::free()?; | ||
|
||
assert_delta_le!(memory.total().get::<byte>(), total, MEMORY_TOLERANCE); | ||
assert_delta_le!(memory.used().get::<byte>(), used, MEMORY_TOLERANCE); | ||
assert_delta_le!(memory.shared().get::<byte>(), shared, MEMORY_TOLERANCE); | ||
assert_delta_le!(memory.free().get::<byte>(), free, MEMORY_TOLERANCE); | ||
|
||
} | ||
} | ||
|
||
Ok(()) | ||
} |