Skip to content

Commit 2253576

Browse files
committed
revise tests to use TestStore, though they break now
1 parent d752f90 commit 2253576

File tree

2 files changed

+17
-81
lines changed

2 files changed

+17
-81
lines changed

lightning/src/util/persist.rs

Lines changed: 5 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -574,104 +574,28 @@ mod tests {
574574
use crate::chain::ChannelMonitorUpdateStatus;
575575
use crate::events::{ClosureReason, MessageSendEventsProvider};
576576
use crate::ln::functional_test_utils::*;
577-
use crate::util::test_utils::{self, TestLogger};
577+
use crate::util::test_utils::{self, TestLogger, TestStore};
578578
use crate::{check_added_monitors, check_closed_broadcast};
579-
//use rand::{distributions::Alphanumeric, thread_rng, Rng};
580-
use std::{collections::HashMap, io::Cursor, sync::Mutex};
581579

582580
// =================================
583581
// TEST UTILITIES
584582
// =================================
585583

586-
struct HashMapKVStore {
587-
storage: Mutex<HashMap<String, Vec<u8>>>,
588-
read_only: bool,
589-
}
590-
591-
impl HashMapKVStore {
592-
fn new(read_only: bool) -> Self {
593-
Self {
594-
storage: Mutex::new(HashMap::new()),
595-
read_only,
596-
}
597-
}
598-
}
599-
600-
impl KVStore for HashMapKVStore {
601-
type Reader = Cursor<Vec<u8>>;
602-
603-
fn read(&self, namespace: &str, key: &str) -> io::Result<Self::Reader> {
604-
let key = [namespace, key].join("/");
605-
let storage = self.storage.lock().unwrap();
606-
let value = storage.get(&key);
607-
match value {
608-
Some(vec) => Ok(Cursor::new(vec.clone())),
609-
None => Err(std::io::Error::new(
610-
std::io::ErrorKind::NotFound,
611-
"not found",
612-
)),
613-
}
614-
}
615-
616-
fn write(&self, namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> {
617-
if self.read_only {
618-
return Err(std::io::Error::new(
619-
std::io::ErrorKind::PermissionDenied,
620-
"read only",
621-
));
622-
}
623-
let key = [namespace, key].join("/");
624-
let mut storage = self.storage.lock().unwrap();
625-
let value = buf.to_vec();
626-
storage.insert(key, value);
627-
Ok(())
628-
}
629-
630-
fn remove(&self, namespace: &str, key: &str) -> io::Result<()> {
631-
if self.read_only {
632-
return Err(std::io::Error::new(
633-
std::io::ErrorKind::PermissionDenied,
634-
"read only",
635-
));
636-
}
637-
let key = [namespace, key].join("/");
638-
let mut storage = self.storage.lock().unwrap();
639-
storage.remove(&key);
640-
Ok(())
641-
}
642-
643-
fn list(&self, namespace: &str) -> io::Result<Vec<String>> {
644-
let ns = [namespace, "/"].join("");
645-
let storage = self.storage.lock().unwrap();
646-
Ok(storage
647-
.keys()
648-
.filter_map(|k| k.starts_with(&ns).then(|| k.replacen(&ns, "", 1)))
649-
.collect())
650-
}
651-
}
652-
653-
fn default_test_persister() -> KVStoreUpdatingPersister<Box<HashMapKVStore>, Box<TestLogger>> {
584+
fn default_test_persister() -> KVStoreUpdatingPersister<Box<TestStore>, Box<TestLogger>> {
654585
KVStoreUpdatingPersister {
655-
kv: Box::new(HashMapKVStore::new(false)),
586+
kv: Box::new(TestStore::new(false)),
656587
logger: Box::new(TestLogger::new()),
657588
}
658589
}
659590

660-
fn read_only_test_persister() -> KVStoreUpdatingPersister<Box<HashMapKVStore>, Box<TestLogger>>
591+
fn read_only_test_persister() -> KVStoreUpdatingPersister<Box<TestStore>, Box<TestLogger>>
661592
{
662593
KVStoreUpdatingPersister {
663-
kv: Box::new(HashMapKVStore::new(true)),
594+
kv: Box::new(TestStore::new(false)),
664595
logger: Box::new(TestLogger::new()),
665596
}
666597
}
667598

668-
#[test]
669-
fn test_read_only_persister_actually_is_ro() {
670-
let persister = read_only_test_persister();
671-
assert!(persister.kv.write("namespace", "key", &[0]).is_err());
672-
assert!(persister.kv.remove("namespace", "key",).is_err());
673-
}
674-
675599
// =================================
676600
// TESTS
677601
// =================================

lightning/src/util/test_utils.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,12 @@ impl KVStore for TestStore {
358358
}
359359

360360
fn write(&self, namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> {
361+
if self.read_only {
362+
return Err(std::io::Error::new(
363+
std::io::ErrorKind::PermissionDenied,
364+
"read only",
365+
));
366+
}
361367
if self.read_only {
362368
return Err(io::Error::new(
363369
io::ErrorKind::PermissionDenied,
@@ -374,6 +380,12 @@ impl KVStore for TestStore {
374380
}
375381

376382
fn remove(&self, namespace: &str, key: &str) -> io::Result<()> {
383+
if self.read_only {
384+
return Err(std::io::Error::new(
385+
std::io::ErrorKind::PermissionDenied,
386+
"read only",
387+
));
388+
}
377389
if self.read_only {
378390
return Err(io::Error::new(
379391
io::ErrorKind::PermissionDenied,

0 commit comments

Comments
 (0)