Alias BoxByteStream, and test git get_file implementation

This commit is contained in:
Brian Picciano 2023-07-08 15:19:31 +02:00
parent 60b90746fc
commit 8b75b141f4
6 changed files with 46 additions and 50 deletions

View File

@ -14,7 +14,7 @@ pub trait Manager {
fn sync_domain<'mgr>( fn sync_domain<'mgr>(
&'mgr self, &'mgr self,
domain: domain::Name, domain: domain::Name,
) -> util::BoxedFuture<'mgr, Result<(), unexpected::Error>>; ) -> util::BoxFuture<'mgr, Result<(), unexpected::Error>>;
fn get_http01_challenge_key(&self, token: &str) -> Result<String, GetHttp01ChallengeKeyError>; fn get_http01_challenge_key(&self, token: &str) -> Result<String, GetHttp01ChallengeKeyError>;
@ -87,7 +87,7 @@ impl Manager for ManagerImpl {
fn sync_domain<'mgr>( fn sync_domain<'mgr>(
&'mgr self, &'mgr self,
domain: domain::Name, domain: domain::Name,
) -> util::BoxedFuture<'mgr, Result<(), unexpected::Error>> { ) -> util::BoxFuture<'mgr, Result<(), unexpected::Error>> {
Box::pin(async move { Box::pin(async move {
// if there's an existing cert, and its expiry (determined by the soonest value of // if there's an existing cert, and its expiry (determined by the soonest value of
// not_after amongst its parts) is later than 30 days from now, then we consider it to be // not_after amongst its parts) is later than 30 days from now, then we consider it to be

View File

@ -3,8 +3,7 @@ use crate::error::unexpected::{self, Mappable};
use crate::origin; use crate::origin;
use crate::util; use crate::util;
use futures::stream; use std::sync;
use std::{io, sync};
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
@ -150,18 +149,18 @@ pub trait Manager: Sync + Send + rustls::server::ResolvesServerCert {
&'store self, &'store self,
domain: &domain::Name, domain: &domain::Name,
path: &str, path: &str,
) -> Result<stream::BoxStream<'static, io::Result<Vec<u8>>>, GetFileError>; ) -> Result<util::BoxByteStream, GetFileError>;
fn sync_cert<'mgr>( fn sync_cert<'mgr>(
&'mgr self, &'mgr self,
domain: domain::Name, domain: domain::Name,
) -> util::BoxedFuture<'mgr, Result<(), unexpected::Error>>; ) -> util::BoxFuture<'mgr, Result<(), unexpected::Error>>;
fn sync_with_config<'mgr>( fn sync_with_config<'mgr>(
&'mgr self, &'mgr self,
domain: domain::Name, domain: domain::Name,
config: config::Config, config: config::Config,
) -> util::BoxedFuture<'mgr, Result<(), SyncWithConfigError>>; ) -> util::BoxFuture<'mgr, Result<(), SyncWithConfigError>>;
fn get_acme_http01_challenge_key( fn get_acme_http01_challenge_key(
&self, &self,
@ -254,7 +253,7 @@ impl Manager for ManagerImpl {
&'store self, &'store self,
domain: &domain::Name, domain: &domain::Name,
path: &str, path: &str,
) -> Result<stream::BoxStream<'static, io::Result<Vec<u8>>>, GetFileError> { ) -> Result<util::BoxByteStream, GetFileError> {
let config = self.domain_config_store.get(domain)?; let config = self.domain_config_store.get(domain)?;
let f = self.origin_store.get_file(&config.origin_descr, path)?; let f = self.origin_store.get_file(&config.origin_descr, path)?;
Ok(f) Ok(f)
@ -263,7 +262,7 @@ impl Manager for ManagerImpl {
fn sync_cert<'mgr>( fn sync_cert<'mgr>(
&'mgr self, &'mgr self,
domain: domain::Name, domain: domain::Name,
) -> util::BoxedFuture<'mgr, Result<(), unexpected::Error>> { ) -> util::BoxFuture<'mgr, Result<(), unexpected::Error>> {
Box::pin(async move { Box::pin(async move {
if let Some(ref acme_manager) = self.acme_manager { if let Some(ref acme_manager) = self.acme_manager {
acme_manager.sync_domain(domain.clone()).await?; acme_manager.sync_domain(domain.clone()).await?;
@ -277,7 +276,7 @@ impl Manager for ManagerImpl {
&'mgr self, &'mgr self,
domain: domain::Name, domain: domain::Name,
config: config::Config, config: config::Config,
) -> util::BoxedFuture<'mgr, Result<(), SyncWithConfigError>> { ) -> util::BoxFuture<'mgr, Result<(), SyncWithConfigError>> {
Box::pin(async move { Box::pin(async move {
let config_hash = config let config_hash = config
.hash() .hash()

View File

@ -1,6 +1,6 @@
use crate::error::unexpected; use crate::error::unexpected;
use futures::stream; use crate::util;
use std::{io, sync}; use std::sync;
pub mod git; pub mod git;
pub mod mux; pub mod mux;
@ -59,11 +59,7 @@ pub trait Store {
into: &mut dyn std::io::Write, into: &mut dyn std::io::Write,
) -> Result<(), ReadFileIntoError>; ) -> Result<(), ReadFileIntoError>;
fn get_file( fn get_file(&self, descr: &Descr, path: &str) -> Result<util::BoxByteStream, GetFileError>;
&self,
descr: &Descr,
path: &str,
) -> Result<stream::BoxStream<'static, io::Result<Vec<u8>>>, GetFileError>;
} }
pub fn new_mock() -> sync::Arc<sync::Mutex<MockStore>> { pub fn new_mock() -> sync::Arc<sync::Mutex<MockStore>> {
@ -93,7 +89,7 @@ impl Store for sync::Arc<sync::Mutex<MockStore>> {
&'store self, &'store self,
descr: &Descr, descr: &Descr,
path: &str, path: &str,
) -> Result<stream::BoxStream<'static, io::Result<Vec<u8>>>, GetFileError> { ) -> Result<util::BoxByteStream, GetFileError> {
self.lock().unwrap().get_file(descr, path) self.lock().unwrap().get_file(descr, path)
} }
} }

View File

@ -1,5 +1,5 @@
use crate::error::unexpected::{self, Intoable, Mappable}; use crate::error::unexpected::{self, Intoable, Mappable};
use crate::origin; use crate::{origin, util};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::{collections, fs, io, sync}; use std::{collections, fs, io, sync};
@ -334,12 +334,11 @@ impl super::Store for FSStore {
Ok(()) Ok(())
} }
// TODO test this
fn get_file<'store>( fn get_file<'store>(
&'store self, &'store self,
descr: &origin::Descr, descr: &origin::Descr,
path: &str, path: &str,
) -> Result<stream::BoxStream<'static, io::Result<Vec<u8>>>, origin::GetFileError> { ) -> Result<util::BoxByteStream, origin::GetFileError> {
let repo_snapshot = match self.get_repo_snapshot(descr) { let repo_snapshot = match self.get_repo_snapshot(descr) {
Ok(Some(repo_snapshot)) => repo_snapshot, Ok(Some(repo_snapshot)) => repo_snapshot,
Ok(None) => return Err(origin::GetFileError::DescrNotSynced), Ok(None) => return Err(origin::GetFileError::DescrNotSynced),
@ -382,10 +381,11 @@ impl super::Store for FSStore {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::origin::{self, Store}; use crate::origin::{self, Store};
use futures::StreamExt;
use tempdir::TempDir; use tempdir::TempDir;
#[test] #[tokio::test]
fn basic() { async fn basic() {
let tmp_dir = TempDir::new("origin_store_git").unwrap(); let tmp_dir = TempDir::new("origin_store_git").unwrap();
let curr_dir = format!("file://{}", std::env::current_dir().unwrap().display()); let curr_dir = format!("file://{}", std::env::current_dir().unwrap().display());
@ -405,31 +405,32 @@ mod tests {
store.sync(&descr).expect("sync should succeed"); store.sync(&descr).expect("sync should succeed");
store.sync(&descr).expect("second sync should succeed"); store.sync(&descr).expect("second sync should succeed");
let assert_write = |path: &str| { {
let mut into: Vec<u8> = vec![];
store
.read_file_into(&descr, path, &mut into)
.expect("write should succeed");
assert!(into.len() > 0);
};
assert_write("src/lib.rs");
assert_write("/src/lib.rs");
let mut into: Vec<u8> = vec![];
// RepoSnapshot doesn't exist // RepoSnapshot doesn't exist
let mut into: Vec<u8> = vec![];
assert!(matches!( assert!(matches!(
store.read_file_into(&other_descr, "DNE", &mut into), store.read_file_into(&other_descr, "DNE", &mut into),
Err::<_, origin::ReadFileIntoError>(origin::ReadFileIntoError::DescrNotSynced), Err::<_, origin::ReadFileIntoError>(origin::ReadFileIntoError::DescrNotSynced),
)); ));
}
// File doesn't exist let assert_file_dne = |path: &str| match store.get_file(&descr, path) {
assert!(matches!( Err(origin::ReadFileIntoError::FileNotFound) => (),
store.read_file_into(&descr, "DNE", &mut into), _ => assert!(false, "file should have not been found"),
Err::<(), origin::ReadFileIntoError>(origin::ReadFileIntoError::FileNotFound), };
));
assert_eq!(into.len(), 0); let assert_file_not_empty = |path: &str| {
let f = store.get_file(&descr, path).expect("file not retrieved");
async move {
let body = f.map(|r| r.unwrap()).concat().await;
assert!(body.len() > 0);
}
};
assert_file_not_empty("src/lib.rs").await;
assert_file_not_empty("/src/lib.rs").await;
assert_file_dne("DNE");
assert_file_dne("src/../src/lib.rs");
let descrs = store.all_descrs().expect("all_descrs called"); let descrs = store.all_descrs().expect("all_descrs called");

View File

@ -1,7 +1,5 @@
use crate::error::unexpected::Mappable; use crate::error::unexpected::Mappable;
use crate::origin; use crate::{origin, util};
use futures::stream;
use std::io;
pub struct Store<F, S> pub struct Store<F, S>
where where
@ -58,7 +56,7 @@ where
&'store self, &'store self,
descr: &origin::Descr, descr: &origin::Descr,
path: &str, path: &str,
) -> Result<stream::BoxStream<'static, io::Result<Vec<u8>>>, origin::GetFileError> { ) -> Result<util::BoxByteStream, origin::GetFileError> {
(self.mapping_fn)(descr) (self.mapping_fn)(descr)
.or_unexpected_while(format!("mapping {:?} to store", &descr))? .or_unexpected_while(format!("mapping {:?} to store", &descr))?
.get_file(descr, path) .get_file(descr, path)

View File

@ -12,13 +12,15 @@ pub fn open_file(path: &path::Path) -> io::Result<Option<fs::File>> {
} }
} }
pub type BoxedFuture<'a, O> = pin::Pin<Box<dyn futures::Future<Output = O> + Send + 'a>>; pub type BoxByteStream = futures::stream::BoxStream<'static, io::Result<Vec<u8>>>;
pub type BoxFuture<'a, O> = pin::Pin<Box<dyn futures::Future<Output = O> + Send + 'a>>;
pub struct TaskStack<E> pub struct TaskStack<E>
where where
E: error::Error + Send + 'static, E: error::Error + Send + 'static,
{ {
wait_group: Vec<BoxedFuture<'static, Result<(), E>>>, wait_group: Vec<BoxFuture<'static, Result<(), E>>>,
} }
impl<E> TaskStack<E> impl<E> TaskStack<E>