basic test working

This commit is contained in:
Felix Ableitner 2024-05-23 11:35:21 +02:00
parent 76393bd964
commit 86d3550901
2 changed files with 19 additions and 12 deletions

View file

@ -94,11 +94,15 @@ impl Instance {
.await .await
} }
#[cfg(test)] /// Only for use in tests
pub async fn delete_all(pool: &mut DbPool<'_>) -> Result<usize, Error> { pub async fn delete_all(pool: &mut DbPool<'_>) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
diesel::delete(federation_queue_state::table)
.execute(conn)
.await?;
diesel::delete(instance::table).execute(conn).await diesel::delete(instance::table).execute(conn).await
} }
pub async fn allowlist(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> { pub async fn allowlist(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
instance::table instance::table

View file

@ -58,11 +58,11 @@ impl SendManager {
} }
pub fn run(mut self) -> CancellableTask { pub fn run(mut self) -> CancellableTask {
let task = CancellableTask::spawn(WORKER_EXIT_TIMEOUT, move |cancel| async move { let cancel = CancellableTask::spawn(WORKER_EXIT_TIMEOUT, move |cancel| async move {
self.do_loop(cancel).await.unwrap(); self.do_loop(cancel).await.unwrap();
self.cancel().await.unwrap(); self.cancel().await.unwrap();
}); });
task cancel
} }
async fn do_loop(&mut self, cancel: CancellationToken) -> LemmyResult<()> { async fn do_loop(&mut self, cancel: CancellationToken) -> LemmyResult<()> {
@ -152,7 +152,7 @@ impl SendManager {
mod test { mod test {
use super::*; use super::*;
use tokio::time::sleep; use tokio::{spawn, time::sleep};
#[tokio::test] #[tokio::test]
async fn test_start_stop_federation_workers() -> LemmyResult<()> { async fn test_start_stop_federation_workers() -> LemmyResult<()> {
@ -176,20 +176,23 @@ mod test {
]; ];
// start it and wait a moment // start it and wait a moment
let task = SendManager::new(opts, federation_config); let mut task = SendManager::new(opts, federation_config);
task.run(); let cancel = CancellationToken::new();
sleep(Duration::from_secs(1)); let cancel_ = cancel.clone();
spawn(async move {
sleep(Duration::from_millis(100)).await;
cancel_.cancel();
});
task.do_loop(cancel.clone()).await.unwrap();
assert_eq!(3, task.workers.len());
// check that correct number of instance workers was started // check that correct number of instance workers was started
// TODO: need to wrap in Arc or something similar // TODO: need to wrap in Arc or something similar
// TODO: test with different `opts`, dead/blocked instances etc // TODO: test with different `opts`, dead/blocked instances etc
assert_eq!(3, task.workers.len()); //assert_eq!(3, task.workers.len());
// cleanup // cleanup
for i in instances { Instance::delete_all(pool).await?;
Instance::delete(pool, i.id).await?;
}
task.cancel().await?;
Ok(()) Ok(())
} }
} }