From 2e3ea2341ad8e540f10088ca4c36269b12928121 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 31 May 2022 13:21:05 -0500 Subject: [PATCH 1/3] add global lock for tests using `mongodb` --- Cargo.lock | 1 + Cargo.toml | 3 ++- tests/block.rs | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fd4229301..2cd57693e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -477,6 +477,7 @@ dependencies = [ "inx", "log", "mongodb", + "once_cell", "packable", "pin-project", "prefix-hex 0.4.0", diff --git a/Cargo.toml b/Cargo.toml index d68883713..dd1f674e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,6 +70,7 @@ 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 } packable = { version = "0.3.2", default-features = false } +once_cell = { version = "1.12.0", default-features = false, features = [ "std" ] } [features] default = [ @@ -81,7 +82,7 @@ default = [ "metrics", ] analytics = [] -api = [ +api = [ "dep:axum", "derive_more/from", "dep:hyper", diff --git a/tests/block.rs b/tests/block.rs index 0ee73750f..0d5c9abc9 100644 --- a/tests/block.rs +++ b/tests/block.rs @@ -1,15 +1,45 @@ // 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> = 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::error::Error> { + let _guard = DB_LOCK.lock().await; + + let db = chronicle::db::MongoDb::connect(config).await?; + db.clear().await?; + + Ok(Self { db, _guard }) + } +} + +impl<'a> Deref for MongoDb<'a> { + type Target = chronicle::db::MongoDb; + + fn deref(&self) -> &Self::Target { + &self.db + } +} #[ignore] #[tokio::test] @@ -35,8 +65,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(); From d89d21777679a299b84af2a658df5105f2bbb074 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 31 May 2022 13:35:44 -0500 Subject: [PATCH 2/3] reorganize dependencies --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index dd1f674e6..b78452fae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,8 +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 } -packable = { version = "0.3.2", default-features = false } once_cell = { version = "1.12.0", default-features = false, features = [ "std" ] } +packable = { version = "0.3.2", default-features = false } [features] default = [ From 51e17762b76673b1cfdd05d706838d9cb3af2aa5 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 3 Jun 2022 10:48:25 -0500 Subject: [PATCH 3/3] clear database on drop --- Cargo.lock | 1 + Cargo.toml | 1 + tests/block.rs | 7 ++++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 2cd57693e..db6acc2cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -898,6 +898,7 @@ checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e" dependencies = [ "futures-channel", "futures-core", + "futures-executor", "futures-io", "futures-sink", "futures-task", diff --git a/Cargo.toml b/Cargo.toml index b78452fae..b35afd85d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,6 +69,7 @@ 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 } diff --git a/tests/block.rs b/tests/block.rs index 0d5c9abc9..6c9ca79f5 100644 --- a/tests/block.rs +++ b/tests/block.rs @@ -27,7 +27,6 @@ impl<'a> MongoDb<'a> { let _guard = DB_LOCK.lock().await; let db = chronicle::db::MongoDb::connect(config).await?; - db.clear().await?; Ok(Self { db, _guard }) } @@ -41,6 +40,12 @@ impl<'a> Deref for MongoDb<'a> { } } +impl<'a> Drop for MongoDb<'a> { + fn drop(&mut self) { + futures::executor::block_on(self.db.clear()).unwrap() + } +} + #[ignore] #[tokio::test] async fn insert_and_get_block() -> Result<(), mongodb::error::Error> {