Skip to content

Commit

Permalink
Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svartalf committed Mar 9, 2020
1 parent a70a00c commit 63ba7c2
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -202,6 +202,16 @@ jobs:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zno-landing-pads"

- name: Execute integration tests (not macOS)
if: startsWith(matrix.os, 'macOS') == false
uses: actions-rs/cargo@v1
with:
command: test
args: --no-fail-fast -p tests
env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zno-landing-pads"

- name: Execute tests (macOS)
if: startsWith(matrix.os, 'macOS') == true
uses: actions-rs/cargo@v1
Expand All @@ -212,6 +222,16 @@ jobs:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Coverflow-checks=off -Zno-landing-pads"

- name: Execute integration tests (macOS)
if: startsWith(matrix.os, 'macOS') == true
uses: actions-rs/cargo@v1
with:
command: test
args: --no-fail-fast -p tests
env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Coverflow-checks=off -Zno-landing-pads"

- name: Gather coverage data
id: coverage
uses: actions-rs/grcov@v0.1
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -34,4 +34,5 @@ members = [
# Internal
"benchmarks",
"examples",
"tests",
]
15 changes: 15 additions & 0 deletions tests/Cargo.toml
@@ -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"
6 changes: 6 additions & 0 deletions tests/README.md
@@ -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.
18 changes: 18 additions & 0 deletions tests/src/lib.rs
@@ -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::*;
}
30 changes: 30 additions & 0 deletions tests/src/linux/free.rs
@@ -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())
}
3 changes: 3 additions & 0 deletions tests/src/linux/mod.rs
@@ -0,0 +1,3 @@
mod free;

pub use self::free::free;
15 changes: 15 additions & 0 deletions tests/src/macros.rs
@@ -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 added tests/src/memory.rs
Empty file.
26 changes: 26 additions & 0 deletions tests/tests/memory.rs
@@ -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(())
}

0 comments on commit 63ba7c2

Please sign in to comment.