Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global lock for tests using mongodb #238

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ console-subscriber = { version = "0.1", default-features = false, optional = tru

[dev-dependencies]
bee-test = { package = "bee-test", git = "https://github.com/iotaledger/bee.git", branch = "shimmer-develop", default-features = false }
futures = { version = "0.3", default-features = false, features = [ "executor" ] }
once_cell = { version = "1.12.0", default-features = false, features = [ "std" ] }
packable = { version = "0.3.2", default-features = false }

[features]
Expand All @@ -81,7 +83,7 @@ default = [
"metrics",
]
analytics = []
api = [
api = [
"dep:axum",
"derive_more/from",
"dep:hyper",
Expand Down
39 changes: 36 additions & 3 deletions tests/block.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::ops::Deref;

use bee_test::rand::block::{rand_block, rand_block_id};
use chronicle::{
db::{MongoDb, MongoDbConfig},
db::MongoDbConfig,
types::{
ledger::{BlockMetadata, ConflictReason, LedgerInclusionState},
stardust::block::{Block, BlockId},
},
};
use once_cell::sync::Lazy;
use packable::PackableExt;
use tokio::sync::{Mutex, MutexGuard};

static DB_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));

struct MongoDb<'a> {
db: chronicle::db::MongoDb,
_guard: MutexGuard<'a, ()>,
}

impl<'a> MongoDb<'a> {
async fn connect(config: &MongoDbConfig) -> Result<MongoDb<'a>, mongodb::error::Error> {
let _guard = DB_LOCK.lock().await;

let db = chronicle::db::MongoDb::connect(config).await?;

Ok(Self { db, _guard })
}
}

impl<'a> Deref for MongoDb<'a> {
type Target = chronicle::db::MongoDb;

fn deref(&self) -> &Self::Target {
&self.db
}
}

impl<'a> Drop for MongoDb<'a> {
fn drop(&mut self) {
futures::executor::block_on(self.db.clear()).unwrap()
}
}

#[ignore]
#[tokio::test]
Expand All @@ -35,8 +70,6 @@ async fn insert_and_get_block() -> Result<(), mongodb::error::Error> {
let config = MongoDbConfig::default().with_suffix("cargo-test");
let db = MongoDb::connect(&config).await?;

db.clear().await?;

db.insert_block_with_metadata(block_id, block, raw, metadata, 0).await?;

let result_block = db.get_block(&block_id).await?.unwrap();
Expand Down