refactor tests

This commit is contained in:
Felix Ableitner 2024-05-30 12:36:39 +02:00
parent 17336b9797
commit ca4b735517
2 changed files with 81 additions and 49 deletions

View file

@ -180,6 +180,7 @@ pub(crate) async fn get_latest_activity_id(pool: &mut DbPool<'_>) -> Result<Acti
/// the domain name is needed for logging, pass it to the stats printer so it doesn't need to look
/// up the domain itself
#[derive(Debug)]
pub(crate) struct FederationQueueStateWithDomain {
pub domain: String,
pub state: FederationQueueState,

View file

@ -95,6 +95,7 @@ impl InstanceWorker {
self.inboxes.update_communities(&self.context).await?;
}
// final update of state in db
dbg!("done");
self.save_and_send_state().await?;
Ok(())
}
@ -164,6 +165,7 @@ impl InstanceWorker {
);
}
if self.stop.is_cancelled() {
dbg!("done");
return Ok(());
}
// send success!
@ -272,13 +274,21 @@ mod test {
use tokio::{
select,
spawn,
sync::mpsc::{error::TryRecvError, unbounded_channel},
sync::mpsc::{error::TryRecvError, unbounded_channel, UnboundedReceiver},
};
use url::Url;
#[tokio::test]
#[serial]
async fn test_worker() -> LemmyResult<()> {
struct Data {
context: activitypub_federation::config::Data<LemmyContext>,
instance: Instance,
person: Person,
stats_receiver: UnboundedReceiver<FederationQueueStateWithDomain>,
inbox_receiver: UnboundedReceiver<String>,
cancel: CancellationToken,
}
impl Data {
async fn init() -> LemmyResult<Self> {
let context = LemmyContext::init_test_context().await;
let instance = Instance::read_or_create(&mut context.pool(), "localhost".to_string()).await?;
@ -296,8 +306,8 @@ mod test {
let person = Person::create(&mut context.pool(), &person_form).await?;
let cancel = CancellationToken::new();
let (stats_sender, mut stats_receiver) = unbounded_channel();
let (inbox_sender, mut inbox_receiver) = unbounded_channel();
let (stats_sender, stats_receiver) = unbounded_channel();
let (inbox_sender, inbox_receiver) = unbounded_channel();
// listen for received activities in background
let cancel_ = cancel.clone();
@ -309,40 +319,61 @@ mod test {
cancel.clone(),
stats_sender,
));
// wait for startup before creating sent activity
// wait for startup
sleep(WORK_FINISHED_RECHECK_DELAY).await;
let sent = send_activity(person.actor_id, &context).await?;
Ok(Self {
context,
instance,
person,
stats_receiver,
inbox_receiver,
cancel,
})
}
async fn cleanup(&self) -> LemmyResult<()> {
self.cancel.cancel();
sleep(WORK_FINISHED_RECHECK_DELAY).await;
Instance::delete_all(&mut self.context.pool()).await?;
Person::delete(&mut self.context.pool(), self.person.id).await?;
Ok(())
}
}
#[tokio::test]
#[serial]
async fn test_stats() -> LemmyResult<()> {
let mut data = Data::init().await?;
// first receive at startup
let rcv = data.stats_receiver.recv().await.unwrap();
assert_eq!(data.instance.id, rcv.state.instance_id);
assert_eq!(Some(ActivityId(0)), rcv.state.last_successful_id);
let sent = send_activity(data.person.actor_id.clone(), &data.context).await?;
sleep(WORK_FINISHED_RECHECK_DELAY * 2).await;
// first receive at startup
let rcv = stats_receiver.recv().await.unwrap();
assert_eq!(instance.id, rcv.state.instance_id);
assert_eq!(Some(ActivityId(0)), rcv.state.last_successful_id);
// receive for successfully sent activity
let inbox_rcv = inbox_receiver.recv().await.unwrap();
let inbox_rcv = data.inbox_receiver.recv().await.unwrap();
let parsed_activity = serde_json::from_str::<WithContext<Value>>(&inbox_rcv)?;
assert_eq!(&sent.data, parsed_activity.inner());
let rcv = stats_receiver.recv().await.unwrap();
assert_eq!(instance.id, rcv.state.instance_id);
let rcv = data.stats_receiver.recv().await.unwrap();
assert_eq!(data.instance.id, rcv.state.instance_id);
assert_eq!(Some(sent.id), rcv.state.last_successful_id);
// cleanup
cancel.cancel();
Instance::delete_all(&mut context.pool()).await?;
Person::delete(&mut context.pool(), person.id).await?;
data.cleanup().await?;
// it also sends state on shutdown
let rcv = stats_receiver.try_recv();
let rcv = data.stats_receiver.try_recv();
assert!(rcv.is_ok());
// nothing further received
let rcv = stats_receiver.try_recv();
let rcv = data.stats_receiver.try_recv();
assert_eq!(Some(TryRecvError::Disconnected), rcv.err());
let inbox_rcv = inbox_receiver.try_recv();
let inbox_rcv = data.inbox_receiver.try_recv();
assert_eq!(Some(TryRecvError::Empty), inbox_rcv.err());
Ok(())