Skip to content

Commit c42a87e

Browse files
committed
integration-tests: add test-utils feature for key-value-storage
The test cases in integration-test shares a same process, while the storage registry is a static variable thus the lifetime is the same as the whole process. We need to do clear operation everytime we do a new test item. Signed-off-by: Xynnn007 <xynnn@linux.alibaba.com>
1 parent baaebad commit c42a87e

4 files changed

Lines changed: 17 additions & 1 deletion

File tree

deps/key-value-storage/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
default = ["postgres", "redis"]
88
postgres = ["sqlx/postgres", "pg-connection-string"]
99
redis = ["dep:redis"]
10+
test-utils = []
1011

1112
[dependencies]
1213
anyhow.workspace = true

deps/key-value-storage/src/global.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ pub async fn get_namespace(namespace: &str) -> Result<KeyValueStorageInstance> {
4949
})?;
5050
Ok(storage)
5151
}
52+
53+
#[cfg(feature = "test-utils")]
54+
pub async fn clear_all_namespaces() -> Result<()> {
55+
let registry = REGISTRY.get_or_init(|| RegistryInner {
56+
storages: RwLock::new(HashMap::new()),
57+
});
58+
let mut storages = registry.storages.write().await;
59+
storages.clear();
60+
Ok(())
61+
}

integration-tests/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ edition.workspace = true
88

99
[dependencies]
1010
kbs = { path = "../kbs" }
11-
key-value-storage = { path = "../deps/key-value-storage" }
11+
key-value-storage = { path = "../deps/key-value-storage", features = [
12+
"test-utils",
13+
] }
1214
policy-engine = { path = "../deps/policy-engine" }
1315
reference-value-provider-service = { path = "../rvps" }
1416

integration-tests/src/common.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ impl TestHarness {
202202
}
203203

204204
pub async fn new(test_parameters: TestParameters) -> Result<TestHarness> {
205+
// integration-tests is a shared test environment, so we need to clear all the namespaces to avoid conflicts between tests.
206+
key_value_storage::global::clear_all_namespaces().await?;
207+
205208
let auth_keypair = PKey::generate_ed25519()?;
206209
let auth_pubkey = String::from_utf8(auth_keypair.public_key_to_pem()?)?;
207210
let auth_privkey = String::from_utf8(auth_keypair.private_key_to_pem_pkcs8()?)?;

0 commit comments

Comments
 (0)