background task

This commit is contained in:
yves committed 2026-11-20 23:22:25 +01:00
1 parent 47b73e6935
commit ce44ee48b3
7 files changed
+483 -69

No files matched your search

+92
View File
@@ -615,3 +615,95 @@ async fn delivery_and_read_receipts() {
assert_eq!(statuses(&alice, "bob", true)[2], Status::Sent);
let _ = std::fs::remove_dir_all(&dir);
}
// ───────────── background task and event stream (roadmap lot 5.9) ─────────────
async fn next_message(rx: &mut tokio::sync::broadcast::Receiver<Event>) -> String {
tokio::time::timeout(Duration::from_secs(10), async {
loop {
if let Event::Message { text, .. } = rx.recv().await.unwrap() {
return text;
}
}
})
.await
.expect("no message within 10 s")
}
#[tokio::test(flavor = "multi_thread")]
async fn background_sync_publishes_events() {
use std::sync::atomic::Ordering::SeqCst;
let (url, store) = spawn_flaky_server().await;
let servers = vec![url];
let dir = tmpdir();
let mut alice = client(&dir, "alice", &servers).await;
let mut bob = client(&dir, "bob", &servers).await;
connect(&mut alice, &mut bob, "bob").await;
// Bob runs in the background with a long long-poll.
let opts = meow_client::BackgroundOptions {
long_poll: Duration::from_secs(20),
..Default::default()
};
bob.set_retry_policy(meow_client::RetryPolicy {
base: Duration::from_secs(1),
..Default::default()
});
let bob = bob.start(opts);
let mut rx = bob.subscribe();
tokio::time::sleep(Duration::from_millis(300)).await;
// Pushed to the subscriber without any explicit sync.
alice.send_text("bob", "hello background").await.unwrap();
assert_eq!(next_message(&mut rx).await, "hello background");
// The lock is free while the task waits on the server: sending is immediate.
tokio::time::sleep(Duration::from_millis(300)).await;
let start = std::time::Instant::now();
let r = bob.lock().await.send_text("alice", "reply").await.unwrap();
assert_eq!(r.delivered_to, 1);
assert!(
start.elapsed() < Duration::from_secs(3),
"send waited {:?}",
start.elapsed()
);
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["reply"]);
// Alice's read receipt reaches Bob's subscribers too.
alice.mark_read("bob").await.unwrap();
let got = tokio::time::timeout(Duration::from_secs(10), async {
loop {
if let Event::Receipt { kind, .. } = rx.recv().await.unwrap() {
if kind == meow_client::ReceiptKind::Read {
return;
}
}
}
})
.await;
assert!(got.is_ok(), "no read receipt event");
// A message queued while the server refuses deposits is retried by the task.
store.fail_posts.store(true, SeqCst);
let r = bob.lock().await.send_text("alice", "retried").await.unwrap();
assert_eq!(r.delivered_to, 0);
store.fail_posts.store(false, SeqCst);
// The task shortens its long-poll to retry on time (no other traffic needed).
let delivered = tokio::time::timeout(Duration::from_secs(10), async {
loop {
if bob.lock().await.pending_sends().is_empty() {
return;
}
tokio::time::sleep(Duration::from_millis(200)).await;
}
})
.await;
assert!(delivered.is_ok(), "queued message not retried");
assert!(texts(&alice.sync(Duration::ZERO).await.unwrap()).contains(&"retried".to_string()));
// Stopping gives the client back, usable as before.
let mut bob = bob.stop().await;
alice.send_text("bob", "after stop").await.unwrap();
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["after stop"]);
let _ = std::fs::remove_dir_all(&dir);
}