Skip to content

Commit 3c7cf02

Browse files
committed
Implement KVStore for TestSyncStore
1 parent c4648a0 commit 3c7cf02

File tree

1 file changed

+121
-16
lines changed

1 file changed

+121
-16
lines changed

tests/common/mod.rs

Lines changed: 121 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ use ldk_node::{
1919
Builder, CustomTlvRecord, Event, LightningBalance, Node, NodeError, PendingSweepBalance,
2020
};
2121

22+
use lightning::io;
2223
use lightning::ln::msgs::SocketAddress;
2324
use lightning::routing::gossip::NodeAlias;
24-
use lightning::util::persist::KVStoreSync;
25+
use lightning::util::persist::{KVStore, KVStoreSync};
2526
use lightning::util::test_utils::TestStore;
2627

2728
use lightning_invoice::{Bolt11InvoiceDescription, Description};
@@ -44,9 +45,12 @@ use rand::distributions::Alphanumeric;
4445
use rand::{thread_rng, Rng};
4546
use serde_json::{json, Value};
4647

48+
use std::boxed::Box;
4749
use std::collections::{HashMap, HashSet};
4850
use std::env;
51+
use std::future::Future;
4952
use std::path::PathBuf;
53+
use std::pin::Pin;
5054
use std::sync::{Arc, RwLock};
5155
use std::time::Duration;
5256

@@ -1198,6 +1202,76 @@ impl TestSyncStore {
11981202
}
11991203
}
12001204

