| 1 | #![cfg(feature = "live")] |
| 2 | |
| 3 | use notebook::{ |
| 4 | Replica, |
| 5 | live::share::{HostedRemote, Sharing}, |
| 6 | }; |
| 7 | use onestore::op::{Edit, Op, PageOp}; |
| 8 | use std::sync::{Arc, Barrier}; |
| 9 | |
| 10 | #[path = "support/live.rs"] |
| 11 | mod live; |
| 12 | use live::*; |
| 13 | |
| 14 | #[test] |
| 15 | fn edit_receipts_survive_guest_and_host_process_death() { |
| 16 | use notebook::live::share::{self, Guest, Host}; |
| 17 | use notebook::session::Notebook; |
| 18 | use notebook::{EditStatus, PendingEdit, Remote}; |
| 19 | use onestore::{CommitError, CommitState, ExGuid, Stamp, Transaction}; |
| 20 | use std::{ |
| 21 | collections::BTreeMap, |
| 22 | io, |
| 23 | path::{Path, PathBuf}, |
| 24 | process::{Child, Command, Stdio}, |
| 25 | time::{Duration, Instant}, |
| 26 | }; |
| 27 | |
| 28 | const TEST: &str = "edit_receipts_survive_guest_and_host_process_death"; |
| 29 | const ROLE: &str = "SNOWBOUND_LIVE_RECOVERY_ROLE"; |
| 30 | const ROOT: &str = "SNOWBOUND_LIVE_RECOVERY_ROOT"; |
| 31 | const URL: &str = "SNOWBOUND_LIVE_RECOVERY_URL"; |
| 32 | const BOUNDARY: &str = "SNOWBOUND_LIVE_RECOVERY_BOUNDARY"; |
| 33 | const INSERTION: &str = " CRASH_RECOVERY"; |
| 34 | |
| 35 | fn body(image: &[u8]) -> (ExGuid, ExGuid, String) { |
| 36 | let store = onestore::Store::parse(image).unwrap(); |
| 37 | assert!(store.checksum_mismatches.is_empty()); |
| 38 | let index = onestore::RevisionIndex::parse(&store).unwrap(); |
| 39 | index.validate_current().unwrap(); |
| 40 | let document = onestore::document::Document::parse(&index).unwrap(); |
| 41 | let space = document.pages().unwrap()[0].0; |
| 42 | let (text, content) = document.spaces[&space] |
| 43 | .active() |
| 44 | .unwrap() |
| 45 | .nodes |
| 46 | .iter() |
| 47 | .find_map(|(id, node)| match &node.kind { |
| 48 | onestore::document::Kind::RichText { text, .. } => Some((*id, text.clone())), |
| 49 | _ => None, |
| 50 | }) |
| 51 | .unwrap(); |
| 52 | (space, text, content) |
| 53 | } |
| 54 | |
| 55 | fn stopped_at_boundary(root: &Path) -> ! { |
| 56 | std::fs::write(root.join("guest-ready"), b"").unwrap(); |
| 57 | loop { |
| 58 | std::thread::park(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | struct Interrupted<'a> { |
| 63 | remote: HostedRemote, |
| 64 | boundary: &'a str, |
| 65 | root: &'a Path, |
| 66 | } |
| 67 | impl Remote for Interrupted<'_> { |
| 68 | fn read(&mut self) -> io::Result<Vec<u8>> { |
| 69 | self.remote.read() |
| 70 | } |
| 71 | fn stamp(&mut self) -> io::Result<Stamp> { |
| 72 | self.remote.stamp() |
| 73 | } |
| 74 | fn publish(&mut self, transaction: &Transaction) -> Result<(), CommitError> { |
| 75 | self.remote.publish(transaction) |
| 76 | } |
| 77 | fn confirm(&mut self, stamp: &Stamp) -> Result<(), CommitError> { |
| 78 | self.remote.confirm(stamp) |
| 79 | } |
| 80 | fn accepts_edits(&self) -> bool { |
| 81 | true |
| 82 | } |
| 83 | fn publish_edits( |
| 84 | &mut self, |
| 85 | transaction: &Transaction, |
| 86 | edits: &[PendingEdit], |
| 87 | revisions: &BTreeMap<ExGuid, ExGuid>, |
| 88 | ) -> Result<(), CommitError> { |
| 89 | if self.boundary == "unsent" { |
| 90 | stopped_at_boundary(self.root); |
| 91 | } |
| 92 | self.remote.publish_edits(transaction, edits, revisions)?; |
| 93 | if self.boundary == "lost" { |
| 94 | return Err(CommitError { |
| 95 | state: CommitState::Unknown, |
| 96 | error: io::ErrorKind::BrokenPipe.into(), |
| 97 | }); |
| 98 | } |
| 99 | Ok(()) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if let Ok(role) = std::env::var(ROLE) { |
| 104 | let root = PathBuf::from(std::env::var_os(ROOT).unwrap()); |
| 105 | let url = std::env::var(URL).unwrap(); |
| 106 | let boundary = std::env::var(BOUNDARY).unwrap(); |
| 107 | if role == "host" { |
| 108 | let saved = root.join("sharing.json"); |
| 109 | let sharing = if saved.exists() { |
| 110 | serde_json::from_slice(&std::fs::read(&saved).unwrap()).unwrap() |
| 111 | } else { |
| 112 | Sharing::new("").unwrap() |
| 113 | }; |
| 114 | let host = Host::start( |
| 115 | Notebook::open(root.join("Garden"), root.join("host")) |
| 116 | .unwrap() |
| 117 | .into_storage(), |
| 118 | hello("Ada"), |
| 119 | sharing, |
| 120 | "Garden", |
| 121 | None, |
| 122 | Some(&url), |
| 123 | || {}, |
| 124 | move |sharing| std::fs::write(&saved, serde_json::to_vec(sharing)?), |
| 125 | ) |
| 126 | .unwrap(); |
| 127 | std::fs::write(root.join("code"), code(&host)).unwrap(); |
| 128 | std::fs::write(root.join("host-ready"), b"").unwrap(); |
| 129 | loop { |
| 130 | std::thread::park(); |
| 131 | } |
| 132 | } |
| 133 | let credentials = root.join("guest.json"); |
| 134 | let (share, secret): ([u8; 16], [u8; 16]) = if role == "guest" { |
| 135 | let code = std::fs::read_to_string(root.join("code")).unwrap(); |
| 136 | let welcome = share::join(hello("Alice"), &code, "", None, Some(&url)).unwrap(); |
| 137 | let issued = (welcome.share, welcome.secret); |
| 138 | std::fs::write(&credentials, serde_json::to_vec(&issued).unwrap()).unwrap(); |
| 139 | issued |
| 140 | } else { |
| 141 | serde_json::from_slice(&std::fs::read(credentials).unwrap()).unwrap() |
| 142 | }; |
| 143 | let guest = Guest::start(hello("Alice"), share, secret, None, Some(&url), || {}).unwrap(); |
| 144 | until("the restarted guest never met its host", || { |
| 145 | guest.host().is_some() |
| 146 | }); |
| 147 | let database = root.join("replica.sqlite"); |
| 148 | let replica = if role == "guest" { |
| 149 | let image = std::fs::read(root.join("Garden/Garden.one")).unwrap(); |
| 150 | let (space, text, content) = body(&image); |
| 151 | let replica = Replica::create(&database, &image).unwrap(); |
| 152 | let end = u32::try_from(content.encode_utf16().count()).unwrap(); |
| 153 | let id = replica |
| 154 | .apply( |
| 155 | "Alice", |
| 156 | Edit { |
| 157 | at: 134_000_000_000_000_000, |
| 158 | ops: vec![Op::Page { |
| 159 | space, |
| 160 | op: PageOp::Text { |
| 161 | text, |
| 162 | range: end..end, |
| 163 | with: INSERTION.into(), |
| 164 | }, |
| 165 | }], |
| 166 | }, |
| 167 | ) |
| 168 | .unwrap(); |
| 169 | std::fs::write(root.join("edit-id"), id.to_string()).unwrap(); |
| 170 | replica |
| 171 | } else { |
| 172 | Replica::open(&database).unwrap() |
| 173 | }; |
| 174 | let id = std::fs::read_to_string(root.join("edit-id")) |
| 175 | .unwrap() |
| 176 | .parse() |
| 177 | .unwrap(); |
| 178 | if role == "guest" { |
| 179 | if boundary == "before" { |
| 180 | stopped_at_boundary(&root); |
| 181 | } |
| 182 | let mut remote = Interrupted { |
| 183 | remote: HostedRemote::new(&guest, "Garden.one"), |
| 184 | boundary: &boundary, |
| 185 | root: &root, |
| 186 | }; |
| 187 | let result = replica.sync_once(&mut remote); |
| 188 | if boundary == "lost" { |
| 189 | assert!(matches!( |
| 190 | result, |
| 191 | Err(notebook::Error::Remote(CommitError { |
| 192 | state: CommitState::Unknown, |
| 193 | .. |
| 194 | })) |
| 195 | )); |
| 196 | assert!(matches!( |
| 197 | replica.status(id).unwrap(), |
| 198 | Some(EditStatus::AwaitingConfirmation { .. }) |
| 199 | )); |
| 200 | } else { |
| 201 | result.unwrap(); |
| 202 | assert!(matches!( |
| 203 | replica.status(id).unwrap(), |
| 204 | Some(EditStatus::Published { .. }) |
| 205 | )); |
| 206 | } |
| 207 | stopped_at_boundary(&root); |
| 208 | } |
| 209 | let mut remote = HostedRemote::new(&guest, "Garden.one"); |
| 210 | for _ in 0..5 { |
| 211 | replica.sync_once(&mut remote).unwrap(); |
| 212 | if matches!( |
| 213 | replica.status(id).unwrap(), |
| 214 | Some(EditStatus::Published { .. }) |
| 215 | ) { |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | assert!(matches!( |
| 220 | replica.status(id).unwrap(), |
| 221 | Some(EditStatus::Published { .. }) |
| 222 | )); |
| 223 | assert!(replica.pending().unwrap().is_empty()); |
| 224 | let settled = std::fs::read(root.join("Garden/Garden.one")).unwrap(); |
| 225 | for _ in 0..3 { |
| 226 | replica.sync_once(&mut remote).unwrap(); |
| 227 | assert_eq!( |
| 228 | std::fs::read(root.join("Garden/Garden.one")).unwrap(), |
| 229 | settled |
| 230 | ); |
| 231 | } |
| 232 | assert_eq!(replica.snapshot().unwrap(), settled); |
| 233 | drop(replica); |
| 234 | std::process::exit(0); |
| 235 | } |
| 236 | |
| 237 | struct Process { |
| 238 | child: Child, |
| 239 | log: PathBuf, |
| 240 | } |
| 241 | impl Process { |
| 242 | fn start(root: &Path, url: &str, role: &str, boundary: &str) -> Self { |
| 243 | let log = root.join(format!("{role}.log")); |
| 244 | let output = std::fs::OpenOptions::new() |
| 245 | .create(true) |
| 246 | .append(true) |
| 247 | .open(&log) |
| 248 | .unwrap(); |
| 249 | let child = Command::new(std::env::current_exe().unwrap()) |
| 250 | .args(["--exact", TEST, "--nocapture"]) |
| 251 | .env(ROLE, role) |
| 252 | .env(ROOT, root) |
| 253 | .env(URL, url) |
| 254 | .env(BOUNDARY, boundary) |
| 255 | .stdout(output.try_clone().unwrap()) |
| 256 | .stderr(Stdio::from(output)) |
| 257 | .spawn() |
| 258 | .unwrap(); |
| 259 | Self { child, log } |
| 260 | } |
| 261 | |
| 262 | fn ready(&mut self, marker: &Path) { |
| 263 | let deadline = Instant::now() + Duration::from_secs(30); |
| 264 | while !marker.exists() { |
| 265 | assert!( |
| 266 | self.child.try_wait().unwrap().is_none() && Instant::now() < deadline, |
| 267 | "{}: {}", |
| 268 | marker.display(), |
| 269 | std::fs::read_to_string(&self.log).unwrap() |
| 270 | ); |
| 271 | std::thread::sleep(Duration::from_millis(20)); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | fn kill(&mut self) { |
| 276 | self.child.kill().unwrap(); |
| 277 | assert!(!self.child.wait().unwrap().success()); |
| 278 | } |
| 279 | } |
| 280 | impl Drop for Process { |
| 281 | fn drop(&mut self) { |
| 282 | let _ = self.child.kill(); |
| 283 | let _ = self.child.wait(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | for boundary in ["before", "unsent", "lost", "confirmed"] { |
| 288 | let directory = tempfile::tempdir().unwrap(); |
| 289 | let root = directory.path(); |
| 290 | let folder = notebook(root); |
| 291 | let file = folder.join("Garden.one"); |
| 292 | let original = |
| 293 | include_bytes!("../../../corpus/conflict-page/native/conflict/notebook/synthetic.one"); |
| 294 | std::fs::write(&file, original).unwrap(); |
| 295 | let original_conflicts = server::conflicts(original); |
| 296 | assert!(!original_conflicts.is_empty()); |
| 297 | let (space, _, content) = body(original); |
| 298 | let sealed = std::fs::read(folder.join("Sealed.one")).unwrap(); |
| 299 | let url = relay(Default::default()); |
| 300 | let mut host = Process::start(root, &url, "host", boundary); |
| 301 | host.ready(&root.join("host-ready")); |
| 302 | let mut guest = Process::start(root, &url, "guest", boundary); |
| 303 | guest.ready(&root.join("guest-ready")); |
| 304 | guest.kill(); |
| 305 | host.kill(); |
| 306 | |
| 307 | let id: u64 = std::fs::read_to_string(root.join("edit-id")) |
| 308 | .unwrap() |
| 309 | .parse() |
| 310 | .unwrap(); |
| 311 | let database = root.join("replica.sqlite"); |
| 312 | let replica = Replica::open(&database).unwrap(); |
| 313 | assert_eq!( |
| 314 | body(&replica.snapshot().unwrap()).2, |
| 315 | format!("{content}{INSERTION}") |
| 316 | ); |
| 317 | if boundary == "confirmed" { |
| 318 | assert!(matches!( |
| 319 | replica.status(id).unwrap(), |
| 320 | Some(EditStatus::Published { .. }) |
| 321 | )); |
| 322 | assert!(replica.pending().unwrap().is_empty()); |
| 323 | } else if matches!(boundary, "lost" | "unsent") { |
| 324 | assert!(matches!( |
| 325 | replica.status(id).unwrap(), |
| 326 | Some(EditStatus::AwaitingConfirmation { .. }) |
| 327 | )); |
| 328 | } else { |
| 329 | assert_eq!(replica.status(id).unwrap(), Some(EditStatus::Pending)); |
| 330 | } |
| 331 | if boundary != "confirmed" { |
| 332 | assert_eq!(replica.pending().unwrap()[0].id, id); |
| 333 | assert_eq!(replica.pending().unwrap().len(), 1); |
| 334 | } |
| 335 | drop(replica); |
| 336 | let committed = std::fs::read(&file).unwrap(); |
| 337 | if matches!(boundary, "before" | "unsent") { |
| 338 | assert_eq!(committed, original); |
| 339 | } else { |
| 340 | assert_eq!(body(&committed).2, format!("{content}{INSERTION}")); |
| 341 | } |
| 342 | let sharing: Sharing = |
| 343 | serde_json::from_slice(&std::fs::read(root.join("sharing.json")).unwrap()).unwrap(); |
| 344 | let credentials = std::fs::read(root.join("guest.json")).unwrap(); |
| 345 | std::fs::remove_file(root.join("host-ready")).unwrap(); |
| 346 | let mut host = Process::start(root, &url, "host", boundary); |
| 347 | host.ready(&root.join("host-ready")); |
| 348 | let mut resumed = Process::start(root, &url, "resume", boundary); |
| 349 | let deadline = Instant::now() + Duration::from_secs(30); |
| 350 | let status = loop { |
| 351 | if let Some(status) = resumed.child.try_wait().unwrap() { |
| 352 | break status; |
| 353 | } |
| 354 | assert!(Instant::now() < deadline, "resumed guest timed out"); |
| 355 | std::thread::sleep(Duration::from_millis(20)); |
| 356 | }; |
| 357 | if !status.success() { |
| 358 | let log = std::fs::read_to_string(&resumed.log).unwrap(); |
| 359 | host.kill(); |
| 360 | let kept = directory.keep(); |
| 361 | panic!("{}: {log}", kept.display()); |
| 362 | } |
| 363 | host.kill(); |
| 364 | let result = std::fs::read(&file).unwrap(); |
| 365 | assert_eq!(body(&result).2, format!("{content}{INSERTION}")); |
| 366 | if matches!(boundary, "lost" | "confirmed") { |
| 367 | assert_eq!( |
| 368 | &result[1024..], |
| 369 | &committed[1024..], |
| 370 | "settlement must not append another revision" |
| 371 | ); |
| 372 | } |
| 373 | let revisions = |image: &[u8]| { |
| 374 | let store = onestore::Store::parse(image).unwrap(); |
| 375 | onestore::RevisionIndex::parse(&store).unwrap().spaces[&space] |
| 376 | .revisions |
| 377 | .len() |
| 378 | }; |
| 379 | assert_eq!(revisions(&result), revisions(original) + 1); |
| 380 | assert_eq!(server::conflicts(&result), original_conflicts); |
| 381 | assert_eq!(std::fs::read(folder.join("Sealed.one")).unwrap(), sealed); |
| 382 | let restarted: Sharing = |
| 383 | serde_json::from_slice(&std::fs::read(root.join("sharing.json")).unwrap()).unwrap(); |
| 384 | assert_eq!(restarted.share, sharing.share); |
| 385 | assert_eq!(restarted.secret, sharing.secret); |
| 386 | let devices = |sharing: &Sharing| { |
| 387 | (sharing.members.iter()) |
| 388 | .map(|device| (device.secret, device.name.clone())) |
| 389 | .collect::<Vec<_>>() |
| 390 | }; |
| 391 | assert_eq!(devices(&restarted), devices(&sharing)); |
| 392 | assert_eq!(std::fs::read(root.join("guest.json")).unwrap(), credentials); |
| 393 | let replica = Replica::open(&database).unwrap(); |
| 394 | assert!(matches!( |
| 395 | replica.status(id).unwrap(), |
| 396 | Some(EditStatus::Published { .. }) |
| 397 | )); |
| 398 | assert!(replica.pending().unwrap().is_empty()); |
| 399 | assert_eq!(replica.snapshot().unwrap(), result); |
| 400 | drop(replica); |
| 401 | let database = rusqlite::Connection::open(&database).unwrap(); |
| 402 | assert_eq!( |
| 403 | database |
| 404 | .query_row("PRAGMA integrity_check", [], |row| row.get::<_, String>(0)) |
| 405 | .unwrap(), |
| 406 | "ok" |
| 407 | ); |
| 408 | println!( |
| 409 | "{boundary}: original edit {id} published once, both processes restarted, SQLite integrity ok" |
| 410 | ); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | #[test] |
| 415 | fn concurrent_batches_keep_receipts_and_rebase_later_keystrokes() { |
| 416 | let directory = tempfile::tempdir().unwrap(); |
| 417 | let folder = notebook(directory.path()); |
| 418 | let url = relay(Default::default()); |
| 419 | let sharing = Sharing::new("").unwrap(); |
| 420 | let host = host(&folder, &directory.path().join("host"), &sharing, &url); |
| 421 | let code = code(&host); |
| 422 | let (alice, _) = guest("Alice", &code, &url, &directory.path().join("alice")); |
| 423 | let (bob, _) = guest("Bob", &code, &url, &directory.path().join("bob")); |
| 424 | let file = folder.join("Garden.one"); |
| 425 | let original = std::fs::read(&file).unwrap(); |
| 426 | let (space, text, _) = server::text(&original); |
| 427 | let edit = |with: &str, at| Edit { |
| 428 | at: 134_000_000_000_000_000, |
| 429 | ops: vec![Op::Page { |
| 430 | space, |
| 431 | op: PageOp::Text { |
| 432 | text, |
| 433 | range: at..at, |
| 434 | with: with.into(), |
| 435 | }, |
| 436 | }], |
| 437 | }; |
| 438 | let a = Arc::new( |
| 439 | Replica::open_or_create(directory.path().join("a.sqlite"), None, || { |
| 440 | Ok(original.clone()) |
| 441 | }) |
| 442 | .unwrap(), |
| 443 | ); |
| 444 | let b = Arc::new( |
| 445 | Replica::open_or_create(directory.path().join("b.sqlite"), None, || { |
| 446 | Ok(original.clone()) |
| 447 | }) |
| 448 | .unwrap(), |
| 449 | ); |
| 450 | let first = a.apply("Alice", edit("A", 13)).unwrap(); |
| 451 | let second = b.apply("Bob", edit("B", 0)).unwrap(); |
| 452 | let barrier = Arc::new(Barrier::new(2)); |
| 453 | std::thread::scope(|scope| { |
| 454 | for (replica, guest) in [(&a, &alice), (&b, &bob)] { |
| 455 | let barrier = Arc::clone(&barrier); |
| 456 | scope.spawn(move || { |
| 457 | barrier.wait(); |
| 458 | replica |
| 459 | .sync_once(&mut HostedRemote::new(guest, "Garden.one")) |
| 460 | .unwrap(); |
| 461 | }); |
| 462 | } |
| 463 | }); |
| 464 | assert!(matches!( |
| 465 | a.status(first).unwrap(), |
| 466 | Some(notebook::EditStatus::Published { .. }) |
| 467 | )); |
| 468 | assert!(matches!( |
| 469 | b.status(second).unwrap(), |
| 470 | Some(notebook::EditStatus::Published { .. }) |
| 471 | )); |
| 472 | let next = server::text(&a.snapshot().unwrap()).2.find('A').unwrap() as u32 + 1; |
| 473 | let third = a.apply("Alice", edit("a", next)).unwrap(); |
| 474 | let mut remote = HostedRemote::new(&alice, "Garden.one"); |
| 475 | for _ in 0..5 { |
| 476 | a.sync_once(&mut remote).unwrap(); |
| 477 | if matches!( |
| 478 | a.status(third).unwrap(), |
| 479 | Some(notebook::EditStatus::Published { .. }) |
| 480 | ) { |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | let result = std::fs::read(&file).unwrap(); |
| 485 | let result_text = server::text(&result).2; |
| 486 | assert!(result_text.contains("Aa"), "{result_text}"); |
| 487 | assert!(result_text.contains('B'), "{result_text}"); |
| 488 | let arena = onestore::Arena::default(); |
| 489 | let mut section = onestore::Section::open(&arena, result).unwrap(); |
| 490 | assert!(section.conflicts().unwrap().is_empty()); |
| 491 | } |
| 492 | |
| 493 | /// A batch whose request the relay loses with the guest's connection is uncertain; once the |
| 494 | /// guest is back, the host hears it again and it lands once. |
| 495 | #[test] |
| 496 | fn a_batch_lost_on_the_way_lands_once_the_guest_is_back() { |
| 497 | use notebook::{EditStatus, Error, Remote, live::share::Guest}; |
| 498 | use onestore::{CommitError, CommitState}; |
| 499 | |
| 500 | let directory = tempfile::tempdir().unwrap(); |
| 501 | let folder = notebook(directory.path()); |
| 502 | let url = relay(Default::default()); |
| 503 | let sharing = Sharing::new("").unwrap(); |
| 504 | let host = host(&folder, &directory.path().join("host"), &sharing, &url); |
| 505 | let welcome = |
| 506 | notebook::live::share::join(hello("Grace"), &code(&host), "", None, Some(&url)).unwrap(); |
| 507 | let relay = PresenceRelay::new(&url, &welcome.room); |
| 508 | let guest = Guest::start( |
| 509 | hello("Grace"), |
| 510 | welcome.share, |
| 511 | welcome.secret, |
| 512 | None, |
| 513 | Some(&relay.url), |
| 514 | || {}, |
| 515 | ) |
| 516 | .unwrap(); |
| 517 | until("the host was never met", || guest.host().is_some()); |
| 518 | let file = folder.join("Garden.one"); |
| 519 | let original = std::fs::read(&file).unwrap(); |
| 520 | let (space, text, _) = server::text(&original); |
| 521 | let typed = |with: &str| Edit { |
| 522 | at: 134_000_000_000_000_000, |
| 523 | ops: vec![Op::Page { |
| 524 | space, |
| 525 | op: PageOp::Text { |
| 526 | text, |
| 527 | range: 0..0, |
| 528 | with: with.into(), |
| 529 | }, |
| 530 | }], |
| 531 | }; |
| 532 | let replica = Replica::open_or_create(directory.path().join("grace.sqlite"), None, || { |
| 533 | Ok(original.clone()) |
| 534 | }) |
| 535 | .unwrap(); |
| 536 | let remote = std::cell::RefCell::new(HostedRemote::new(&guest, "Garden.one")); |
| 537 | let first = replica.apply("Grace", typed("A")).unwrap(); |
| 538 | until("the first edit was never published", || { |
| 539 | let _ = replica.sync_once(&mut *remote.borrow_mut()); |
| 540 | matches!( |
| 541 | replica.status(first).unwrap(), |
| 542 | Some(EditStatus::Published { .. }) |
| 543 | ) |
| 544 | }); |
| 545 | until("the host's stamp was never known without asking", || { |
| 546 | let before = relay.state.lock().unwrap().access_bytes; |
| 547 | remote.borrow_mut().stamp().unwrap(); |
| 548 | relay.state.lock().unwrap().access_bytes == before |
| 549 | }); |
| 550 | |
| 551 | relay.state.lock().unwrap().swallow = true; |
| 552 | let second = replica.apply("Grace", typed("B")).unwrap(); |
| 553 | assert!(matches!( |
| 554 | replica.sync_once(&mut *remote.borrow_mut()), |
| 555 | Err(Error::Remote(CommitError { |
| 556 | state: CommitState::Unknown, |
| 557 | .. |
| 558 | })) |
| 559 | )); |
| 560 | assert!(matches!( |
| 561 | replica.status(second).unwrap(), |
| 562 | Some(EditStatus::AwaitingConfirmation { .. }) |
| 563 | )); |
| 564 | // OneNote on the host holds the section as the batch comes again, and lets it go. The |
| 565 | // app's sync worker keeps its remote after such a refusal. |
| 566 | let held = std::fs::File::open(&file).unwrap(); |
| 567 | held.lock_shared().unwrap(); |
| 568 | let remote = std::cell::RefCell::new(HostedRemote::new(&guest, "Garden.one")); |
| 569 | until( |
| 570 | "the host never refused the batch while OneNote held it", |
| 571 | || { |
| 572 | matches!( |
| 573 | replica.sync_once(&mut *remote.borrow_mut()), |
| 574 | Err(Error::Remote(CommitError { |
| 575 | state: CommitState::NotCommitted, |
| 576 | .. |
| 577 | })) |
| 578 | ) |
| 579 | }, |
| 580 | ); |
| 581 | drop(held); |
| 582 | until("the uncertain batch never landed", || { |
| 583 | let _ = replica.sync_once(&mut *remote.borrow_mut()); |
| 584 | matches!( |
| 585 | replica.status(second).unwrap(), |
| 586 | Some(EditStatus::Published { .. }) |
| 587 | ) |
| 588 | }); |
| 589 | assert_eq!( |
| 590 | server::text(&std::fs::read(&file).unwrap()).2, |
| 591 | "BAOriginal text" |
| 592 | ); |
| 593 | } |
| 594 | |
| 595 | /// A section closed while its batch is uncertain sends the batch again from the |
| 596 | /// background, which keeps a notebook's closed sections in sync. |
| 597 | #[test] |
| 598 | fn a_closed_section_s_uncertain_batch_lands_from_the_background() { |
| 599 | use notebook::{ |
| 600 | EditStatus, Error, Remote, |
| 601 | live::share::Guest, |
| 602 | session::{Background, Notebook}, |
| 603 | }; |
| 604 | use onestore::{CommitError, CommitState}; |
| 605 | |
| 606 | let directory = tempfile::tempdir().unwrap(); |
| 607 | let folder = notebook(directory.path()); |
| 608 | let url = relay(Default::default()); |
| 609 | let sharing = Sharing::new("").unwrap(); |
| 610 | let host = host(&folder, &directory.path().join("host"), &sharing, &url); |
| 611 | let welcome = |
| 612 | notebook::live::share::join(hello("Grace"), &code(&host), "", None, Some(&url)).unwrap(); |
| 613 | let relay = PresenceRelay::new(&url, &welcome.room); |
| 614 | let guest = Guest::start( |
| 615 | hello("Grace"), |
| 616 | welcome.share, |
| 617 | welcome.secret, |
| 618 | None, |
| 619 | Some(&relay.url), |
| 620 | || {}, |
| 621 | ) |
| 622 | .unwrap(); |
| 623 | until("the host was never met", || guest.host().is_some()); |
| 624 | let mut notebook = |
| 625 | Notebook::open_hosted(Arc::clone(&guest), directory.path().join("grace")).unwrap(); |
| 626 | let file = folder.join("Garden.one"); |
| 627 | let original = std::fs::read(&file).unwrap(); |
| 628 | let (space, text, _) = server::text(&original); |
| 629 | let path = notebook.replica_path("Garden.one").unwrap(); |
| 630 | std::fs::create_dir_all(path.parent().unwrap()).unwrap(); |
| 631 | let replica = Replica::open_or_create(&path, None, || Ok(original.clone())).unwrap(); |
| 632 | let typed = |with: &str| Edit { |
| 633 | at: 134_000_000_000_000_000, |
| 634 | ops: vec![Op::Page { |
| 635 | space, |
| 636 | op: PageOp::Text { |
| 637 | text, |
| 638 | range: 0..0, |
| 639 | with: with.into(), |
| 640 | }, |
| 641 | }], |
| 642 | }; |
| 643 | let first = replica.apply("Grace", typed("A")).unwrap(); |
| 644 | until("the first edit was never published", || { |
| 645 | let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Garden.one")); |
| 646 | matches!( |
| 647 | replica.status(first).unwrap(), |
| 648 | Some(EditStatus::Published { .. }) |
| 649 | ) |
| 650 | }); |
| 651 | until("the host's stamp was never known without asking", || { |
| 652 | let before = relay.state.lock().unwrap().access_bytes; |
| 653 | HostedRemote::new(&guest, "Garden.one").stamp().unwrap(); |
| 654 | relay.state.lock().unwrap().access_bytes == before |
| 655 | }); |
| 656 | relay.state.lock().unwrap().swallow = true; |
| 657 | let id = replica.apply("Grace", typed("B")).unwrap(); |
| 658 | assert!(matches!( |
| 659 | replica.sync_once(&mut HostedRemote::new(&guest, "Garden.one")), |
| 660 | Err(Error::Remote(CommitError { |
| 661 | state: CommitState::Unknown, |
| 662 | .. |
| 663 | })) |
| 664 | )); |
| 665 | drop(replica); |
| 666 | let background = Background::hosted(Arc::clone(&guest), || {}).unwrap(); |
| 667 | background.watch(notebook.replicas()); |
| 668 | // As the app does, the background is woken whenever the guest meets its host again. |
| 669 | let reached = std::cell::Cell::new(guest.host().is_some()); |
| 670 | until("the closed section's batch never landed", || { |
| 671 | let now = guest.host().is_some(); |
| 672 | if now && !reached.replace(now) { |
| 673 | background.wake(); |
| 674 | } |
| 675 | server::text(&std::fs::read(&file).unwrap()).2 == "BAOriginal text" |
| 676 | }); |
| 677 | background.stop(); |
| 678 | let replica = Replica::open(&path).unwrap(); |
| 679 | assert!(matches!( |
| 680 | replica.status(id).unwrap(), |
| 681 | Some(EditStatus::Published { .. }) |
| 682 | )); |
| 683 | } |
| 684 | |
| 685 | /// A batch lost on the way to a host that restarts, while the section moves on, is |
| 686 | /// rebased onto the host's section once the new host no longer holds its base, and lands |
| 687 | /// beside the host's own edit. |
| 688 | #[test] |
| 689 | fn a_lost_batch_whose_base_the_host_dropped_is_rebased() { |
| 690 | use notebook::{EditStatus, Remote, live::share::Guest}; |
| 691 | |
| 692 | let directory = tempfile::tempdir().unwrap(); |
| 693 | let folder = notebook(directory.path()); |
| 694 | let url = relay(Default::default()); |
| 695 | let sharing = Sharing::new("").unwrap(); |
| 696 | let host = host(&folder, &directory.path().join("host"), &sharing, &url); |
| 697 | let welcome = |
| 698 | notebook::live::share::join(hello("Grace"), &code(&host), "", None, Some(&url)).unwrap(); |
| 699 | let relay = PresenceRelay::new(&url, &welcome.room); |
| 700 | let guest = Guest::start( |
| 701 | hello("Grace"), |
| 702 | welcome.share, |
| 703 | welcome.secret, |
| 704 | None, |
| 705 | Some(&relay.url), |
| 706 | || {}, |
| 707 | ) |
| 708 | .unwrap(); |
| 709 | until("the host was never met", || guest.host().is_some()); |
| 710 | let file = folder.join("Garden.one"); |
| 711 | let original = std::fs::read(&file).unwrap(); |
| 712 | let (space, text, _) = server::text(&original); |
| 713 | let typed = |with: &str| Edit { |
| 714 | at: 134_000_000_000_000_000, |
| 715 | ops: vec![Op::Page { |
| 716 | space, |
| 717 | op: PageOp::Text { |
| 718 | text, |
| 719 | range: 0..0, |
| 720 | with: with.into(), |
| 721 | }, |
| 722 | }], |
| 723 | }; |
| 724 | let replica = Replica::open_or_create(directory.path().join("grace.sqlite"), None, || { |
| 725 | Ok(original.clone()) |
| 726 | }) |
| 727 | .unwrap(); |
| 728 | let remote = std::cell::RefCell::new(HostedRemote::new(&guest, "Garden.one")); |
| 729 | let first = replica.apply("Grace", typed("A")).unwrap(); |
| 730 | until("the first edit was never published", || { |
| 731 | let _ = replica.sync_once(&mut *remote.borrow_mut()); |
| 732 | matches!( |
| 733 | replica.status(first).unwrap(), |
| 734 | Some(EditStatus::Published { .. }) |
| 735 | ) |
| 736 | }); |
| 737 | until("the host's stamp was never known without asking", || { |
| 738 | let before = relay.state.lock().unwrap().access_bytes; |
| 739 | remote.borrow_mut().stamp().unwrap(); |
| 740 | relay.state.lock().unwrap().access_bytes == before |
| 741 | }); |
| 742 | relay.state.lock().unwrap().swallow = true; |
| 743 | let second = replica.apply("Grace", typed("B")).unwrap(); |
| 744 | assert!(replica.sync_once(&mut *remote.borrow_mut()).is_err()); |
| 745 | assert!(matches!( |
| 746 | replica.status(second).unwrap(), |
| 747 | Some(EditStatus::AwaitingConfirmation { .. }) |
| 748 | )); |
| 749 | |
| 750 | // The host quits, edits the section itself, and shares again. |
| 751 | let saved = host.sharing(); |
| 752 | drop(host); |
| 753 | let image = std::fs::read(&file).unwrap(); |
| 754 | std::fs::write(&file, server::typed(&image, space, text, 14..14, "!")).unwrap(); |
| 755 | let _host = self::host(&folder, &directory.path().join("host"), &saved, &url); |
| 756 | until("the uncertain batch never landed", || { |
| 757 | let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Garden.one")); |
| 758 | matches!( |
| 759 | replica.status(second).unwrap(), |
| 760 | Some(EditStatus::Published { .. }) |
| 761 | ) |
| 762 | }); |
| 763 | assert_eq!( |
| 764 | server::text(&std::fs::read(&file).unwrap()).2, |
| 765 | "BAOriginal text!" |
| 766 | ); |
| 767 | } |
| 768 | |
| 769 | /// A protected section's commit is sent again as it was, whole: one the relay lost lands |
| 770 | /// once the guest is back, and one whose answer was lost after it landed is found in the |
| 771 | /// host's section rather than landing twice. |
| 772 | #[test] |
| 773 | fn a_protected_section_s_uncertain_commit_lands_once() { |
| 774 | use notebook::{EditStatus, Error, PendingEdit, Remote, live::share::Guest, session::Notebook}; |
| 775 | use onestore::{CommitError, CommitState, ExGuid, Stamp, Transaction}; |
| 776 | use std::collections::BTreeMap; |
| 777 | |
| 778 | /// A remote whose commits land but whose answers are lost. |
| 779 | struct Unanswered(HostedRemote); |
| 780 | impl Remote for Unanswered { |
| 781 | fn read(&mut self) -> std::io::Result<Vec<u8>> { |
| 782 | self.0.read() |
| 783 | } |
| 784 | fn stamp(&mut self) -> std::io::Result<Stamp> { |
| 785 | self.0.stamp() |
| 786 | } |
| 787 | fn publish(&mut self, transaction: &Transaction) -> Result<(), CommitError> { |
| 788 | self.0.publish(transaction)?; |
| 789 | Err(CommitError { |
| 790 | state: CommitState::Unknown, |
| 791 | error: std::io::ErrorKind::BrokenPipe.into(), |
| 792 | }) |
| 793 | } |
| 794 | fn confirm(&mut self, stamp: &Stamp) -> Result<(), CommitError> { |
| 795 | self.0.confirm(stamp) |
| 796 | } |
| 797 | fn accepts_edits(&self) -> bool { |
| 798 | self.0.accepts_edits() |
| 799 | } |
| 800 | fn publish_edits( |
| 801 | &mut self, |
| 802 | transaction: &Transaction, |
| 803 | _: &[PendingEdit], |
| 804 | _: &BTreeMap<ExGuid, ExGuid>, |
| 805 | ) -> Result<(), CommitError> { |
| 806 | self.publish(transaction) |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | let directory = tempfile::tempdir().unwrap(); |
| 811 | let folder = notebook(directory.path()); |
| 812 | let url = relay(Default::default()); |
| 813 | let sharing = Sharing::new("").unwrap(); |
| 814 | let host = host(&folder, &directory.path().join("host"), &sharing, &url); |
| 815 | let welcome = |
| 816 | notebook::live::share::join(hello("Grace"), &code(&host), "", None, Some(&url)).unwrap(); |
| 817 | let relay = PresenceRelay::new(&url, &welcome.room); |
| 818 | let guest = Guest::start( |
| 819 | hello("Grace"), |
| 820 | welcome.share, |
| 821 | welcome.secret, |
| 822 | None, |
| 823 | Some(&relay.url), |
| 824 | || {}, |
| 825 | ) |
| 826 | .unwrap(); |
| 827 | until("the host was never met", || guest.host().is_some()); |
| 828 | let notebook = |
| 829 | Notebook::open_hosted(Arc::clone(&guest), directory.path().join("grace")).unwrap(); |
| 830 | let key = notebook.unlock("Sealed.one", PASSWORD).unwrap(); |
| 831 | let file = folder.join("Sealed.one"); |
| 832 | let original = std::fs::read(&file).unwrap(); |
| 833 | let replica = |
| 834 | Replica::open_or_create(directory.path().join("sealed.sqlite"), Some(&key), || { |
| 835 | Ok(original.clone()) |
| 836 | }) |
| 837 | .unwrap(); |
| 838 | let arena = onestore::Arena::default(); |
| 839 | let mut unlocked = onestore::Section::unlock(&arena, original.clone(), &key).unwrap(); |
| 840 | let (space, ..) = unlocked.pages().unwrap()[0]; |
| 841 | let text = server::page_texts(&unlocked.page(space).unwrap()); |
| 842 | assert_eq!(text, ["Sealed text"]); |
| 843 | let paragraph = unlocked |
| 844 | .page(space) |
| 845 | .unwrap() |
| 846 | .objects |
| 847 | .iter() |
| 848 | .find_map(|object| match object { |
| 849 | onestore::page::PageObject::Outline(outline) => outline |
| 850 | .paragraphs |
| 851 | .iter() |
| 852 | .find_map(|p| p.text().map(|t| t.id)), |
| 853 | _ => None, |
| 854 | }) |
| 855 | .unwrap(); |
| 856 | let typed = |range: std::ops::Range<u32>, with: &str| Edit { |
| 857 | at: 134_000_000_000_000_000, |
| 858 | ops: vec![Op::Page { |
| 859 | space, |
| 860 | op: PageOp::Text { |
| 861 | text: paragraph, |
| 862 | range, |
| 863 | with: with.into(), |
| 864 | }, |
| 865 | }], |
| 866 | }; |
| 867 | let stored = || { |
| 868 | let arena = onestore::Arena::default(); |
| 869 | let section = |
| 870 | onestore::Section::unlock(&arena, std::fs::read(&file).unwrap(), &key).unwrap(); |
| 871 | server::page_texts(&section.page(space).unwrap()) |
| 872 | }; |
| 873 | let published = |id| { |
| 874 | matches!( |
| 875 | replica.status(id).unwrap(), |
| 876 | Some(EditStatus::Published { .. }) |
| 877 | ) |
| 878 | }; |
| 879 | let first = replica.apply("Grace", typed(0..0, "A")).unwrap(); |
| 880 | until("the first edit was never published", || { |
| 881 | let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Sealed.one")); |
| 882 | published(first) |
| 883 | }); |
| 884 | HostedRemote::new(&guest, "Sealed.one").read().unwrap(); |
| 885 | until("the host's stamp was never known without asking", || { |
| 886 | host.touched(&["Sealed.one".into()]); |
| 887 | let before = relay.state.lock().unwrap().access_bytes; |
| 888 | HostedRemote::new(&guest, "Sealed.one").stamp().unwrap(); |
| 889 | relay.state.lock().unwrap().access_bytes == before |
| 890 | }); |
| 891 | |
| 892 | relay.state.lock().unwrap().swallow = true; |
| 893 | let second = replica.apply("Grace", typed(0..0, "B")).unwrap(); |
| 894 | assert!(matches!( |
| 895 | replica.sync_once(&mut HostedRemote::new(&guest, "Sealed.one")), |
| 896 | Err(Error::Remote(CommitError { |
| 897 | state: CommitState::Unknown, |
| 898 | .. |
| 899 | })) |
| 900 | )); |
| 901 | until("the commit the relay lost never landed", || { |
| 902 | let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Sealed.one")); |
| 903 | published(second) |
| 904 | }); |
| 905 | assert_eq!(stored(), ["BASealed text"]); |
| 906 | |
| 907 | let third = replica.apply("Grace", typed(0..0, "C")).unwrap(); |
| 908 | until("the commit whose answer was lost never landed", || { |
| 909 | replica |
| 910 | .sync_once(&mut Unanswered(HostedRemote::new(&guest, "Sealed.one"))) |
| 911 | .is_err() |
| 912 | }); |
| 913 | assert_eq!(stored(), ["CBASealed text"]); |
| 914 | // The host deletes what the commit typed before the guest hears of it. |
| 915 | let image = std::fs::read(&file).unwrap(); |
| 916 | let arena = onestore::Arena::default(); |
| 917 | let mut section = onestore::Section::unlock(&arena, image.clone(), &key).unwrap(); |
| 918 | section.apply("Ada", &typed(0..1, "")).unwrap(); |
| 919 | let mut image = image; |
| 920 | section.seal().unwrap().unwrap().apply(&mut image).unwrap(); |
| 921 | std::fs::write(&file, image).unwrap(); |
| 922 | host.touched(&["Sealed.one".into()]); |
| 923 | until("the commit that landed was never confirmed", || { |
| 924 | let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Sealed.one")); |
| 925 | published(third) |
| 926 | }); |
| 927 | assert_eq!(stored(), ["BASealed text"]); |
| 928 | } |
| 929 | |
| 930 | /// A batch whose answer was lost after it landed, which the host then undid and OneNote's |
| 931 | /// Optimize dropped the revision of, is still known as landed by a host that restarted |
| 932 | /// before the guest sent it again: what the host undid stays undone. |
| 933 | #[test] |
| 934 | fn a_restarted_host_knows_a_landed_batch_optimize_dropped() { |
| 935 | use notebook::{ |
| 936 | EditStatus, PendingEdit, Remote, |
| 937 | live::share::{Guest, Host}, |
| 938 | session::Notebook, |
| 939 | }; |
| 940 | use onestore::{CommitError, CommitState, ExGuid, Stamp, Transaction}; |
| 941 | use std::{collections::BTreeMap, sync::Mutex}; |
| 942 | |
| 943 | /// A remote whose batches land but whose answers are lost. |
| 944 | struct Unanswered(HostedRemote); |
| 945 | impl Remote for Unanswered { |
| 946 | fn read(&mut self) -> std::io::Result<Vec<u8>> { |
| 947 | self.0.read() |
| 948 | } |
| 949 | fn stamp(&mut self) -> std::io::Result<Stamp> { |
| 950 | self.0.stamp() |
| 951 | } |
| 952 | fn publish(&mut self, transaction: &Transaction) -> Result<(), CommitError> { |
| 953 | self.0.publish(transaction) |
| 954 | } |
| 955 | fn confirm(&mut self, stamp: &Stamp) -> Result<(), CommitError> { |
| 956 | self.0.confirm(stamp) |
| 957 | } |
| 958 | fn accepts_edits(&self) -> bool { |
| 959 | self.0.accepts_edits() |
| 960 | } |
| 961 | fn publish_edits( |
| 962 | &mut self, |
| 963 | transaction: &Transaction, |
| 964 | edits: &[PendingEdit], |
| 965 | revisions: &BTreeMap<ExGuid, ExGuid>, |
| 966 | ) -> Result<(), CommitError> { |
| 967 | self.0.publish_edits(transaction, edits, revisions)?; |
| 968 | Err(CommitError { |
| 969 | state: CommitState::Unknown, |
| 970 | error: std::io::ErrorKind::BrokenPipe.into(), |
| 971 | }) |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | let directory = tempfile::tempdir().unwrap(); |
| 976 | let folder = notebook(directory.path()); |
| 977 | let url = relay(Default::default()); |
| 978 | let kept = Arc::new(Mutex::new(Sharing::new("").unwrap())); |
| 979 | let start = |sharing: Sharing| { |
| 980 | let kept = Arc::clone(&kept); |
| 981 | Host::start( |
| 982 | Notebook::open(&folder, directory.path().join("host")) |
| 983 | .unwrap() |
| 984 | .into_storage(), |
| 985 | hello("Ada"), |
| 986 | sharing, |
| 987 | "Garden", |
| 988 | None, |
| 989 | Some(&url), |
| 990 | || {}, |
| 991 | move |sharing| { |
| 992 | *kept.lock().unwrap() = sharing.clone(); |
| 993 | Ok(()) |
| 994 | }, |
| 995 | ) |
| 996 | .unwrap() |
| 997 | }; |
| 998 | let sharing = kept.lock().unwrap().clone(); |
| 999 | let host = start(sharing); |
| 1000 | let welcome = |
| 1001 | notebook::live::share::join(hello("Grace"), &code(&host), "", None, Some(&url)).unwrap(); |
| 1002 | let guest = Guest::start( |
| 1003 | hello("Grace"), |
| 1004 | welcome.share, |
| 1005 | welcome.secret, |
| 1006 | None, |
| 1007 | Some(&url), |
| 1008 | || {}, |
| 1009 | ) |
| 1010 | .unwrap(); |
| 1011 | until("the host was never met", || guest.host().is_some()); |
| 1012 | let file = folder.join("Garden.one"); |
| 1013 | let original = std::fs::read(&file).unwrap(); |
| 1014 | let (space, text, _) = server::text(&original); |
| 1015 | let typed = |range: std::ops::Range<u32>, with: &str| Edit { |
| 1016 | at: 134_000_000_000_000_000, |
| 1017 | ops: vec![Op::Page { |
| 1018 | space, |
| 1019 | op: PageOp::Text { |
| 1020 | text, |
| 1021 | range, |
| 1022 | with: with.into(), |
| 1023 | }, |
| 1024 | }], |
| 1025 | }; |
| 1026 | let replica = Replica::open_or_create(directory.path().join("grace.sqlite"), None, || { |
| 1027 | Ok(original.clone()) |
| 1028 | }) |
| 1029 | .unwrap(); |
| 1030 | let published = |id| { |
| 1031 | matches!( |
| 1032 | replica.status(id).unwrap(), |
| 1033 | Some(EditStatus::Published { .. }) |
| 1034 | ) |
| 1035 | }; |
| 1036 | let first = replica.apply("Grace", typed(0..0, "A")).unwrap(); |
| 1037 | until("the first edit was never published", || { |
| 1038 | let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Garden.one")); |
| 1039 | published(first) |
| 1040 | }); |
| 1041 | let second = replica.apply("Grace", typed(0..0, "B")).unwrap(); |
| 1042 | until("the batch never landed", || { |
| 1043 | replica |
| 1044 | .sync_once(&mut Unanswered(HostedRemote::new(&guest, "Garden.one"))) |
| 1045 | .is_err() |
| 1046 | }); |
| 1047 | assert_eq!( |
| 1048 | server::text(&std::fs::read(&file).unwrap()).2, |
| 1049 | "BAOriginal text" |
| 1050 | ); |
| 1051 | |
| 1052 | // The host deletes what the batch typed and quits; Optimize keeps the page's newest |
| 1053 | // revision alone, so the section holds neither the text nor the batch's revision. |
| 1054 | drop(host); |
| 1055 | let arena = onestore::Arena::default(); |
| 1056 | let mut optimized = onestore::Section::open(&arena, original.clone()).unwrap(); |
| 1057 | optimized.apply("Ada", &typed(0..0, "A")).unwrap(); |
| 1058 | let mut image = original; |
| 1059 | optimized |
| 1060 | .seal() |
| 1061 | .unwrap() |
| 1062 | .unwrap() |
| 1063 | .apply(&mut image) |
| 1064 | .unwrap(); |
| 1065 | std::fs::write(&file, image).unwrap(); |
| 1066 | let sharing = kept.lock().unwrap().clone(); |
| 1067 | let _host = start(sharing); |
| 1068 | until("the batch sent again was never confirmed", || { |
| 1069 | let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Garden.one")); |
| 1070 | published(second) |
| 1071 | }); |
| 1072 | assert_eq!( |
| 1073 | server::text(&std::fs::read(&file).unwrap()).2, |
| 1074 | "AOriginal text" |
| 1075 | ); |
| 1076 | assert_eq!( |
| 1077 | server::text(&replica.snapshot().unwrap()).2, |
| 1078 | "AOriginal text" |
| 1079 | ); |
| 1080 | } |
| 1081 | |
| 1082 | /// A host that stops sharing keeps nothing of it after, as its app forgets the share: what |
| 1083 | /// a guest's last batch noted is not kept once the share has stopped. |
| 1084 | #[test] |
| 1085 | fn a_stopped_share_is_not_kept_again() { |
| 1086 | use notebook::{EditStatus, live::share::Guest}; |
| 1087 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 1088 | |
| 1089 | let directory = tempfile::tempdir().unwrap(); |
| 1090 | let folder = notebook(directory.path()); |
| 1091 | let url = relay(Default::default()); |
| 1092 | let keeps = Arc::new(AtomicUsize::new(0)); |
| 1093 | let counted = Arc::clone(&keeps); |
| 1094 | let host = notebook::live::share::Host::start( |
| 1095 | notebook::session::Notebook::open(&folder, directory.path().join("host")) |
| 1096 | .unwrap() |
| 1097 | .into_storage(), |
| 1098 | hello("Ada"), |
| 1099 | Sharing::new("").unwrap(), |
| 1100 | "Garden", |
| 1101 | None, |
| 1102 | Some(&url), |
| 1103 | || {}, |
| 1104 | move |_| { |
| 1105 | counted.fetch_add(1, Ordering::SeqCst); |
| 1106 | Ok(()) |
| 1107 | }, |
| 1108 | ) |
| 1109 | .unwrap(); |
| 1110 | let welcome = |
| 1111 | notebook::live::share::join(hello("Grace"), &code(&host), "", None, Some(&url)).unwrap(); |
| 1112 | let guest = Guest::start( |
| 1113 | hello("Grace"), |
| 1114 | welcome.share, |
| 1115 | welcome.secret, |
| 1116 | None, |
| 1117 | Some(&url), |
| 1118 | || {}, |
| 1119 | ) |
| 1120 | .unwrap(); |
| 1121 | until("the host was never met", || guest.host().is_some()); |
| 1122 | let original = std::fs::read(folder.join("Garden.one")).unwrap(); |
| 1123 | let (space, text, _) = server::text(&original); |
| 1124 | let replica = Replica::open_or_create(directory.path().join("grace.sqlite"), None, || { |
| 1125 | Ok(original.clone()) |
| 1126 | }) |
| 1127 | .unwrap(); |
| 1128 | let edit = Edit { |
| 1129 | at: 134_000_000_000_000_000, |
| 1130 | ops: vec![Op::Page { |
| 1131 | space, |
| 1132 | op: PageOp::Text { |
| 1133 | text, |
| 1134 | range: 0..0, |
| 1135 | with: "A".into(), |
| 1136 | }, |
| 1137 | }], |
| 1138 | }; |
| 1139 | let id = replica.apply("Grace", edit).unwrap(); |
| 1140 | until("the edit was never published", || { |
| 1141 | let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Garden.one")); |
| 1142 | matches!( |
| 1143 | replica.status(id).unwrap(), |
| 1144 | Some(EditStatus::Published { .. }) |
| 1145 | ) |
| 1146 | }); |
| 1147 | host.stop(); |
| 1148 | let stopped = keeps.load(Ordering::SeqCst); |
| 1149 | std::thread::sleep(std::time::Duration::from_secs(3)); |
| 1150 | drop(host); |
| 1151 | assert_eq!(keeps.load(Ordering::SeqCst), stopped); |
| 1152 | } |