diff options
Diffstat (limited to 'src/replica.zig')
| -rw-r--r-- | src/replica.zig | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/replica.zig b/src/replica.zig index e5d5ad16..4140beca 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -218,6 +218,16 @@ pub const Authority = struct { return a.unit(0, a.seq); } + /// An input of its own frontend's, in the view that frontend shows + /// (the first: tui.zig `Shell.handle` goes into `s.own`), as its unit. + pub fn own(a: *Authority, ev: pardes.Event) ![]const u8 { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + a.core.toView(0); + was.end(); + return a.local(ev); + } + /// A step of its own (a pty's output) into the open batch: under a /// flood a replica gets a unit a frame, not one a read (each unit of the /// authority's rolls back a replica's pending inputs), and its digest @@ -256,6 +266,51 @@ pub const Authority = struct { return .{ .view = k, .unit = try a.unit(0, a.seq) }; } + /// A client joining as `Peers.join` takes one: its view kept for it + /// (`back`, after a divergence) or a new one, null when every view is + /// taken (refused); the unit that says so to the others (valid until the + /// next call), and the dump it loads, its blobs in place (`gpa`'s), with + /// the digest it checks its load by. + pub fn join(a: *Authority, client: u16, back: ?u8) !?struct { view: u8, unit: []const u8, dump: []u8, digest: u64 } { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const again = if (back) |v| a.core.rejoinView(v) else false; + const k = (if (again) back else a.core.openView()) orelse return null; + a.core.undrawn |= @as(u8, 1) << @intCast(k); + a.view_of[client] = k; + a.seq += 1; + const u = try a.unit(0, a.seq); + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer blobs.deinit(a.core.gpa); + const state = try pardes.dump.dumpJoin(a.core, &blobs); + defer a.core.gpa.free(state); + tape.resync(&a.t); + var whole: std.ArrayListUnmanaged(u8) = .empty; + errdefer whole.deinit(a.gpa); + var pos: usize = 0; + for (blobs.items) |b| { + try whole.appendSlice(a.gpa, state[pos..b.at]); + try whole.appendSlice(a.gpa, b.bytes); + pos = b.at; + } + try whole.appendSlice(a.gpa, state[pos..]); + return .{ .view = k, .unit = u, .dump = try whole.toOwnedSlice(a.gpa), .digest = tape.digestOf(a.core) }; + } + + /// Client `client` gone as `Peers.leave` lets one go: its view kept for + /// it (`again`: it joins again for it) or closed; the unit. + pub fn leave(a: *Authority, client: u16, again: bool) ![]const u8 { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + const k = a.view_of[client]; + a.core.undrawn &= ~(@as(u8, 1) << @intCast(k)); + if (again) a.core.awayView(k) else a.core.closeView(k); + was.end(); + a.seq += 1; + return a.unit(0, a.seq); + } + /// Client `client`'s view gone (its link ended), as its unit. pub fn closeView(a: *Authority, client: u16) ![]const u8 { std.debug.assert(a.batched == 0); @@ -1013,6 +1068,9 @@ pub const Replica = struct { // was there before is of a grid the deltas since moved on // from (a rollback never brings a terminal's past back). r.core().shell_rows = .{}; + // A tab's width is the process's (config.tab_now), which a + // step that set it (Tab) wrote and the copy does not hold. + pardes.config.tab_now = r.core().settings.tab; } r.applied = 0; // Its views as the copy was taken, or as the last delta set them @@ -3558,6 +3616,30 @@ test "another client's input first: a replica rolls back, replays it and applies try std.testing.expectEqual(@as(u32, 4), r2.stats.fast); } +test "a rollback over a Tab it applied puts the process's tab width back as its core has it" { + // config.tab_now is the process's, written by the step that set the + // width (the soak, five clients: a Tab undone left it, and the next + // replayed step measured the files' tabs otherwise). + const gpa = std.testing.allocator; + defer pardes.config.tab_now = pardes.config.tab_width; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r = try Replica.init(gpa, 1, first, false); + defer r.deinit(); + pardes.config.tab_now = r.core().settings.tab; + const was = r.core().settings.tab; + const msg = try gpa.dupe(u8, try r.input(.{ .command = "Tab 2" })); + defer gpa.free(msg); + try std.testing.expectEqual(@as(usize, 1), r.applied); + try std.testing.expectEqual(@as(u16, 2), pardes.config.tab_now); + // The session's own key first: back to the confirmed position. + r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + try std.testing.expectEqual(was, r.core().settings.tab); + try std.testing.expectEqual(was, pardes.config.tab_now); +} + test "a delta applied under a speculation outlives its rollback: the terminal stays the authority's newest" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; |
