mod common;
use std::path::PathBuf;
use std::time::Duration;
use sapphire_backend::protocol as proto;
use sapphire_framework_bridge::LoopbackNetwork;
const QUIET: Duration = Duration::from_secs(1);
const QUIET_DEADLINE: Duration = Duration::from_secs(30);
const OBSERVE: Duration = Duration::from_secs(3);
const LOOP_LIMIT: u64 = 15;
async fn await_file(host: &common::Host, rel: &str) -> String {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
loop {
if let Ok(text) = std::fs::read_to_string(host.ws.join(rel)) {
return text;
}
assert!(std::time::Instant::now() < deadline, "{rel} never arrived");
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
}
async fn write(host: &common::Host, rel: &str, content: &str) {
let _: proto::Ack = host
.client
.call(
proto::WRITE_FILE,
proto::ContentParams {
path: PathBuf::from(rel),
content: content.into(),
},
)
.await
.unwrap();
}
#[tokio::test(flavor = "multi_thread")]
async fn a_change_arrives_without_waiting_for_the_next_dial() {
let net = LoopbackNetwork::new();
let (a, b) = common::synced_pair(&net).await;
common::settle(&[&a, &b]).await;
let started = std::time::Instant::now();
write(&a, "quick.md", "now").await;
assert_eq!(await_file(&b, "quick.md").await, "now");
assert!(
started.elapsed() < std::time::Duration::from_secs(10),
"a live session should deliver in well under the dial interval, took {:?}",
started.elapsed()
);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_change_propagates_through_a_host_in_the_middle() {
let net = LoopbackNetwork::partitioned(&[
(common::NODE_A, common::NODE_S),
(common::NODE_S, common::NODE_B),
]);
let (a, s, b) = common::synced_triple(&net).await;
common::settle(&[&a, &s, &b]).await;
write(&a, "relayed.md", "from a").await;
assert_eq!(await_file(&b, "relayed.md").await, "from a");
let _ = s;
}
#[tokio::test(flavor = "multi_thread")]
async fn an_entry_is_not_forwarded_back_to_the_peer_it_came_from() {
let net = LoopbackNetwork::new();
let (a, b) = common::synced_pair(&net).await;
common::settle(&[&a, &b]).await;
let before = common::quiet(&[&b], QUIET, QUIET_DEADLINE).await[0];
write(&a, "one-way.md", "x").await;
await_file(&b, "one-way.md").await;
common::quiet(&[&b], QUIET, QUIET_DEADLINE).await;
let grown = common::frames_sent(&b) - before;
assert!(
grown < LOOP_LIMIT,
"B sent {grown} frames for one incoming file; it is echoing"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn three_connected_hosts_do_not_loop_an_edit_between_them() {
let net = LoopbackNetwork::new(); let (a, s, b) = common::synced_triple(&net).await;
common::settle(&[&a, &s, &b]).await;
write(&a, "triangle.md", "x").await;
await_file(&b, "triangle.md").await;
await_file(&s, "triangle.md").await;
let before = common::quiet(&[&a, &b], QUIET, QUIET_DEADLINE).await;
tokio::time::sleep(OBSERVE).await;
let grown = [
common::frames_sent(&a) - before[0],
common::frames_sent(&b) - before[1],
];
assert!(
grown[0] < LOOP_LIMIT && grown[1] < LOOP_LIMIT,
"A or B is still sending after the edit was propagated: A +{}, B +{} over {OBSERVE:?}",
grown[0],
grown[1]
);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_peer_that_goes_away_is_redialled() {
let net = LoopbackNetwork::new();
let (a, b) = common::synced_pair(&net).await;
common::settle(&[&a, &b]).await;
b.drop_connections().await;
write(&a, "after-drop.md", "x").await;
assert_eq!(
await_file(&b, "after-drop.md").await,
"x",
"a dropped connection must be redialled, not mourned"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn an_unreachable_peer_does_not_stall_the_others() {
let net = LoopbackNetwork::partitioned(&[(common::NODE_A, common::NODE_B)]);
let (a, s, b) = common::synced_triple(&net).await;
common::settle(&[&a, &b]).await;
write(&a, "despite-s.md", "x").await;
assert_eq!(await_file(&b, "despite-s.md").await, "x");
let _ = s;
}
async fn await_contents(host: &common::Host, rel: &str, want: &str) {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
loop {
if std::fs::read_to_string(host.ws.join(rel)).is_ok_and(|text| text == want) {
return;
}
assert!(
std::time::Instant::now() < deadline,
"{rel} never became {want:?} on {}",
host.ws.display()
);
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
}
async fn await_absent(host: &common::Host, rel: &str) {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
loop {
if std::fs::symlink_metadata(host.ws.join(rel)).is_err() {
return;
}
assert!(
std::time::Instant::now() < deadline,
"{rel} never disappeared from {}",
host.ws.display()
);
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
}
#[tokio::test(flavor = "multi_thread")]
async fn a_delete_after_a_remote_edit_propagates_over_a_live_session() {
let net = LoopbackNetwork::new();
let (a, b) = common::synced_pair(&net).await;
common::settle(&[&a, &b]).await;
write(&a, "note.md", "one").await;
assert_eq!(await_file(&b, "note.md").await, "one");
std::fs::write(b.ws.join("note.md"), "two").unwrap();
await_contents(&a, "note.md", "two").await;
std::fs::remove_file(a.ws.join("note.md")).unwrap();
await_absent(&b, "note.md").await;
}