[relay] Wake-up: a push endpoint per mailbox, and a constant wake to it when a blob arrives
ci / check (pull_request) Has been cancelled
ci / check (pull_request) Has been cancelled
This commit is contained in:
@@ -129,13 +129,32 @@ impl Transport for Http {
|
||||
serde_json::from_slice(&body).map_err(|_| TransportError::Failed)?;
|
||||
Ok(claimed.key_package.bytes().to_vec())
|
||||
}
|
||||
fn set_push(
|
||||
&self,
|
||||
id: &MailboxId,
|
||||
fetch: &Token,
|
||||
endpoint: Option<&PushEndpoint>,
|
||||
) -> Result<(), TransportError> {
|
||||
let path = format!("/v0/mailboxes/{}/push", id.to_wire());
|
||||
match endpoint {
|
||||
Some(e) => {
|
||||
let body = serde_json::to_vec(&SetPush {
|
||||
endpoint: e.clone(),
|
||||
})
|
||||
.map_err(|_| TransportError::Failed)?;
|
||||
self.call("PUT", &path, Some(fetch), &body, true)
|
||||
}
|
||||
None => self.call("DELETE", &path, Some(fetch), &[], false),
|
||||
}
|
||||
.map(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts the real relay on 127.0.0.1:<free port> with its database in `db`.
|
||||
fn start_relay(db: &std::path::Path) -> SocketAddr {
|
||||
let store = Arc::new(Store::open(db).expect("store"));
|
||||
let clock: Clock = Arc::new(|| 1_794_000_000);
|
||||
let app = router(AppState { store, clock });
|
||||
let app = router(AppState::new(store, clock));
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
@@ -222,3 +241,37 @@ fn pair_and_talk_through_a_real_relay_which_never_sees_a_name_or_a_word() {
|
||||
// The messages were there, as ciphertext: the database grew while they waited.
|
||||
assert!(waiting.len() >= 4096, "the relay stored something");
|
||||
}
|
||||
|
||||
// Wake-up (`.agents/features/wake-up.md` §2.4): an endpoint per mailbox, set and cleared through the
|
||||
// real relay with the mailbox's fetch token.
|
||||
#[test]
|
||||
fn each_inbox_takes_its_own_push_endpoint_through_the_real_relay() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let db = dir.path().join("relay.db");
|
||||
let relay = Http(start_relay(&db));
|
||||
let mut eva = Client::new("Eva", relay.clone()).expect("eva");
|
||||
let mut jan = Client::new("Jan", relay.clone()).expect("jan");
|
||||
let invite = eva.create_invite().expect("invite");
|
||||
jan.accept_invite(&invite).expect("accept");
|
||||
eva.poll().expect("poll");
|
||||
let (eva_boxes, jan_boxes) = (eva.inboxes(), jan.inboxes());
|
||||
assert_eq!((eva_boxes.len(), jan_boxes.len()), (1, 1));
|
||||
let endpoint = |path: &str| PushEndpoint::parse(&format!("https://push.invalid/{path}"));
|
||||
let a = endpoint("upEvaOnly1").expect("endpoint");
|
||||
eva.set_push(&eva_boxes[0], Some(&a)).expect("set");
|
||||
jan.set_push(
|
||||
&jan_boxes[0],
|
||||
Some(&endpoint("upJanOnly2").expect("endpoint")),
|
||||
)
|
||||
.expect("set");
|
||||
let stored = std::fs::read(&db).expect("db");
|
||||
assert!(contains(&stored, b"upEvaOnly1") && contains(&stored, b"upJanOnly2"));
|
||||
// Not Jan's mailbox: Eva cannot set it.
|
||||
assert_eq!(
|
||||
eva.set_push(&jan_boxes[0], Some(&a)),
|
||||
Err(ClientError::UnknownContact)
|
||||
);
|
||||
eva.set_push(&eva_boxes[0], None).expect("clear");
|
||||
// Nothing is posted after this: a post would make the relay try to wake `push.invalid`, and no
|
||||
// test reaches for the network.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user