Skip to content

Commit c1e2d06

Browse files
committed
Implement KVStore for TestSyncStore
1 parent b2d9ede commit c1e2d06

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

@@ -1199,6 +1203,76 @@ impl TestSyncStore {
11991203
}
12001204
}
12011205

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

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

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

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

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

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

0 commit comments

Comments
 (0)