Skip to content

Commit

Permalink
Add lock to the test framework
Browse files Browse the repository at this point in the history
Signed-off-by: MOZGIII <[email protected]>
  • Loading branch information
MOZGIII committed Jun 12, 2020
1 parent efa05a0 commit 4c684ad
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/kubernetes-test-framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ description = "Kubernetes Test Framework used to test Vector in Kubernetes"
k8s-openapi = { version = "0.7", default-features = false, features = ["v1_15"] }
serde_json = "1"
tempfile = "3"
once_cell = "1"
2 changes: 2 additions & 0 deletions lib/kubernetes-test-framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

pub mod framework;
pub mod interface;
mod lock;
pub mod log_lookup;
pub mod namespace;
mod resource_file;
Expand All @@ -30,5 +31,6 @@ pub mod wait_for_rollout;

pub use framework::Framework;
pub use interface::Interface;
pub use lock::lock;

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
15 changes: 15 additions & 0 deletions lib/kubernetes-test-framework/src/lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use once_cell::sync::OnceCell;
use std::sync::{Mutex, MutexGuard};

/// A shared lock to use commonly among the tests.
/// The goal is to guranatee that only one test is executing concurrently, since
/// tests use a shared resource - a k8s cluster - and will conflict with each
/// other unless they're executing sequentially.
pub fn lock() -> MutexGuard<'static, ()> {
static INSTANCE: OnceCell<Mutex<()>> = OnceCell::new();
match INSTANCE.get_or_init(|| Mutex::new(())).lock() {
Ok(guard) => guard,
// Ignore poison error.
Err(err) => err.into_inner(),
}
}

0 comments on commit 4c684ad

Please sign in to comment.