Files
meow/crates/meow-client/tests/flow.rs
T
2026-11-20 23:22:25 +01:00

710 lines
27 KiB
Rust

//! End-to-end in-process test: two real HTTP servers (memory store) and three clients.
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use meow_client::{Event, MeowClient, State};
use meow_crypto::password::KdfParams;
use meow_server::store::MemoryStore;
use meow_server::{encode_card, AppState, Limits};
use tokio::sync::broadcast;
async fn spawn_server() -> String {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!("http://{}", listener.local_addr().unwrap());
let keys = meow_core::server::ServerKeys::generate();
let (notify, _) = broadcast::channel(1024);
let state = Arc::new(AppState {
card: encode_card(&keys, &url, "test", ""),
keys,
store: Arc::new(MemoryStore::new(notify.clone(), 1000)),
notify,
limits: Limits::default(),
});
tokio::spawn(meow_server::serve(listener, state));
url
}
fn tmpdir() -> PathBuf {
let d = std::env::temp_dir().join(format!("meow-test-{}", rand_suffix()));
std::fs::create_dir_all(&d).unwrap();
d
}
fn rand_suffix() -> String {
meow_crypto::random_bytes::<8>()
.iter()
.map(|b| format!("{b:02x}"))
.collect()
}
async fn client(dir: &std::path::Path, name: &str, servers: &[String]) -> MeowClient {
let mut c = MeowClient::create(
&dir.join(format!("{name}.mwid2")),
name,
"pw",
KdfParams::INSECURE_TEST,
false,
)
.await
.unwrap();
for s in servers {
c.add_server(s).await.unwrap();
}
c
}
fn no_receipts(events: Vec<Event>) -> Vec<Event> {
events
.into_iter()
.filter(|e| !matches!(e, Event::Receipt { .. }))
.collect()
}
async fn no_receipt_settings(c: &mut MeowClient) {
c.set_settings(meow_client::Settings {
delivered_receipts: false,
read_receipts: false,
})
.await
.unwrap();
}
fn texts(events: &[Event]) -> Vec<String> {
events
.iter()
.filter_map(|e| match e {
Event::Message { text, .. } => Some(text.clone()),
_ => None,
})
.collect()
}
#[tokio::test(flavor = "multi_thread")]
async fn file_and_server_invitations_then_messaging() {
let s1 = spawn_server().await;
let s2 = spawn_server().await;
let servers = vec![s1.clone(), s2.clone()];
let dir = tmpdir();
let mut bob = client(&dir, "bob", &servers).await;
let mut alice = client(&dir, "alice", &servers).await;
let mut remote = client(&dir, "remote", &servers).await;
// ── file invitation ──
let offer = bob.create_invitation("alice", &[], "hello alice", 3600).await.unwrap();
assert_eq!(alice.inspect_invitation(&offer).unwrap().initiator_name, "bob");
let answer = alice.answer_invitation(&offer, None, &[]).await.unwrap();
let confirm = bob.accept_answer(&answer).await.unwrap();
assert_eq!(bob.contact("alice").unwrap().state, State::ConfirmSent);
bob.send_text("alice", "sent before finalize").await.unwrap();
alice.finalize_invitation(&confirm).await.unwrap();
assert_eq!(alice.contact("bob").unwrap().state, State::Active);
let ev = bob.sync(Duration::ZERO).await.unwrap();
assert!(ev.contains(&Event::ContactActivated {
contact: "alice".into()
}));
assert_eq!(bob.contact("alice").unwrap().state, State::Active);
// ── messaging, delivered on both servers but processed once ──
bob.send_text("alice", "hello").await.unwrap();
let r = bob.send_text("alice", "again").await.unwrap();
assert_eq!(r.delivered_to, 2);
let ev = alice.sync(Duration::ZERO).await.unwrap();
assert_eq!(texts(&ev), vec!["sent before finalize", "hello", "again"]);
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
alice.send_text("bob", "hi bob").await.unwrap();
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["hi bob"]);
// ── file ──
let f = dir.join("note.txt");
std::fs::write(&f, b"file content").unwrap();
bob.send_file("alice", &f).await.unwrap();
let ev = alice.sync(Duration::ZERO).await.unwrap();
let file_id = ev
.iter()
.find_map(|e| match e {
Event::File {
contact,
id,
name,
data,
} if contact == "bob" && name == "note.txt" => {
assert_eq!(data, b"file content");
Some(id.clone())
}
_ => None,
})
.expect("file event");
// The content is kept in the store.
assert_eq!(alice.file("bob", &file_id).unwrap().unwrap(), b"file content");
// ── server invitation ──
let url = bob
.publish_invitation("remote", &[], "via server", "secret", 3600)
.await
.unwrap();
assert!(remote
.accept_published_invitation(&url, "wrong", None, &[])
.await
.is_err());
let who = remote
.accept_published_invitation(&url, "secret", None, &[])
.await
.unwrap();
assert_eq!(who, "bob");
let ev = no_receipts(bob.sync(Duration::ZERO).await.unwrap());
assert_eq!(
ev,
vec![Event::InvitationAccepted {
contact: "remote".into()
}]
);
let ev = no_receipts(remote.sync(Duration::ZERO).await.unwrap());
assert_eq!(ev, vec![Event::ContactActivated { contact: "bob".into() }]);
let ev = no_receipts(bob.sync(Duration::ZERO).await.unwrap());
assert_eq!(
ev,
vec![Event::ContactActivated {
contact: "remote".into()
}]
);
// ── long poll ──
let path = dir.join("alice.mwid2");
drop(alice);
let waiter = tokio::spawn(async move {
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
let start = std::time::Instant::now();
let ev = alice.sync(Duration::from_secs(20)).await.unwrap();
(texts(&ev), start.elapsed())
});
tokio::time::sleep(Duration::from_millis(500)).await;
bob.send_text("alice", "wake up").await.unwrap();
let (got, elapsed) = waiter.await.unwrap();
assert_eq!(got, vec!["wake up"]);
assert!(elapsed < Duration::from_secs(10), "long poll took {elapsed:?}");
// ── one process at a time per identity ──
assert!(matches!(
MeowClient::open(&dir.join("bob.mwid2"), "pw").await,
Err(meow_client::Error::Locked(_))
));
drop(bob);
// ── persistence & wrong password ──
assert!(MeowClient::open(&dir.join("bob.mwid2"), "nope").await.is_err());
let bob2 = MeowClient::open(&dir.join("bob.mwid2"), "pw").await.unwrap();
assert_eq!(bob2.contacts().len(), 2);
assert_eq!(bob2.history("alice").unwrap().len(), 6); // 5 sent + "hi bob"
let _ = std::fs::remove_dir_all(&dir);
}
// ───────────── at-least-once delivery (roadmap lot 3.8) ─────────────
/// Memory store whose acknowledgements can be "lost" on demand.
struct FlakyStore {
inner: MemoryStore,
drop_acks: std::sync::atomic::AtomicBool,
/// Refuse deposits (the server answers with an error).
fail_posts: std::sync::atomic::AtomicBool,
}
#[async_trait::async_trait]
impl meow_server::store::ServerStore for FlakyStore {
async fn post(&self, mb: [u8; 32], payload: Vec<u8>, now: u64) -> meow_server::store::StoreResult<()> {
if self.fail_posts.load(std::sync::atomic::Ordering::SeqCst) {
return Err(meow_server::store::StoreError::Backend("down".into()));
}
self.inner.post(mb, payload, now).await
}
async fn fetch(
&self,
mbs: &[[u8; 32]],
limit: usize,
) -> meow_server::store::StoreResult<Vec<meow_proto::v2::StoredMessage>> {
self.inner.fetch(mbs, limit).await
}
async fn ack(&self, mb: [u8; 32], ids: &[Vec<u8>]) -> meow_server::store::StoreResult<()> {
if self.drop_acks.load(std::sync::atomic::Ordering::SeqCst) {
return Ok(());
}
self.inner.ack(mb, ids).await
}
async fn mark_request(&self, id: &[u8], ttl: Duration) -> meow_server::store::StoreResult<bool> {
self.inner.mark_request(id, ttl).await
}
async fn put_invitation(
&self,
payload: Vec<u8>,
password: &str,
ttl: Duration,
code_len: usize,
) -> meow_server::store::StoreResult<String> {
self.inner.put_invitation(payload, password, ttl, code_len).await
}
async fn get_invitation(&self, code: &str, password: &str) -> meow_server::store::StoreResult<Vec<u8>> {
self.inner.get_invitation(code, password).await
}
}
async fn spawn_flaky_server() -> (String, Arc<FlakyStore>) {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!("http://{}", listener.local_addr().unwrap());
let keys = meow_core::server::ServerKeys::generate();
let (notify, _) = broadcast::channel(1024);
let store = Arc::new(FlakyStore {
inner: MemoryStore::new(notify.clone(), 1000),
drop_acks: false.into(),
fail_posts: false.into(),
});
let state = Arc::new(AppState {
card: encode_card(&keys, &url, "flaky", ""),
keys,
store: store.clone(),
notify,
limits: Limits::default(),
});
tokio::spawn(meow_server::serve(listener, state));
(url, store)
}
/// What a client that crashed right after receiving the response would have done:
/// pull without ever acknowledging.
async fn raw_pull(
server: &meow_core::server::ServerInfo,
mailbox: &meow_crypto::SigningKeyPair,
) -> Vec<meow_proto::v2::StoredMessage> {
use meow_proto::v2::{self, request::Body as Req, response::Body as Resp};
let rid = meow_crypto::random_bytes::<16>().to_vec();
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();
let req = v2::Request {
request_id: rid.clone(),
timestamp: ts,
body: Some(Req::Pull(v2::Pull {
auths: vec![meow_core::rpc::mailbox_auth(mailbox, server, &rid, vec![], ts)],
wait_seconds: 0,
})),
};
let t = meow_client::transport::Transport::new().unwrap();
match t.call_request(server, req, Duration::ZERO).await.unwrap() {
Resp::PullResult(p) => p.messages,
other => panic!("unexpected {other:?}"),
}
}
#[tokio::test(flavor = "multi_thread")]
async fn no_message_lost_when_the_client_dies() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!("http://{}", listener.local_addr().unwrap());
let keys = meow_core::server::ServerKeys::generate();
let (notify, _) = broadcast::channel(1024);
let store = Arc::new(FlakyStore {
inner: MemoryStore::new(notify.clone(), 1000),
drop_acks: false.into(),
fail_posts: false.into(),
});
let limits = Limits {
max_pull_messages: 2, // force the "more" path
..Limits::default()
};
let state = Arc::new(AppState {
card: encode_card(&keys, &url, "flaky", ""),
keys,
store: store.clone(),
notify,
limits,
});
tokio::spawn(meow_server::serve(listener, state));
let dir = tmpdir();
let servers = vec![url];
let mut bob = client(&dir, "bob", &servers).await;
let mut alice = client(&dir, "alice", &servers).await;
// This test counts what stays on the server: no receipts traffic.
no_receipt_settings(&mut bob).await;
no_receipt_settings(&mut alice).await;
let offer = bob.create_invitation("alice", &[], "", 3600).await.unwrap();
let answer = alice.answer_invitation(&offer, None, &[]).await.unwrap();
let confirm = bob.accept_answer(&answer).await.unwrap();
alice.finalize_invitation(&confirm).await.unwrap();
bob.sync(Duration::ZERO).await.unwrap();
let server = alice.servers()[0].clone();
let mailbox = alice.contact("bob").unwrap().keys.as_ref().unwrap().mailbox.clone();
// 1. A client receives messages then dies before saving: nothing is lost.
for t in ["m1", "m2", "m3"] {
bob.send_text("alice", t).await.unwrap();
}
assert_eq!(raw_pull(&server, &mailbox).await.len(), 2); // limit 2, more = true
let ev = alice.sync(Duration::ZERO).await.unwrap();
assert_eq!(texts(&ev), vec!["m1", "m2", "m3"]); // several rounds ("more")
// Everything processed has been acknowledged and deleted.
assert!(raw_pull(&server, &mailbox).await.is_empty());
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
// 2. The acknowledgement is lost: the redelivered message is not processed twice,
// even after a restart (pending acks are persisted with the state).
store.drop_acks.store(true, std::sync::atomic::Ordering::SeqCst);
bob.send_text("alice", "m4").await.unwrap();
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["m4"]);
assert_eq!(raw_pull(&server, &mailbox).await.len(), 1);
let path = dir.join("alice.mwid2");
drop(alice);
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
store.drop_acks.store(false, std::sync::atomic::Ordering::SeqCst);
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
assert!(raw_pull(&server, &mailbox).await.is_empty());
assert_eq!(alice.history("bob").unwrap().len(), 4);
assert_eq!(store.inner.stored_messages(), 0);
// 3. Mailboxes that disappear still get their last messages acknowledged:
// the temporary answer mailbox of a server invitation...
let mut carol = client(&dir, "carol", &servers).await;
no_receipt_settings(&mut carol).await;
let link = bob.publish_invitation("carol", &[], "", "", 3600).await.unwrap();
carol.accept_published_invitation(&link, "", None, &[]).await.unwrap();
bob.sync(Duration::ZERO).await.unwrap(); // step 2 → 3, answer mailbox retired
carol.sync(Duration::ZERO).await.unwrap(); // step 3 → 4
bob.sync(Duration::ZERO).await.unwrap(); // ack
carol.send_text("bob", "hi").await.unwrap();
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["hi"]);
assert_eq!(store.inner.stored_messages(), 0);
// ...and a contact removed while an ack is still pending.
store.drop_acks.store(true, std::sync::atomic::Ordering::SeqCst);
bob.send_text("alice", "bye").await.unwrap();
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["bye"]);
store.drop_acks.store(false, std::sync::atomic::Ordering::SeqCst);
alice.remove_contact("bob").await.unwrap();
assert_eq!(store.inner.stored_messages(), 1);
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
assert_eq!(store.inner.stored_messages(), 0);
let _ = std::fs::remove_dir_all(&dir);
}
// ───────────── hidden spaces (roadmap lot 4.5) ─────────────
/// File invitation from `a` to `b`, completed.
async fn connect(a: &mut MeowClient, b: &mut MeowClient, b_name: &str) {
let offer = a.create_invitation(b_name, &[], "", 3600).await.unwrap();
let answer = b.answer_invitation(&offer, None, &[]).await.unwrap();
let confirm = a.accept_answer(&answer).await.unwrap();
b.finalize_invitation(&confirm).await.unwrap();
a.sync(Duration::ZERO).await.unwrap();
}
fn names(c: &MeowClient) -> Vec<String> {
c.contacts().iter().map(|k| k.name.clone()).collect()
}
#[tokio::test(flavor = "multi_thread")]
async fn hidden_contacts() {
let servers = vec![spawn_server().await];
let dir = tmpdir();
let path = dir.join("alice.mwid2");
let mut alice = client(&dir, "alice", &servers).await;
let mut bob = client(&dir, "bob", &servers).await;
let mut carol = client(&dir, "carol", &servers).await;
connect(&mut alice, &mut bob, "bob").await;
connect(&mut alice, &mut carol, "carol").await;
bob.send_text("alice", "before hiding").await.unwrap();
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["before hiding"]);
// Hide bob, with his history.
alice.hide_contact("bob", "bob-pw").await.unwrap();
assert!(alice.is_hidden("bob").unwrap());
assert!(!alice.is_hidden("carol").unwrap());
assert_eq!(alice.history("bob").unwrap().len(), 1);
drop(alice);
// Without the hidden password, bob does not exist and is not even polled.
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
assert_eq!(names(&alice), vec!["carol"]);
assert!(alice.history("bob").is_err());
bob.send_text("alice", "while locked").await.unwrap();
carol.send_text("alice", "hi").await.unwrap();
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["hi"]);
// With it, bob is back and his messages were waiting on the server.
assert!(!alice.unlock("wrong").await.unwrap());
assert!(alice.unlock("bob-pw").await.unwrap());
assert_eq!(names(&alice), vec!["carol", "bob"]);
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["while locked"]);
alice.send_text("bob", "from the hidden side").await.unwrap();
assert_eq!(
texts(&bob.sync(Duration::ZERO).await.unwrap()),
vec!["from the hidden side"]
);
assert_eq!(alice.history("bob").unwrap().len(), 3);
// Several contacts can share a hidden space.
alice.hide_contact("carol", "bob-pw").await.unwrap();
drop(alice);
let mut alice = MeowClient::open(&path, "pw").await.unwrap();
assert!(names(&alice).is_empty());
assert!(alice.unlock("bob-pw").await.unwrap());
assert_eq!(names(&alice), vec!["bob", "carol"]);
// Back to the main space.
alice.reveal_contact("bob").await.unwrap();
drop(alice);
let alice = MeowClient::open(&path, "pw").await.unwrap();
assert_eq!(names(&alice), vec!["bob"]);
assert_eq!(alice.history("bob").unwrap().len(), 3);
let _ = std::fs::remove_dir_all(&dir);
}
// ───────────── persistent send queue (roadmap lot 5.5) ─────────────
#[tokio::test(flavor = "multi_thread")]
async fn offline_messages_are_queued_and_retried() {
use std::sync::atomic::Ordering::SeqCst;
let (url, store) = spawn_flaky_server().await;
let servers = vec![url];
let dir = tmpdir();
let path = dir.join("bob.mwid2");
let mut alice = client(&dir, "alice", &servers).await;
let mut bob = client(&dir, "bob", &servers).await;
connect(&mut alice, &mut bob, "bob").await;
// Server refuses deposits: sending still succeeds, the message is queued.
store.fail_posts.store(true, SeqCst);
let r1 = bob.send_text("alice", "queued 1").await.unwrap();
let r2 = bob.send_text("alice", "queued 2").await.unwrap();
assert_eq!((r1.delivered_to, r2.delivered_to), (0, 0));
let pending = bob.pending_sends();
assert_eq!(pending.len(), 2);
assert!(pending.iter().all(|p| p.contact == "alice" && p.attempts >= 1));
assert_eq!(pending[0].message_id, r1.message_id);
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
// The queue survives a restart; not yet due (backoff), so a sync does not send.
drop(bob);
let mut bob = MeowClient::open(&path, "pw").await.unwrap();
assert_eq!(bob.pending_sends().len(), 2);
store.fail_posts.store(false, SeqCst);
bob.sync(Duration::ZERO).await.unwrap();
assert_eq!(bob.pending_sends().len(), 2);
// Forced flush: both delivered (in one Post), in order.
assert!(bob.flush_outbox().await.unwrap().is_empty());
assert!(bob.pending_sends().is_empty());
assert_eq!(
texts(&alice.sync(Duration::ZERO).await.unwrap()),
vec!["queued 1", "queued 2"]
);
// Due items are retried by the next send.
bob.set_retry_policy(meow_client::RetryPolicy {
base: Duration::from_secs(1),
..Default::default()
});
store.fail_posts.store(true, SeqCst);
bob.send_text("alice", "late").await.unwrap();
store.fail_posts.store(false, SeqCst);
tokio::time::sleep(Duration::from_millis(2500)).await;
let r = bob.send_text("alice", "now").await.unwrap();
assert_eq!(r.delivered_to, 1);
assert!(bob.pending_sends().is_empty());
assert_eq!(texts(&alice.sync(Duration::ZERO).await.unwrap()), vec!["late", "now"]);
// An item that expires undelivered is reported.
bob.set_retry_policy(meow_client::RetryPolicy {
max_age: Duration::ZERO,
..Default::default()
});
store.fail_posts.store(true, SeqCst);
let r = bob.send_text("alice", "lost").await.unwrap();
assert_eq!(r.delivered_to, 0);
let ev = bob.flush_outbox().await.unwrap();
assert_eq!(
ev,
vec![Event::SendFailed {
contact: "alice".into(),
message_id: r.message_id
}]
);
assert!(bob.pending_sends().is_empty());
let _ = std::fs::remove_dir_all(&dir);
}
// ───────────── receipts (roadmap lot 5.6) ─────────────
fn statuses(c: &MeowClient, name: &str, outbound: bool) -> Vec<meow_client::Status> {
c.history(name)
.unwrap()
.iter()
.filter(|h| h.outbound == outbound)
.map(|h| h.status)
.collect()
}
#[tokio::test(flavor = "multi_thread")]
async fn delivery_and_read_receipts() {
use meow_client::{ReceiptKind, Settings, Status};
let servers = vec![spawn_server().await, spawn_server().await];
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.sync(Duration::ZERO).await.unwrap();
let m1 = alice.send_text("bob", "one").await.unwrap().message_id;
let m2 = alice.send_text("bob", "two").await.unwrap().message_id;
assert_eq!(statuses(&alice, "bob", true), vec![Status::Sent, Status::Sent]);
// Bob receives (from 2 servers, processed once): one delivery receipt.
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["one", "two"]);
assert_eq!(
statuses(&bob, "alice", false),
vec![Status::Delivered, Status::Delivered]
);
let ev = alice.sync(Duration::ZERO).await.unwrap();
assert_eq!(
ev,
vec![Event::Receipt {
contact: "bob".into(),
kind: ReceiptKind::Delivered,
message_ids: vec![m1.clone(), m2.clone()]
}]
);
assert_eq!(
statuses(&alice, "bob", true),
vec![Status::Delivered, Status::Delivered]
);
// Bob reads.
assert_eq!(bob.mark_read("alice").await.unwrap(), 2);
assert_eq!(bob.mark_read("alice").await.unwrap(), 0);
assert_eq!(statuses(&bob, "alice", false), vec![Status::Read, Status::Read]);
let ev = alice.sync(Duration::ZERO).await.unwrap();
assert_eq!(
ev,
vec![Event::Receipt {
contact: "bob".into(),
kind: ReceiptKind::Read,
message_ids: vec![m1, m2]
}]
);
assert_eq!(statuses(&alice, "bob", true), vec![Status::Read, Status::Read]);
// Receipts are not acknowledged by receipts.
assert!(bob.sync(Duration::ZERO).await.unwrap().is_empty());
// Receipts can be turned off (persisted with the identity).
let off = Settings {
delivered_receipts: false,
read_receipts: false,
};
bob.set_settings(off).await.unwrap();
let path = dir.join("bob.mwid2");
drop(bob);
let mut bob = MeowClient::open(&path, "pw").await.unwrap();
assert_eq!(bob.settings(), off);
alice.send_text("bob", "three").await.unwrap();
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["three"]);
assert_eq!(bob.mark_read("alice").await.unwrap(), 1);
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
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);
}