1205+
impl KVStore for TestSyncStore {
1206+
fn read(
1207+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
1208+
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, io::Error>> + Send>> {
1209+
let primary_namespace = primary_namespace.to_string();
1210+
let secondary_namespace = secondary_namespace.to_string();
1211+
let key = key.to_string();
1212+
let inner = Arc::clone(&self.inner);
1213+
let fut = tokio::task::spawn_blocking(move || {
1214+
inner.read_internal(&primary_namespace, &secondary_namespace, &key)
1215+
});
1216+
Box::pin(async move {
1217+
fut.await.unwrap_or_else(|e| {
1218+
let msg = format!("Failed to IO operation due join error: {}", e);
1219+
Err(io::Error::new(io::ErrorKind::Other, msg))
1220+
})
1221+
})
1222+
}
1223+
fn write(
1224+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec<u8>,
1225+
) -> Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send>> {
1226+
let primary_namespace = primary_namespace.to_string();
1227+
let secondary_namespace = secondary_namespace.to_string();
1228+
let key = key.to_string();
1229+
let inner = Arc::clone(&self.inner);
1230+
let fut = tokio::task::spawn_blocking(move || {
1231+
inner.write_internal(&primary_namespace, &secondary_namespace, &key, buf)
1232+
});
1233+
Box::pin(async move {
1234+
fut.await.unwrap_or_else(|e| {
1235+
let msg = format!("Failed to IO operation due join error: {}", e);
1236+
Err(io::Error::new(io::ErrorKind::Other, msg))
1237+
})
1238+
})
1239+
}
1240+
fn remove(
1241+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool,
1242+
) -> Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send>> {
1243+
let primary_namespace = primary_namespace.to_string();
1244+
let secondary_namespace = secondary_namespace.to_string();
1245+
let key = key.to_string();
1246+
let inner = Arc::clone(&self.inner);
1247+
let fut = tokio::task::spawn_blocking(move || {
1248+
inner.remove_internal(&primary_namespace, &secondary_namespace, &key, lazy)
1249+
});
1250+
Box::pin(async move {
1251+
fut.await.unwrap_or_else(|e| {
1252+
let msg = format!("Failed to IO operation due join error: {}", e);
1253+
Err(io::Error::new(io::ErrorKind::Other, msg))
1254+
})
1255+
})
1256+
}
1257+
fn list(
1258+
&self, primary_namespace: &str, secondary_namespace: &str,
1259+
) -> Pin<Box<dyn Future<Output = Result<Vec<String>, io::Error>> + Send>> {
1260+
let primary_namespace = primary_namespace.to_string();
1261+
let secondary_namespace = secondary_namespace.to_string();
1262+
let inner = Arc::clone(&self.inner);
1263+
let fut = tokio::task::spawn_blocking(move || {
1264+
inner.list_internal(&primary_namespace, &secondary_namespace)
1265+
});
1266+
Box::pin(async move {
1267+
fut.await.unwrap_or_else(|e| {
1268+
let msg = format!("Failed to IO operation due join error: {}", e);
1269+
Err(io::Error::new(io::ErrorKind::Other, msg))
1270+
})
1271+
})
1272+
}
1273+
}
1274+
12011275
impl KVStoreSync for TestSyncStore {
12021276
fn read(
12031277
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
@@ -1252,9 +1326,10 @@ impl TestSyncStoreInner {
12521326
fn do_list(
12531327
&self, primary_namespace: &str, secondary_namespace: &str,
12541328
) -> lightning::io::Result<Vec<String>> {
1255-
let fs_res = self.fs_store.list(primary_namespace, secondary_namespace);
1256-
let sqlite_res = self.sqlite_store.list(primary_namespace, secondary_namespace);
1257-
let test_res = self.test_store.list(primary_namespace, secondary_namespace);
1329+
let fs_res = KVStoreSync::list(&self.fs_store, primary_namespace, secondary_namespace);
1330+
let sqlite_res =
1331+
KVStoreSync::list(&self.sqlite_store, primary_namespace, secondary_namespace);
1332+
let test_res = KVStoreSync::list(&self.test_store, primary_namespace, secondary_namespace);
12581333

12591334
match fs_res {
12601335
Ok(mut list) => {
@@ -1283,9 +1358,11 @@ impl TestSyncStoreInner {
12831358
) -> lightning::io::Result<Vec<u8>> {
12841359
let _guard = self.serializer.read().unwrap();
12851360

1286-
let fs_res = self.fs_store.read(primary_namespace, secondary_namespace, key);
1287-
let sqlite_res = self.sqlite_store.read(primary_namespace, secondary_namespace, key);
1288-
let test_res = self.test_store.read(primary_namespace, secondary_namespace, key);
1361+
let fs_res = KVStoreSync::read(&self.fs_store, primary_namespace, secondary_namespace, key);
1362+
let sqlite_res =
1363+
KVStoreSync::read(&self.sqlite_store, primary_namespace, secondary_namespace, key);
1364+
let test_res =
1365+
KVStoreSync::read(&self.test_store, primary_namespace, secondary_namespace, key);
12891366

12901367
match fs_res {
12911368
Ok(read) => {
@@ -1307,11 +1384,27 @@ impl TestSyncStoreInner {
13071384
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec<u8>,
13081385
) -> lightning::io::Result<()> {
13091386
let _guard = self.serializer.write().unwrap();
1310-
let fs_res = self.fs_store.write(primary_namespace, secondary_namespace, key, buf.clone());
1311-
let sqlite_res =
1312-
self.sqlite_store.write(primary_namespace, secondary_namespace, key, buf.clone());
1313-
let test_res =
1314-
self.test_store.write(primary_namespace, secondary_namespace, key, buf.clone());
1387+
let fs_res = KVStoreSync::write(
1388+
&self.fs_store,
1389+
primary_namespace,
1390+
secondary_namespace,
1391+
key,
1392+
buf.clone(),
1393+
);
1394+
let sqlite_res = KVStoreSync::write(
1395+
&self.sqlite_store,
1396+
primary_namespace,
1397+
secondary_namespace,
1398+
key,
1399+
buf.clone(),
1400+
);
1401+
let test_res = KVStoreSync::write(
1402+
&self.test_store,
1403+
primary_namespace,
1404+
secondary_namespace,
1405+
key,
1406+
buf.clone(),
1407+
);
13151408

13161409
assert!(self
13171410
.do_list(primary_namespace, secondary_namespace)
@@ -1336,10 +1429,22 @@ impl TestSyncStoreInner {
13361429
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool,
13371430
) -> lightning::io::Result<()> {
13381431
let _guard = self.serializer.write().unwrap();
1339-
let fs_res = self.fs_store.remove(primary_namespace, secondary_namespace, key, lazy);
1340-
let sqlite_res =
1341-
self.sqlite_store.remove(primary_namespace, secondary_namespace, key, lazy);
1342-
let test_res = self.test_store.remove(primary_namespace, secondary_namespace, key, lazy);
1432+
let fs_res =
1433+
KVStoreSync::remove(&self.fs_store, primary_namespace, secondary_namespace, key, lazy);
1434+
let sqlite_res = KVStoreSync::remove(
1435+
&self.sqlite_store,
1436+
primary_namespace,
1437+
secondary_namespace,
1438+
key,
1439+
lazy,
1440+
);
1441+
let test_res = KVStoreSync::remove(
1442+
&self.test_store,
1443+
primary_namespace,
1444+
secondary_namespace,
1445+
key,
1446+
lazy,
1447+
);
13431448

13441449
assert!(!self
13451450
.do_list(primary_namespace, secondary_namespace)

0 commit comments

Comments
 (0)