project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use crate::{ |
| 2 | loro::{AFTER_APPLY_DIFF_HANDLER, FAIL_APPLY_DIFF_AFTER_HANDLER}, |
| 3 | undo::UndoManager, |
| 4 | LoroDoc, |
| 5 | }; |
| 6 | use std::sync::{ |
| 7 | atomic::{AtomicUsize, Ordering}, |
| 8 | Arc, |
| 9 | }; |
| 10 | |
| 11 | #[test] |
| 12 | fn failed_undo_and_redo_restore_text_history_and_events() { |
| 13 | for mixed in [false, true] { |
| 14 | let doc = LoroDoc::new_auto_commit(); |
| 15 | doc.set_peer_id(1).unwrap(); |
| 16 | let text = doc.get_text("text"); |
| 17 | text.insert(0, "a😀bc", crate::cursor::PosType::Unicode) |
| 18 | .unwrap(); |
| 19 | doc.commit_then_renew(); |
| 20 | let undo = UndoManager::new(&doc); |
| 21 | undo.set_merge_interval(0); |
| 22 | text.delete(1, 1, crate::cursor::PosType::Unicode).unwrap(); |
| 23 | text.mark(0, 2, "bold", true.into(), crate::cursor::PosType::Unicode) |
| 24 | .unwrap(); |
| 25 | if mixed { |
| 26 | doc.get_map("map").insert("changed", 42).unwrap(); |
| 27 | doc.get_list("list").push(42).unwrap(); |
| 28 | doc.get_movable_list("movable").push(42.into()).unwrap(); |
| 29 | doc.get_tree("tree") |
| 30 | .create(crate::state::TreeParentId::Root) |
| 31 | .unwrap(); |
| 32 | #[cfg(feature = "counter")] |
| 33 | doc.get_counter("counter").increment(0.1).unwrap(); |
| 34 | } |
| 35 | doc.commit_then_renew(); |
| 36 | let events = Arc::new(AtomicUsize::new(0)); |
| 37 | let captured = events.clone(); |
| 38 | let _sub = doc.subscribe_root(Arc::new(move |_| { |
| 39 | captured.fetch_add(1, Ordering::Relaxed); |
| 40 | })); |
| 41 | |
| 42 | for redo in [false, true] { |
| 43 | let value = doc.get_deep_value(); |
| 44 | let delta = text.get_delta(); |
| 45 | let frontiers = doc.state_frontiers(); |
| 46 | let vv = doc.oplog_vv(); |
| 47 | let count = doc.oplog().lock().cached_visible_op_count(); |
| 48 | let stacks = (undo.undo_count(), undo.redo_count()); |
| 49 | for _ in 0..2 { |
| 50 | let seen = events.load(Ordering::Relaxed); |
| 51 | FAIL_APPLY_DIFF_AFTER_HANDLER.with(|flag| flag.set(true)); |
| 52 | let result = if redo { undo.redo() } else { undo.undo() }; |
| 53 | assert!(matches!( |
| 54 | result, |
| 55 | Err(crate::LoroError::OutOfBound { |
| 56 | pos: usize::MAX, |
| 57 | .. |
| 58 | }) |
| 59 | )); |
| 60 | assert_eq!(doc.get_deep_value(), value); |
| 61 | assert_eq!(text.get_delta(), delta); |
| 62 | assert_eq!(doc.state_frontiers(), frontiers); |
| 63 | assert_eq!(doc.oplog_vv(), vv); |
| 64 | assert_eq!(doc.oplog().lock().cached_visible_op_count(), count); |
| 65 | assert_eq!(doc.get_pending_txn_len(), 0); |
| 66 | assert_eq!((undo.undo_count(), undo.redo_count()), stacks); |
| 67 | assert_eq!(events.load(Ordering::Relaxed), seen); |
| 68 | } |
| 69 | assert!(if redo { undo.redo() } else { undo.undo() }.unwrap()); |
| 70 | } |
| 71 | assert_eq!(text.get_delta(), { |
| 72 | let restored = LoroDoc::new_auto_commit(); |
| 73 | restored |
| 74 | .import(&doc.export(crate::encoding::ExportMode::Snapshot).unwrap()) |
| 75 | .unwrap(); |
| 76 | restored.get_text("text").get_delta() |
| 77 | }); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn failed_undo_does_not_discard_a_concurrent_writers_edit() { |
| 83 | use std::{sync::mpsc, thread, time::Duration}; |
| 84 | let doc = LoroDoc::new_auto_commit(); |
| 85 | doc.set_peer_id(1).unwrap(); |
| 86 | let text = doc.get_text("text"); |
| 87 | text.insert(0, "abc", crate::cursor::PosType::Unicode) |
| 88 | .unwrap(); |
| 89 | doc.commit_then_renew(); |
| 90 | let undo = UndoManager::new(&doc); |
| 91 | text.delete(0, 1, crate::cursor::PosType::Unicode).unwrap(); |
| 92 | doc.commit_then_renew(); |
| 93 | let other = doc.clone(); |
| 94 | let (go, begin) = mpsc::channel(); |
| 95 | let (ready, attempted) = mpsc::channel(); |
| 96 | let (done, finished) = mpsc::channel(); |
| 97 | let writer = thread::spawn(move || { |
| 98 | begin.recv().unwrap(); |
| 99 | ready.send(()).unwrap(); |
| 100 | other |
| 101 | .get_text("text") |
| 102 | .insert(2, "X", crate::cursor::PosType::Unicode) |
| 103 | .unwrap(); |
| 104 | other.commit_then_renew(); |
| 105 | let _ = done.send(()); |
| 106 | }); |
| 107 | AFTER_APPLY_DIFF_HANDLER.with(|hook| { |
| 108 | *hook.borrow_mut() = Some(Box::new(move || { |
| 109 | go.send(()).unwrap(); |
| 110 | attempted.recv().unwrap(); |
| 111 | assert!(matches!( |
| 112 | finished.recv_timeout(Duration::from_millis(50)), |
| 113 | Err(mpsc::RecvTimeoutError::Timeout) |
| 114 | )); |
| 115 | })); |
| 116 | }); |
| 117 | FAIL_APPLY_DIFF_AFTER_HANDLER.with(|flag| flag.set(true)); |
| 118 | assert!(undo.undo().is_err()); |
| 119 | writer.join().unwrap(); |
| 120 | assert_eq!(text.to_string(), "bcX"); |
| 121 | assert_eq!(undo.undo_count(), 2); |
| 122 | assert_eq!(undo.redo_count(), 0); |
| 123 | } |