refactor(storage-server): add SectorId, FileName, PackedPath newtypes, eliminate raw primitives and DRY violations
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
use crate::adapters::mock_fs::MockFileSystem;
|
||||
use crate::filesystem::FileHandle;
|
||||
use crate::protocol::*;
|
||||
use crate::server::StorageServer;
|
||||
use server_framework::Server;
|
||||
@@ -8,42 +7,33 @@ fn test_server() -> StorageServer<MockFileSystem> {
|
||||
StorageServer::new(MockFileSystem::new())
|
||||
}
|
||||
|
||||
fn path_words(s: &[u8]) -> (usize, usize) {
|
||||
let mut w0 = [0u8; 8];
|
||||
let mut w1 = [0u8; 8];
|
||||
for (i, &b) in s.iter().enumerate() {
|
||||
if i < 8 { w0[i] = b; }
|
||||
else if i < 16 { w1[i - 8] = b; }
|
||||
}
|
||||
(usize::from_le_bytes(w0), usize::from_le_bytes(w1))
|
||||
fn path(s: &[u8]) -> PackedPath {
|
||||
PackedPath::from_bytes(s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_and_open() {
|
||||
let mut server = test_server();
|
||||
let (w0, w1) = path_words(b"test.txt");
|
||||
|
||||
let resp = server.handle(StorageRequest::Create { path_word0: w0, path_word1: w1 });
|
||||
let resp = server.handle(StorageRequest::Create { path: path(b"test.txt") });
|
||||
assert!(matches!(resp, StorageResponse::Handle(_)));
|
||||
|
||||
let resp = server.handle(StorageRequest::Open { path_word0: w0, path_word1: w1 });
|
||||
let resp = server.handle(StorageRequest::Open { path: path(b"test.txt") });
|
||||
assert!(matches!(resp, StorageResponse::Handle(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_nonexistent_returns_not_found() {
|
||||
let mut server = test_server();
|
||||
let (w0, w1) = path_words(b"nope.txt");
|
||||
let resp = server.handle(StorageRequest::Open { path_word0: w0, path_word1: w1 });
|
||||
let resp = server.handle(StorageRequest::Open { path: path(b"nope.txt") });
|
||||
assert_eq!(resp, StorageResponse::NotFound);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_then_read() {
|
||||
let mut server = test_server();
|
||||
let (w0, w1) = path_words(b"data");
|
||||
|
||||
let handle = match server.handle(StorageRequest::Create { path_word0: w0, path_word1: w1 }) {
|
||||
let handle = match server.handle(StorageRequest::Create { path: path(b"data") }) {
|
||||
StorageResponse::Handle(h) => h,
|
||||
_ => panic!("expected handle"),
|
||||
};
|
||||
@@ -58,35 +48,30 @@ fn write_then_read() {
|
||||
#[test]
|
||||
fn delete_file() {
|
||||
let mut server = test_server();
|
||||
let (w0, w1) = path_words(b"del.txt");
|
||||
|
||||
server.handle(StorageRequest::Create { path_word0: w0, path_word1: w1 });
|
||||
let resp = server.handle(StorageRequest::Delete { path_word0: w0, path_word1: w1 });
|
||||
server.handle(StorageRequest::Create { path: path(b"del.txt") });
|
||||
let resp = server.handle(StorageRequest::Delete { path: path(b"del.txt") });
|
||||
assert_eq!(resp, StorageResponse::Ok);
|
||||
|
||||
let resp = server.handle(StorageRequest::Open { path_word0: w0, path_word1: w1 });
|
||||
let resp = server.handle(StorageRequest::Open { path: path(b"del.txt") });
|
||||
assert_eq!(resp, StorageResponse::NotFound);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_dir() {
|
||||
let mut server = test_server();
|
||||
let (w0a, w1a) = path_words(b"a.txt");
|
||||
let (w0b, w1b) = path_words(b"b.txt");
|
||||
|
||||
server.handle(StorageRequest::Create { path_word0: w0a, path_word1: w1a });
|
||||
server.handle(StorageRequest::Create { path_word0: w0b, path_word1: w1b });
|
||||
server.handle(StorageRequest::Create { path: path(b"a.txt") });
|
||||
server.handle(StorageRequest::Create { path: path(b"b.txt") });
|
||||
|
||||
let (w0r, w1r) = path_words(b"/");
|
||||
let resp = server.handle(StorageRequest::ListDir { path_word0: w0r, path_word1: w1r });
|
||||
let resp = server.handle(StorageRequest::ListDir { path: path(b"/") });
|
||||
assert_eq!(resp, StorageResponse::DirEntries { count: 2 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn close_succeeds() {
|
||||
let mut server = test_server();
|
||||
let (w0, w1) = path_words(b"c.txt");
|
||||
let handle = match server.handle(StorageRequest::Create { path_word0: w0, path_word1: w1 }) {
|
||||
let handle = match server.handle(StorageRequest::Create { path: path(b"c.txt") }) {
|
||||
StorageResponse::Handle(h) => h,
|
||||
_ => panic!("expected handle"),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user