1use super::*;
2
3#[cfg(unix)]
4#[test]
5fn cached_sections_refuse_links_outside_the_notebook_and_dangling_links() {
6 let temporary = tempfile::tempdir().unwrap();
7 let folder = temporary.path().join("notebook");
8 let cache = temporary.path().join("cache");
9 let creation = PageCreation::new(None, Some("Mine"), "Fixture").unwrap();
10 let notebook = Notebook::create(&folder, &cache, 0x00f0_a0c0, &creation).unwrap();
11 let path = &notebook.catalog().sections[0].path;
12 notebook
13 .section(path, false, || {})
14 .unwrap()
15 .close()
16 .unwrap();
17 let file = folder.join(path);
18 let outside = temporary.path().join("outside.one");
19 fs::rename(&file, &outside).unwrap();
20 std::os::unix::fs::symlink(&outside, &file).unwrap();
21 assert!(notebook.section(path, false, || {}).is_err());
22 fs::remove_file(outside).unwrap();
23 assert!(notebook.section(path, false, || {}).is_err());
24}
25
26#[test]
27fn renaming_an_unlisted_section_creates_its_toc_entry_and_preserves_its_neighbor() {
28 let temporary = tempfile::tempdir().unwrap();
29 let folder = temporary.path().join("notebook");
30 let cache = temporary.path().join("cache");
31 let page = PageCreation::new(None, Some("Garden plan"), "Fixture").unwrap();
32 let mut notebook = Notebook::create(&folder, &cache, Notebook::NEW_COLOR, &page).unwrap();
33 let original = notebook.catalog().sections[0].path.clone();
34 let other = notebook.create_section("", "Other", &page).unwrap();
35 let identity = notebook.section_path(&original).unwrap().file_id;
36 let neighbor = notebook.section_path(&other).unwrap().file_id;
37 drop(notebook);
38 fs::remove_file(folder.join(TOC)).unwrap();
39 let mut notebook = Notebook::open(&folder, &cache).unwrap();
40 assert!(notebook.catalog().toc.is_none());
41 assert_eq!(notebook.catalog().sections.len(), 2);
42 assert_eq!(
43 notebook.rename(&original, "Delayed section").unwrap(),
44 "Delayed section.one"
45 );
46 assert!(!folder.join(&original).exists());
47 let check = |notebook: &Notebook| {
48 assert_eq!(notebook.catalog().sections.len(), 2);
49 assert!(notebook.section_path(&original).is_err());
50 assert_eq!(
51 notebook
52 .section_path("Delayed section.one")
53 .unwrap()
54 .file_id,
55 identity
56 );
57 assert_eq!(notebook.section_path(&other).unwrap().file_id, neighbor);
58 let toc = notebook.read_toc("").unwrap();
59 assert!(lists(&toc, identity).unwrap());
60 assert_eq!(
61 stored_pages(&notebook.read_section("Delayed section.one").unwrap()).unwrap()[0]
62 .page
63 .title,
64 "Garden plan"
65 );
66 };
67 check(&notebook);
68 drop(notebook);
69 check(&Notebook::open(&folder, temporary.path().join("cold-cache")).unwrap());
70 if let Some(export) = std::env::var_os("SNOWBOUND_RENAME_EXPORT") {
71 let export = Path::new(&export);
72 fs::create_dir_all(export).unwrap();
73 for entry in fs::read_dir(&folder).unwrap() {
74 let entry = entry.unwrap();
75 fs::copy(entry.path(), export.join(entry.file_name())).unwrap();
76 }
77 }
78}
79
80#[test]
81fn moving_discovered_entries_preserves_unregistered_source_tocs() {
82 for group in [false, true] {
83 for source_toc in ["missing", "empty", "unlisted", "registered"] {
84 let temporary = tempfile::tempdir().unwrap();
85 let folder = temporary.path().join("notebook");
86 let cache = temporary.path().join("cache");
87 let page = PageCreation::new(None, Some("Garden plan"), "Fixture").unwrap();
88 let mut notebook =
89 Notebook::create(&folder, &cache, Notebook::NEW_COLOR, &page).unwrap();
90 let first = notebook.catalog().sections[0].path.clone();
91 let neighbor = notebook.create_section("", "Neighbor", &page).unwrap();
92 let target = notebook.create_group("", "Target").unwrap();
93 let existing = notebook.create_section(&target, "Existing", &page).unwrap();
94 let (entry, section, identity) = if group {
95 let entry = notebook.create_group("", "Source").unwrap();
96 let section = notebook.create_section(&entry, "Inside", &page).unwrap();
97 let identity = notebook
98 .folder(&entry)
99 .unwrap()
100 .toc
101 .as_ref()
102 .unwrap()
103 .file_id;
104 (entry, section, identity)
105 } else {
106 let identity = notebook.section_path(&first).unwrap().file_id;
107 (first.clone(), first, identity)
108 };
109 let section_id = notebook.section_path(&section).unwrap().file_id;
110 let neighbor_bytes = fs::read(folder.join(&neighbor)).unwrap();
111 let existing_bytes = fs::read(folder.join(&existing)).unwrap();
112 if source_toc == "unlisted" {
113 notebook
114 .edit_toc("", &[onestore::TocEdit::Remove { identity }])
115 .unwrap();
116 }
117 drop(notebook);
118 if source_toc == "missing" {
119 fs::remove_file(folder.join(TOC)).unwrap();
120 } else if source_toc == "empty" {
121 fs::write(
122 folder.join(TOC),
123 onestore::create_table_of_contents(TOC, &[]).unwrap(),
124 )
125 .unwrap();
126 }
127 let before = fs::read(folder.join(TOC)).ok();
128 let mut notebook = Notebook::open(&folder, &cache).unwrap();
129 let order: Vec<_> = notebook
130 .catalog()
131 .order
132 .iter()
133 .filter(|path| **path != entry)
134 .cloned()
135 .collect();
136 let moved = catalog_path(&target, &entry);
137 assert_eq!(notebook.move_entry(&entry, &target).unwrap(), moved);
138 assert!(!folder.join(&entry).exists());
139 let moved_section = catalog_path(&target, &section);
140 let check = |notebook: &Notebook| {
141 assert_eq!(notebook.catalog().order, order);
142 assert_eq!(
143 notebook.section_path(&moved_section).unwrap().file_id,
144 section_id
145 );
146 assert!(lists(&notebook.read_toc(&target).unwrap(), identity).unwrap());
147 assert_eq!(
148 stored_pages(&notebook.read_section(&moved_section).unwrap()).unwrap()[0]
149 .page
150 .title,
151 "Garden plan"
152 );
153 if source_toc == "registered" {
154 let toc = notebook.read_toc("").unwrap();
155 assert!(!lists(&toc, identity).unwrap());
156 assert_eq!(
157 notebook.catalog().toc.as_ref().unwrap().color,
158 Some(Notebook::NEW_COLOR)
159 );
160 } else {
161 assert_eq!(fs::read(folder.join(TOC)).ok(), before);
162 }
163 assert_eq!(fs::read(folder.join(&neighbor)).unwrap(), neighbor_bytes);
164 assert_eq!(fs::read(folder.join(&existing)).unwrap(), existing_bytes);
165 };
166 check(&notebook);
167 drop(notebook);
168 check(&Notebook::open(&folder, temporary.path().join("cold-cache")).unwrap());
169 }
170 }
171}
172
173#[test]
174fn moving_an_entry_reads_its_registered_source_toc_before_renaming() {
175 let temporary = tempfile::tempdir().unwrap();
176 let folder = temporary.path().join("notebook");
177 let page = PageCreation::new(None, Some("Garden plan"), "Fixture").unwrap();
178 let mut notebook = Notebook::create(
179 &folder,
180 temporary.path().join("cache"),
181 Notebook::NEW_COLOR,
182 &page,
183 )
184 .unwrap();
185 let section = notebook.catalog().sections[0].path.clone();
186 notebook.create_group("", "Target").unwrap();
187 fs::write(folder.join(TOC), b"unreadable source TOC").unwrap();
188 assert!(notebook.move_entry(&section, "Target").is_err());
189 assert!(folder.join(&section).exists());
190 assert!(!folder.join("Target").join(&section).exists());
191}
192
193struct FaultStorage {
194 inner: Directory,
195 revision: Mutex<Option<(String, Transaction)>>,
196 receipt: Option<Arc<Mutex<Receipt>>>,
197 /// Placing a section file in its folder fails.
198 misplace: bool,
199 /// A checked rename lands, then reports a failure, as a close failing after it does.
200 torn: bool,
201}
202
203#[derive(Default)]
204struct Receipt {
205 reject: Option<String>,
206 reject_confirmation: Option<String>,
207 confirmed: Vec<String>,
208}
209
210impl FaultStorage {
211 fn race(&self, path: &str) {
212 let revision = self.revision.lock().unwrap().take();
213 if let Some((section, transaction)) = revision {
214 assert_eq!(section, path);
215 self.inner.commit(&section, &transaction).unwrap();
216 }
217 }
218}
219
220impl Storage for FaultStorage {
221 fn discover(
222 &self,
223 cache: &mut discover::Cache,
224 limits: discover::Limits,
225 ) -> Result<discover::Folder> {
226 self.inner.discover(cache, limits)
227 }
228 fn location(&self) -> String {
229 self.inner.location()
230 }
231 fn entries(&self, folder: &str) -> io::Result<Vec<discover::Entry>> {
232 self.inner.entries(folder)
233 }
234 fn exists(&self, path: &str) -> bool {
235 self.inner.exists(path)
236 }
237 fn stamp(&self, path: &str) -> io::Result<Stamp> {
238 self.inner.stamp(path)
239 }
240 fn read(&self, path: &str) -> Result<Vec<u8>> {
241 self.inner.read(path)
242 }
243 fn read_file(&self, path: &str, limit: usize) -> Result<Vec<u8>> {
244 self.inner.read_file(path, limit)
245 }
246 fn create(&self, path: &str, bytes: &[u8]) -> Result<()> {
247 self.inner.create(path, bytes)
248 }
249 fn create_directory(&self, path: &str) -> Result<()> {
250 self.inner.create_directory(path)
251 }
252 fn hide(&self, path: &str) -> Result<()> {
253 self.inner.hide(path)
254 }
255 fn rename(&self, from: &str, to: &str) -> Result<()> {
256 self.inner.rename(from, to)
257 }
258 fn rename_root(&self, name: &str, files: &[String]) -> Result<String> {
259 self.inner.rename_root(name, files)
260 }
261 fn replace(&self, from: &str, to: &str) -> Result<()> {
262 self.inner.replace(from, to)
263 }
264 fn delete(&self, path: &str) -> Result<()> {
265 self.inner.delete(path)
266 }
267 fn place(&self, path: &str, ancestor: [u8; 16], name: &str) -> Result<()> {
268 if self.misplace && path.ends_with(".one") {
269 return Err(io::Error::from(io::ErrorKind::PermissionDenied).into());
270 }
271 self.inner.place(path, ancestor, name)
272 }
273 fn commit(&self, path: &str, transaction: &Transaction) -> Result<()> {
274 if let Some(receipt) = &self.receipt {
275 if receipt.lock().unwrap().reject.as_deref() != Some(path) {
276 self.inner.commit(path, transaction)?;
277 }
278 return Err(CommitError {
279 state: onestore::CommitState::Unknown,
280 error: io::ErrorKind::WouldBlock.into(),
281 }
282 .into());
283 }
284 self.inner.commit(path, transaction)
285 }
286 fn confirm(&self, path: &str, base: &Stamp) -> std::result::Result<(), CommitError> {
287 if let Some(receipt) = &self.receipt {
288 let mut receipt = receipt.lock().unwrap();
289 receipt.confirmed.push(path.into());
290 if receipt.reject_confirmation.as_deref() == Some(path) {
291 return Err(CommitError {
292 state: onestore::CommitState::Unknown,
293 error: io::ErrorKind::PermissionDenied.into(),
294 });
295 }
296 }
297 self.inner.confirm(path, base)
298 }
299 fn supersede(&self, path: &str, base: &Stamp, with: &str) -> Result<()> {
300 self.race(path);
301 self.inner.supersede(path, base, with)
302 }
303 fn rename_unchanged(&self, from: &str, to: &str, base: &Stamp) -> Result<()> {
304 self.race(from);
305 self.inner.rename_unchanged(from, to, base)?;
306 match self.torn {
307 true => Err(io::Error::from(io::ErrorKind::BrokenPipe).into()),
308 false => Ok(()),
309 }
310 }
311}
312
313/// A notebook holding "Mine" in its first section and a notebook to move it to, the first on
314/// `storage` made from its folder; the section's catalog path.
315fn moving(
316 temporary: &Path,
317 storage: impl FnOnce(Directory, Option<(String, Transaction)>) -> FaultStorage,
318 theirs: bool,
319) -> (Notebook, Notebook, String) {
320 let (folder, cache) = (temporary.join("notebook"), temporary.join("cache"));
321 let creation = PageCreation::new(None, Some("Mine"), "Fixture").unwrap();
322 let notebook = Notebook::create(&folder, &cache, 0x00f0_a0c0, &creation).unwrap();
323 let path = notebook.catalog().sections[0].path.clone();
324 // Another writer's revision, which the storage commits as the move checks the file.
325 let revision = theirs.then(|| {
326 let arena = onestore::Arena::default();
327 let image = notebook.read_section(&path).unwrap();
328 let mut section = onestore::Section::open(&arena, image).unwrap();
329 let theirs = PageCreation::new(None, Some("Theirs"), "OneNote").unwrap();
330 let edit = Edit {
331 at: crate::now(),
332 ops: vec![Op::Section(SectionOp::Create(theirs))],
333 };
334 section.apply("OneNote", &edit).unwrap();
335 (path.clone(), section.seal().unwrap().unwrap())
336 });
337 drop(notebook);
338 let root = folder.canonicalize().unwrap();
339 let storage = storage(Directory(root.clone()), revision);
340 let source = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
341 let target = Notebook::create(temporary.join("target"), &cache, 0x91baae, &creation).unwrap();
342 (source, target, path)
343}
344
345fn titles(image: &[u8]) -> Vec<String> {
346 (stored_pages(image).unwrap().into_iter())
347 .map(|stored| stored.page.title)
348 .collect()
349}
350
351/// A revision another writer commits after the copy is made keeps the section where it was,
352/// with the revision, beside the copy.
353#[test]
354fn a_section_changed_while_it_moves_stays_where_it_was() {
355 let temporary = tempfile::tempdir().unwrap();
356 let (mut source, mut target, path) = moving(
357 temporary.path(),
358 |inner, revision| FaultStorage {
359 inner,
360 revision: Mutex::new(revision),
361 receipt: None,
362 misplace: false,
363 torn: false,
364 },
365 true,
366 );
367 let section = source.section(&path, false, || {}).unwrap();
368 let moved = (source.move_section(&path, section, &mut target, "", None)).unwrap();
369 assert!(moved.kept.is_some());
370 assert_eq!(
371 titles(&source.read_section(&path).unwrap()),
372 ["Mine", "Theirs"]
373 );
374 assert_eq!(titles(&target.read_section(&moved.path).unwrap()), ["Mine"]);
375}
376
377/// Once the section has left for the bin, a listing that fails to follow it leaves a move, not
378/// a section said to have stayed.
379#[test]
380fn a_move_whose_bin_listing_fails_has_still_moved() {
381 let temporary = tempfile::tempdir().unwrap();
382 let (mut source, mut target, path) = moving(
383 temporary.path(),
384 |inner, _| FaultStorage {
385 inner,
386 revision: Mutex::new(None),
387 receipt: None,
388 misplace: true,
389 torn: false,
390 },
391 false,
392 );
393 let section = source.section(&path, false, || {}).unwrap();
394 let replica = source.replica_path(&path).unwrap();
395 let moved = (source.move_section(&path, section, &mut target, "", None)).unwrap();
396 assert!(moved.kept.is_none(), "{:?}", moved.kept);
397 assert!(!source.storage.exists(&path));
398 assert!(source.storage.exists(&catalog_path(RECYCLE_BIN, &path)));
399 assert_eq!(titles(&target.read_section(&moved.path).unwrap()), ["Mine"]);
400 assert!(!replica.exists(), "the original's replica goes with it");
401}
402
403/// A revision another writer commits while a password is being set is never overwritten:
404/// the section keeps it, and the password waits for a later try.
405#[test]
406fn a_password_never_overwrites_a_concurrent_revision() {
407 let temporary = tempfile::tempdir().unwrap();
408 let folder = temporary.path().join("notebook");
409 let cache = temporary.path().join("cache");
410 let creation = PageCreation::new(None, Some("Mine"), "Fixture").unwrap();
411 let path = Notebook::create(&folder, &cache, 0x00f0_a0c0, &creation)
412 .unwrap()
413 .catalog()
414 .sections[0]
415 .path
416 .clone();
417 let file = folder.canonicalize().unwrap().join(&path);
418 let image = std::fs::read(&file).unwrap();
419 let arena = onestore::Arena::default();
420 let mut section = onestore::Section::open(&arena, image).unwrap();
421 let theirs = PageCreation::new(None, Some("Theirs"), "OneNote").unwrap();
422 section
423 .apply(
424 "OneNote",
425 &Edit {
426 at: crate::now(),
427 ops: vec![Op::Section(SectionOp::Create(theirs))],
428 },
429 )
430 .unwrap();
431 let revision = section.seal().unwrap().unwrap();
432
433 let root = folder.canonicalize().unwrap();
434 let storage = FaultStorage {
435 inner: Directory(root.clone()),
436 revision: Mutex::new(Some((path.clone(), revision))),
437 receipt: None,
438 misplace: false,
439 torn: false,
440 };
441 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
442 let set = notebook.set_password(&path, None, Some("secret"));
443 let titles = |image: &[u8]| -> Vec<String> {
444 stored_pages(image)
445 .unwrap()
446 .into_iter()
447 .map(|page| page.page.title)
448 .collect()
449 };
450 let stored = std::fs::read(&file).unwrap();
451 match set {
452 Ok(key) => {
453 let key = key.unwrap();
454 let pages = stored_pages_unlocked(&stored, &key).unwrap();
455 assert!(pages.iter().any(|page| page.page.title == "Theirs"));
456 }
457 Err(_) => assert_eq!(titles(&stored), ["Mine", "Theirs"]),
458 }
459}
460
461#[test]
462fn recycling_confirms_uncertain_toc_and_page_appends_and_undo() {
463 let temporary = tempfile::tempdir().unwrap();
464 let folder = temporary.path().join("notebook");
465 let cache = temporary.path().join("cache");
466 let creation = PageCreation::new(None, Some("Keep this page"), "Fixture").unwrap();
467 let notebook = Notebook::create(&folder, &cache, Notebook::NEW_COLOR, &creation).unwrap();
468 let path = notebook.catalog().sections[0].path.clone();
469 let source = notebook.read_section(&path).unwrap();
470 let page = stored_pages(&source).unwrap().remove(0).page;
471 drop(notebook);
472 let root = folder.canonicalize().unwrap();
473 let receipt = Arc::new(Mutex::new(Receipt::default()));
474 let storage = FaultStorage {
475 inner: Directory(root.clone()),
476 revision: Mutex::new(None),
477 receipt: Some(receipt.clone()),
478 misplace: false,
479 torn: false,
480 };
481 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
482 notebook
483 .recycle_pages(std::slice::from_ref(&page), "Fixture")
484 .unwrap();
485 let deleted = catalog_path(RECYCLE_BIN, "OneNote_DeletedPages.one");
486 assert_eq!(
487 receipt.lock().unwrap().confirmed,
488 [
489 TOC.to_string(),
490 catalog_path(RECYCLE_BIN, TOC),
491 deleted.clone(),
492 ]
493 );
494 let binned = fs::read(folder.join(&deleted)).unwrap();
495 let pages = stored_pages(&binned).unwrap();
496 assert_eq!(pages.len(), 1);
497 assert_eq!(pages[0].page.title, page.title);
498 assert_eq!(pages[0].page.identity, page.identity);
499 assert_eq!(pages[0].page.created, page.created);
500 assert_eq!(pages[0].page.date_text(), page.date_text());
501 let cold = Notebook::open(&folder, temporary.path().join("cold-cache")).unwrap();
502 assert_eq!(cold.catalog().groups.len(), 1);
503 assert_eq!(cold.folder(RECYCLE_BIN).unwrap().sections.len(), 1);
504 assert_eq!(
505 stored_pages(&cold.read_section(&deleted).unwrap())
506 .unwrap()
507 .len(),
508 1
509 );
510 drop(cold);
511 notebook.unrecycle_pages(&[page.identity.unwrap()]).unwrap();
512 let restored = fs::read(folder.join(&deleted)).unwrap();
513 assert!(
514 restored.len() > binned.len(),
515 "Undo must append its deletion revision"
516 );
517 assert!(stored_pages(&restored).unwrap().is_empty());
518 assert_eq!(receipt.lock().unwrap().confirmed.last(), Some(&deleted));
519 assert_eq!(receipt.lock().unwrap().confirmed.len(), 4);
520 assert_eq!(fs::read(folder.join(&path)).unwrap(), source);
521}
522
523#[test]
524fn recycling_rejects_unconfirmed_appends_and_can_retry_each_stage() {
525 let deleted = catalog_path(RECYCLE_BIN, "OneNote_DeletedPages.one");
526 for (rejected, retained) in [
527 (TOC.to_string(), false),
528 (catalog_path(RECYCLE_BIN, TOC), false),
529 (deleted.clone(), false),
530 (deleted.clone(), true),
531 ] {
532 let temporary = tempfile::tempdir().unwrap();
533 let folder = temporary.path().join("notebook");
534 let cache = temporary.path().join("cache");
535 let creation = PageCreation::new(None, Some("Keep this page"), "Fixture").unwrap();
536 let notebook = Notebook::create(&folder, &cache, Notebook::NEW_COLOR, &creation).unwrap();
537 let path = notebook.catalog().sections[0].path.clone();
538 let mut source = notebook.read_section(&path).unwrap();
539 let mut page = stored_pages(&source).unwrap().remove(0).page;
540 drop(notebook);
541 let root = folder.canonicalize().unwrap();
542 let receipt = Arc::new(Mutex::new(Receipt {
543 reject: (!retained).then(|| rejected.clone()),
544 reject_confirmation: retained.then(|| rejected.clone()),
545 confirmed: Vec::new(),
546 }));
547 let storage = FaultStorage {
548 inner: Directory(root.clone()),
549 revision: Mutex::new(None),
550 receipt: Some(receipt.clone()),
551 misplace: false,
552 torn: false,
553 };
554 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
555 assert!(
556 matches!(
557 notebook.recycle_pages(std::slice::from_ref(&page), "Fixture"),
558 Err(Error::Remote(_))
559 ),
560 "{rejected}"
561 );
562 assert_eq!(receipt.lock().unwrap().confirmed.last(), Some(&rejected));
563 assert_eq!(fs::read(folder.join(&path)).unwrap(), source);
564 if folder.join(&deleted).exists() {
565 assert_eq!(
566 stored_pages(&fs::read(folder.join(&deleted)).unwrap())
567 .unwrap()
568 .len(),
569 usize::from(retained)
570 );
571 }
572 if retained {
573 let arena = onestore::Arena::default();
574 let mut section = onestore::Section::open(&arena, source).unwrap();
575 let space = section.pages().unwrap()[0].0;
576 section
577 .apply(
578 "Fixture",
579 &Edit {
580 at: crate::now(),
581 ops: vec![Op::Page {
582 space,
583 op: onestore::op::PageOp::Color(Some(0xabcdef)),
584 }],
585 },
586 )
587 .unwrap();
588 section
589 .seal()
590 .unwrap()
591 .unwrap()
592 .commit_file(folder.join(&path))
593 .unwrap();
594 source = fs::read(folder.join(&path)).unwrap();
595 page = stored_pages(&source).unwrap().remove(0).page;
596 }
597 {
598 let mut receipt = receipt.lock().unwrap();
599 receipt.reject = None;
600 receipt.reject_confirmation = None;
601 }
602 notebook
603 .recycle_pages(std::slice::from_ref(&page), "Fixture")
604 .unwrap();
605 let binned = stored_pages(&fs::read(folder.join(&deleted)).unwrap()).unwrap();
606 assert_eq!(binned.len(), 1 + usize::from(retained), "{rejected}");
607 for snapshot in &binned {
608 assert_eq!(snapshot.page.title, page.title);
609 assert_eq!(snapshot.page.identity, page.identity);
610 }
611 assert_eq!(binned.last().unwrap().page.color, page.color);
612 if retained {
613 assert_eq!(binned[0].page.color, None);
614 assert_eq!(binned.last().unwrap().page.color, Some(0xabcdef));
615 }
616 assert_eq!(fs::read(folder.join(&path)).unwrap(), source);
617 }
618}
619
620#[test]
621fn initially_offline_workers_keep_the_original_queue_until_explicitly_woken() {
622 use std::sync::atomic::{AtomicUsize, Ordering};
623 use std::time::Instant;
624
625 struct Counted {
626 file: FileRemote,
627 calls: Arc<[AtomicUsize; 5]>,
628 }
629 impl Remote for Counted {
630 fn read(&mut self) -> io::Result<Vec<u8>> {
631 self.calls[1].fetch_add(1, Ordering::SeqCst);
632 self.file.read()
633 }
634 fn stamp(&mut self) -> io::Result<Stamp> {
635 self.calls[2].fetch_add(1, Ordering::SeqCst);
636 self.file.stamp()
637 }
638 fn publish(&mut self, transaction: &Transaction) -> std::result::Result<(), CommitError> {
639 self.calls[3].fetch_add(1, Ordering::SeqCst);
640 self.file.publish(transaction)
641 }
642 fn confirm(&mut self, base: &Stamp) -> std::result::Result<(), CommitError> {
643 self.calls[4].fetch_add(1, Ordering::SeqCst);
644 self.file.confirm(base)
645 }
646 }
647
648 for relocate in [false, true] {
649 let temporary = tempfile::tempdir().unwrap();
650 let file = temporary.path().join("Original.one");
651 let cache = temporary.path().join("original.sqlite");
652 let source = onestore::create_section("Original.one", "Original", "Fixture").unwrap();
653 fs::write(&file, &source).unwrap();
654 let replica = Replica::create(&cache, &source).unwrap();
655 let creation =
656 PageCreation::new(None, Some("Offline initialization marker"), "Fixture").unwrap();
657 let id = replica
658 .apply(
659 "Fixture",
660 Edit {
661 at: crate::now(),
662 ops: vec![Op::Section(SectionOp::Create(creation))],
663 },
664 )
665 .unwrap();
666 let pending = replica.pending().unwrap();
667 drop(replica);
668 let calls = Arc::new(std::array::from_fn(|_| AtomicUsize::new(0)));
669 let counter = Arc::clone(&calls);
670 let remote = file.clone();
671 let (notify, notified) = mpsc::channel();
672 let mut section = Section::start(
673 file.clone(),
674 Replica::open(&cache).unwrap(),
675 move || {
676 counter[0].fetch_add(1, Ordering::SeqCst);
677 Ok(Counted {
678 file: FileRemote(remote.clone()),
679 calls: Arc::clone(&counter),
680 })
681 },
682 CHECK,
683 true,
684 move || {
685 let _ = notify.send(());
686 },
687 )
688 .unwrap();
689 let target = if relocate {
690 let target = temporary.path().join("Moved.one");
691 fs::rename(&file, &target).unwrap();
692 let counter = Arc::clone(&calls);
693 section
694 .relocate_with(&target, move |file, _| {
695 counter[0].fetch_add(1, Ordering::SeqCst);
696 Ok(Counted {
697 file: FileRemote(file.to_owned()),
698 calls: Arc::clone(&counter),
699 })
700 })
701 .unwrap();
702 target
703 } else {
704 file
705 };
706 let deadline = Instant::now() + Duration::from_millis(300);
707 while Instant::now() < deadline {
708 assert_eq!(section.pending().unwrap(), pending);
709 assert_eq!(fs::read(&target).unwrap(), source);
710 assert!(calls.iter().all(|count| count.load(Ordering::SeqCst) == 0));
711 std::thread::sleep(Duration::from_millis(10));
712 }
713 section.wake();
714 let deadline = Instant::now() + Duration::from_secs(10);
715 loop {
716 notified
717 .recv_timeout(deadline.saturating_duration_since(Instant::now()))
718 .unwrap();
719 if section.events().iter().any(|event| matches!(event,
720 Event::Attempt { id: published, status: EditStatus::Published { .. } } if *published == id)) {
721 break;
722 }
723 }
724 assert!(section.pending().unwrap().is_empty());
725 assert_eq!(calls[0].load(Ordering::SeqCst), 1);
726 assert_eq!(calls[3].load(Ordering::SeqCst), 1);
727 section.close().unwrap();
728 let pages = stored_pages(&fs::read(&target).unwrap()).unwrap();
729 assert_eq!(
730 pages
731 .iter()
732 .filter(|page| page.page.title == "Offline initialization marker")
733 .count(),
734 1
735 );
736 assert!(Replica::open(&cache).unwrap().pending().unwrap().is_empty());
737 }
738}
739
740#[test]
741fn associated_apply_reply_precedes_success_wake_without_a_publication_event() {
742 use std::sync::atomic::{AtomicBool, Ordering};
743
744 let temporary = tempfile::tempdir().unwrap();
745 let file = temporary.path().join("Reply.one");
746 let source = onestore::create_section("Reply.one", "Original", "Fixture").unwrap();
747 fs::write(&file, &source).unwrap();
748 let (woken, wake) = mpsc::channel();
749 let completed = Arc::new(AtomicBool::new(false));
750 let observed = Arc::clone(&completed);
751 let section = Section::open(&file, temporary.path().join("cache"), true, move || {
752 let _ = woken.send(observed.load(Ordering::Acquire));
753 })
754 .unwrap();
755 while wake.try_recv().is_ok() {}
756 let creation = PageCreation::new(None, Some("Associated original"), "Fixture").unwrap();
757 let space = creation.space();
758 let edit = Edit {
759 at: crate::now(),
760 ops: vec![Op::Section(SectionOp::Create(creation))],
761 };
762 let original = edit.clone();
763 let (replied, reply) = mpsc::channel();
764 section
765 .apply_with("Original author", edit, move |result| {
766 completed.store(true, Ordering::Release);
767 replied.send(result).unwrap();
768 })
769 .unwrap();
770 let id = reply
771 .recv_timeout(Duration::from_secs(10))
772 .unwrap()
773 .unwrap();
774 assert!(wake.recv_timeout(Duration::from_secs(10)).unwrap());
775 let pending = section.pending().unwrap();
776 assert_eq!(pending.len(), 1);
777 assert_eq!(pending[0].id, id);
778 assert_eq!(pending[0].author, "Original author");
779 assert_eq!(pending[0].edit, original);
780 assert_eq!(section.page(space).unwrap().title, "Associated original");
781 assert!(section.events().is_empty());
782 section.close().unwrap();
783 assert_eq!(fs::read(file).unwrap(), source);
784}
785
786#[test]
787fn edits_the_cache_refuses_wait_in_order_and_land_once() {
788 use std::time::Instant;
789
790 let temporary = tempfile::tempdir().unwrap();
791 let file = temporary.path().join("Waiting.one");
792 let cache = temporary.path().join("waiting.sqlite");
793 let source = onestore::create_section("Waiting.one", "Original", "Fixture").unwrap();
794 fs::write(&file, &source).unwrap();
795 let replica = Replica::create(&cache, &source).unwrap();
796 let section = Section::resume(&file, replica, true, || {}).unwrap();
797 let replica = Arc::clone(section.replica());
798 // Queued before the fault, so the worker asks to seal while edits wait.
799 let seed = replica
800 .apply(
801 "Fixture",
802 Edit {
803 at: crate::now(),
804 ops: vec![Op::Section(SectionOp::Create(
805 PageCreation::new(None, Some("Seed"), "Fixture").unwrap(),
806 ))],
807 },
808 )
809 .unwrap();
810 // The cache is full, until it may grow again; the first edit needs room.
811 let full = |pages: Option<i64>| {
812 let connection = replica.lock().unwrap();
813 let pages = pages.unwrap_or_else(|| {
814 connection
815 .pragma_query_value(None, "page_count", |row| row.get(0))
816 .unwrap()
817 });
818 connection
819 .pragma_update(None, "max_page_count", pages)
820 .unwrap();
821 };
822 full(None);
823 let title = "Waiting page ".repeat(1000);
824 let creation = PageCreation::new(None, Some(&title), "Fixture").unwrap();
825 let space = creation.space();
826 // The second edit colours the page the first creates.
827 let edits = [
828 Op::Section(SectionOp::Create(creation)),
829 Op::Page {
830 space,
831 op: onestore::op::PageOp::Color(Some(0x00ab_cdef)),
832 },
833 ]
834 .map(|op| Edit {
835 at: crate::now(),
836 ops: vec![op],
837 });
838 let (replied, replies) = mpsc::channel();
839 for edit in edits.clone() {
840 let replied = replied.clone();
841 section
842 .apply_with("Fixture", edit, move |result| {
843 replied
844 .send(result.map_err(|error| error.to_string()))
845 .unwrap();
846 })
847 .unwrap();
848 }
849 let deadline = Instant::now() + Duration::from_secs(10);
850 while !section
851 .events()
852 .iter()
853 .any(|event| matches!(event, Event::Failed(_)))
854 {
855 assert!(Instant::now() < deadline);
856 std::thread::sleep(Duration::from_millis(10));
857 }
858 let waiting = |section: &Section| {
859 assert!(replies.try_recv().is_err());
860 let pending = section.pending().unwrap();
861 assert_eq!(
862 pending.iter().map(|edit| edit.id).collect::<Vec<_>>(),
863 [seed]
864 );
865 let page = section.page(space).unwrap();
866 assert_eq!((&page.title, page.color), (&title, Some(0x00ab_cdef)));
867 assert!(matches!(
868 replica.ask(|reply| crate::working::Request::Seal { reply }),
869 Err(Error::Io(error)) if error.kind() == io::ErrorKind::WouldBlock
870 ));
871 assert!(matches!(
872 section.written(),
873 Err(Error::Io(error)) if error.kind() == io::ErrorKind::WouldBlock
874 ));
875 };
876 waiting(&section);
877 // A caller blocking on its edit still learns of the failure, and the waiting edits stay.
878 let blocked = Edit {
879 at: crate::now(),
880 ops: vec![Op::Page {
881 space,
882 op: onestore::op::PageOp::Color(Some(0x0012_3456)),
883 }],
884 };
885 assert!(replica.apply("Fixture", blocked).is_err());
886 waiting(&section);
887 section.wake();
888 std::thread::sleep(Duration::from_millis(200));
889 waiting(&section);
890
891 full(Some(1 << 30));
892 let ids = [(); 2].map(|()| {
893 replies
894 .recv_timeout(Duration::from_secs(10))
895 .unwrap()
896 .unwrap()
897 });
898 let pending = section.pending().unwrap();
899 assert_eq!(
900 pending[1..]
901 .iter()
902 .map(|edit| (edit.id, &edit.edit))
903 .collect::<Vec<_>>(),
904 [(ids[0], &edits[0]), (ids[1], &edits[1])]
905 );
906
907 // An edit that could never be written is refused alone; the next one lands.
908 replica
909 .lock()
910 .unwrap()
911 .execute_batch(
912 "CREATE TEMP TRIGGER refuse BEFORE INSERT ON main.edits WHEN NEW.author = 'Refused'
913 BEGIN SELECT RAISE(ABORT, 'Disposable refusal'); END;",
914 )
915 .unwrap();
916 for (author, colour) in [("Refused", 0x0012_3456), ("Fixture", 0x0065_4321)] {
917 let replied = replied.clone();
918 section
919 .apply_with(
920 author,
921 Edit {
922 at: crate::now(),
923 ops: vec![Op::Page {
924 space,
925 op: onestore::op::PageOp::Color(Some(colour)),
926 }],
927 },
928 move |result| {
929 replied
930 .send(result.map_err(|error| error.to_string()))
931 .unwrap();
932 },
933 )
934 .unwrap();
935 }
936 assert!(
937 replies
938 .recv_timeout(Duration::from_secs(10))
939 .unwrap()
940 .is_err()
941 );
942 let last = replies
943 .recv_timeout(Duration::from_secs(10))
944 .unwrap()
945 .unwrap();
946 section.written().unwrap();
947 assert_eq!(section.pending().unwrap().last().unwrap().id, last);
948 assert_eq!(section.page(space).unwrap().color, Some(0x0065_4321));
949 section.wake();
950 let deadline = Instant::now() + Duration::from_secs(10);
951 while !section.events().iter().any(|event| {
952 matches!(event, Event::Attempt { id, status: EditStatus::Published { .. } } if *id == last)
953 }) {
954 assert!(Instant::now() < deadline);
955 std::thread::sleep(Duration::from_millis(10));
956 }
957 section.close().unwrap();
958 drop(replica);
959 let pages = stored_pages(&fs::read(&file).unwrap()).unwrap();
960 let waited: Vec<_> = pages
961 .iter()
962 .filter(|page| page.page.title == title)
963 .collect();
964 assert_eq!(waited.len(), 1);
965 assert_eq!(waited[0].page.color, Some(0x0065_4321));
966 assert!(Replica::open(&cache).unwrap().pending().unwrap().is_empty());
967}
968
969#[test]
970fn a_cache_owned_while_closed_opens_for_nobody_else() {
971 let temporary = tempfile::tempdir().unwrap();
972 let cache = temporary.path().join("owned.sqlite");
973 let source = onestore::create_section("Owned.one", "Original", "Fixture").unwrap();
974 drop(Replica::create(&cache, &source).unwrap());
975 let named = PathBuf::from(crate::connect(&cache).unwrap().path().unwrap());
976 let owned = crate::Owned::take(&named).unwrap();
977 assert!(Replica::open(&cache).is_err_and(|error| error.busy()));
978 assert!(crate::closed(&cache).is_err_and(|error| error.busy()));
979 drop(owned);
980 assert!(!crate::owner_lock(&named).exists());
981 drop(Replica::open(&cache).unwrap());
982 // Another process holds only the lock beside the cache.
983 let lock = std::fs::File::create(crate::owner_lock(&named)).unwrap();
984 lock.try_lock().unwrap();
985 assert!(Replica::open(&cache).is_err_and(|error| error.busy()));
986 drop(lock);
987 drop(Replica::open(&cache).unwrap());
988}
989
990/// A section whose rename into the bin landed, though what followed it failed, has moved.
991#[test]
992fn a_move_whose_rename_lands_and_then_fails_has_still_moved() {
993 let temporary = tempfile::tempdir().unwrap();
994 let (mut source, mut target, path) = moving(
995 temporary.path(),
996 |inner, _| FaultStorage {
997 inner,
998 revision: Mutex::new(None),
999 receipt: None,
1000 misplace: false,
1001 torn: true,
1002 },
1003 false,
1004 );
1005 let section = source.section(&path, false, || {}).unwrap();
1006 let moved = (source.move_section(&path, section, &mut target, "", None)).unwrap();
1007 assert!(moved.kept.is_none(), "{:?}", moved.kept);
1008 assert!(source.storage.exists(&catalog_path(RECYCLE_BIN, &path)));
1009}