Skip to content

Commit 4b645da

Browse files
committed
Add test_utils
We add a utility function needed by upcoming `KVStore` implementation tests.
1 parent 7da08b4 commit 4b645da

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lightning-persister/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#[cfg(ldk_bench)] extern crate criterion;
1212

13+
#[cfg(test)]
14+
mod test_utils;
15+
1316
mod util;
1417

1518
extern crate lightning;

lightning-persister/src/test_utils.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use lightning::util::persist::KVStore;
2+
3+
use std::panic::RefUnwindSafe;
4+
5+
pub(crate) fn do_read_write_remove_list_persist<K: KVStore + RefUnwindSafe>(kv_store: &K) {
6+
let data = [42u8; 32];
7+
8+
let namespace = "testspace";
9+
let sub_namespace = "testsubspace";
10+
let key = "testkey";
11+
12+
// Test the basic KVStore operations.
13+
kv_store.write(namespace, sub_namespace, key, &data).unwrap();
14+
15+
// Test empty namespace/sub_namespace is allowed, but not empty namespace and non-empty
16+
// sub-namespace, and not empty key.
17+
kv_store.write("", "", key, &data).unwrap();
18+
let res = std::panic::catch_unwind(|| kv_store.write("", sub_namespace, key, &data));
19+
assert!(res.is_err());
20+
let res = std::panic::catch_unwind(|| kv_store.write(namespace, sub_namespace, "", &data));
21+
assert!(res.is_err());
22+
23+
let listed_keys = kv_store.list(namespace, sub_namespace).unwrap();
24+
assert_eq!(listed_keys.len(), 1);
25+
assert_eq!(listed_keys[0], key);
26+
27+
let read_data = kv_store.read(namespace, sub_namespace, key).unwrap();
28+
assert_eq!(data, &*read_data);
29+
30+
kv_store.remove(namespace, sub_namespace, key, false).unwrap();
31+
32+
let listed_keys = kv_store.list(namespace, sub_namespace).unwrap();
33+
assert_eq!(listed_keys.len(), 0);
34+
}

0 commit comments

Comments
 (0)