Skip to content

Commit e3ae2b2

Browse files
committed
Implement KVStore for TestSyncStore
1 parent 10380d3 commit e3ae2b2

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

@@ -1207,6 +1211,76 @@ impl TestSyncStore {
12071211
}
12081212
}
12091213

1214+
impl KVStore for TestSyncStore {
1215+
fn read(
1216+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
1217+
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, io::Error>> + Send>> {
1218+
let primary_namespace = primary_namespace.to_string();
1219+
let secondary_namespace = secondary_namespace.to_string();
1220+
let key = key.to_string();
1221+
let inner = Arc::clone(&self.inner);
1222+
let fut = tokio::task::spawn_blocking(move || {
1223+
inner.read_internal(&primary_namespace, &secondary_namespace, &key)
1224+
});
1225+
Box::pin(async move {
1226+
fut.await.unwrap_or_else(|e| {
1227+
let msg = format!("Failed to IO operation due join error: {}", e);
1228+
Err(io::Error::new(io::ErrorKind::Other, msg))
1229+
})
1230+
})
1231+
}
1232+
fn write(
1233+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec<u8>,
1234+
) -> Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send>> {
1235+
let primary_namespace = primary_namespace.to_string();
1236+
let secondary_namespace = secondary_namespace.to_string();
1237+
let key = key.to_string();
1238+
let inner = Arc::clone(&self.inner);
1239+
let fut = tokio::task::spawn_blocking(move || {
1240+
inner.write_internal(&primary_namespace, &secondary_namespace, &key, buf)
1241+
});
1242+
Box::pin(async move {
1243+
fut.await.unwrap_or_else(|e| {
1244+
let msg = format!("Failed to IO operation due join error: {}", e);
1245+
Err(io::Error::new(io::ErrorKind::Other, msg))
1246+
})
1247+
})
1248+
}
1249+
fn remove(
1250+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool,
1251+
) -> Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send>> {
1252+
let primary_namespace = primary_namespace.to_string();
1253+
let secondary_namespace = secondary_namespace.to_string();
1254+
let key = key.to_string();
1255+
let inner = Arc::clone(&self.inner);
1256+
let fut = tokio::task::spawn_blocking(move || {
1257+
inner.remove_internal(&primary_namespace, &secondary_namespace, &key, lazy)
1258+
});
1259+
Box::pin(async move {
1260+
fut.await.unwrap_or_else(|e| {
1261+
let msg = format!("Failed to IO operation due join error: {}", e);
1262+
Err(io::Error::new(io::ErrorKind::Other, msg))
1263+
})
1264+
})
1265+
}
1266+
fn list(
1267+
&self, primary_namespace: &str, secondary_namespace: &str,
1268+
) -> Pin<Box<dyn Future<Output = Result<Vec<String>, io::Error>> + Send>> {
1269+
let primary_namespace = primary_namespace.to_string();
1270+
let secondary_namespace = secondary_namespace.to_string();
1271+
let inner = Arc::clone(&self.inner);
1272+
let fut = tokio::task::spawn_blocking(move || {
1273+
inner.list_internal(&primary_namespace, &secondary_namespace)
1274+
});
1275+
Box::pin(async move {
1276+
fut.await.unwrap_or_else(|e| {
1277+
let msg = format!("Failed to IO operation due join error: {}", e);
1278+
Err(io::Error::new(io::ErrorKind::Other, msg))
1279+
})
1280+
})
1281+
}
1282+
}
1283+
12101284
impl KVStoreSync for TestSyncStore {
12111285
fn read(
12121286
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
@@ -1261,9 +1335,10 @@ impl TestSyncStoreInner {
12611335
fn do_list(
12621336
&self, primary_namespace: &str, secondary_namespace: &str,
12631337
) -> lightning::io::Result<Vec<String>> {
1264-
let fs_res = self.fs_store.list(primary_namespace, secondary_namespace);
1265-
let sqlite_res = self.sqlite_store.list(primary_namespace, secondary_namespace);
1266-
let test_res = self.test_store.list(primary_namespace, secondary_namespace);
1338+
let fs_res = KVStoreSync::list(&self.fs_store, primary_namespace, secondary_namespace);
1339+
let sqlite_res =
1340+
KVStoreSync::list(&self.sqlite_store, primary_namespace, secondary_namespace);
1341+
let test_res = KVStoreSync::list(&self.test_store, primary_namespace, secondary_namespace);
12671342

12681343
match fs_res {
12691344
Ok(mut list) => {
@@ -1292,9 +1367,11 @@ impl TestSyncStoreInner {
12921367
) -> lightning::io::Result<Vec<u8>> {
12931368
let _guard = self.serializer.read().unwrap();
12941369

1295-
let fs_res = self.fs_store.read(primary_namespace, secondary_namespace, key);
1296-
let sqlite_res = self.sqlite_store.read(primary_namespace, secondary_namespace, key);
1297-
let test_res = self.test_store.read(primary_namespace, secondary_namespace, key);
1370+
let fs_res = KVStoreSync::read(&self.fs_store, primary_namespace, secondary_namespace, key);
1371+
let sqlite_res =
1372+
KVStoreSync::read(&self.sqlite_store, primary_namespace, secondary_namespace, key);
1373+
let test_res =
1374+
KVStoreSync::read(&self.test_store, primary_namespace, secondary_namespace, key);
12981375

12991376
match fs_res {
13001377
Ok(read) => {
@@ -1316,11 +1393,27 @@ impl TestSyncStoreInner {
13161393
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec<u8>,
13171394
) -> lightning::io::Result<()> {
13181395
let _guard = self.serializer.write().unwrap();
1319-
let fs_res = self.fs_store.write(primary_namespace, secondary_namespace, key, buf.clone());
1320-
let sqlite_res =
1321-
self.sqlite_store.write(primary_namespace, secondary_namespace, key, buf.clone());
1322-
let test_res =
1323-
self.test_store.write(primary_namespace, secondary_namespace, key, buf.clone());
1396+
let fs_res = KVStoreSync::write(
1397+
&self.fs_store,
1398+
primary_namespace,
1399+
secondary_namespace,
1400+
key,
1401+
buf.clone(),
1402+
);
1403+
let sqlite_res = KVStoreSync::write(
1404+
&self.sqlite_store,
1405+
primary_namespace,
1406+
secondary_namespace,
1407+
key,
1408+
buf.clone(),
1409+
);
1410+
let test_res = KVStoreSync::write(
1411+
&self.test_store,
1412+
primary_namespace,
1413+
secondary_namespace,
1414+
key,
1415+
buf.clone(),
1416+
);
13241417

13251418
assert!(self
13261419
.do_list(primary_namespace, secondary_namespace)
@@ -1345,10 +1438,22 @@ impl TestSyncStoreInner {
13451438
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool,
13461439
) -> lightning::io::Result<()> {
13471440
let _guard = self.serializer.write().unwrap();
1348-
let fs_res = self.fs_store.remove(primary_namespace, secondary_namespace, key, lazy);
1349-
let sqlite_res =
1350-
self.sqlite_store.remove(primary_namespace, secondary_namespace, key, lazy);
1351-
let test_res = self.test_store.remove(primary_namespace, secondary_namespace, key, lazy);
1441+
let fs_res =
1442+
KVStoreSync::remove(&self.fs_store, primary_namespace, secondary_namespace, key, lazy);
1443+
let sqlite_res = KVStoreSync::remove(
1444+
&self.sqlite_store,
1445+
primary_namespace,
1446+
secondary_namespace,
1447+
key,
1448+
lazy,
1449+
);
1450+
let test_res = KVStoreSync::remove(
1451+
&self.test_store,
1452+
primary_namespace,
1453+
secondary_namespace,
1454+
key,
1455+
lazy,
1456+
);
13521457

13531458
assert!(!self
13541459
.do_list(primary_namespace, secondary_namespace)

0 commit comments

Comments
 (0)