summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-09 00:52:52 -0300
committerGabriel Schneider <[email protected]>2026-11-09 01:28:28 -0300
commit5157579ff7f9ae0cfc3c7629e953da05bce340fc (patch)
tree02147789efdcea998981efcfe05d4bcb3e9bb7b0 /src/replica.zig
parent4c4b9bf54d77dbc0f9f4e2619746908e501c6001 (diff)
downloadpardes-5157579ff7f9ae0cfc3c7629e953da05bce340fc.tar.gz
pardes-5157579ff7f9ae0cfc3c7629e953da05bce340fc.zip
tests: at the end of their files: every top-level test block of the 46 files that had one mid-file (the locality analysis's 45 and test/hxdiff.zig; gui.zig, macos.zig, look, host_io, 9p_io, terminal, tui, mouse, fs, replica, layout among them, 11,429 test lines), with the functions only tests call, moved after the file's code in their order, a mechanical move (.scratch/suites/tests_to_end.py), zig fmt'd; the unit suites pass as before (tui 1486/1487, gui 1536/1537, core 956/956 on both), macos.zig only parse-checked here
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig472
1 files changed, 236 insertions, 236 deletions
diff --git a/src/replica.zig b/src/replica.zig
index 5b2facb4..1499d166 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -2266,9 +2266,9 @@ pub const Peers = struct {
fn writeStats(ps: *Peers, path: []const u8) void {
var buf: [400]u8 = undefined;
const line = std.fmt.bufPrint(&buf, "session inputs {d} input_us {d}/{d}/{d} boundaries {d} boundary_us {d}/{d}/{d} join_us {d} dump_kb {d} state_kb {d} cut {d}\n", .{
- ps.input_ns.items.len, pctOf(ps.input_ns.items, 50) / 1000, pctOf(ps.input_ns.items, 99) / 1000, pctOf(ps.input_ns.items, 100) / 1000,
- ps.boundary_ns.items.len, pctOf(ps.boundary_ns.items, 50) / 1000, pctOf(ps.boundary_ns.items, 99) / 1000, pctOf(ps.boundary_ns.items, 100) / 1000,
- ps.join_ns / 1000, ps.dump_bytes >> 10, ps.state_bytes >> 10, ps.cut,
+ ps.input_ns.items.len, pctOf(ps.input_ns.items, 50) / 1000, pctOf(ps.input_ns.items, 99) / 1000, pctOf(ps.input_ns.items, 100) / 1000,
+ ps.boundary_ns.items.len, pctOf(ps.boundary_ns.items, 50) / 1000, pctOf(ps.boundary_ns.items, 99) / 1000, pctOf(ps.boundary_ns.items, 100) / 1000,
+ ps.join_ns / 1000, ps.dump_bytes >> 10, ps.state_bytes >> 10, ps.cut,
}) catch return;
var z: [4096]u8 = undefined;
const pz = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return;
@@ -2978,11 +2978,11 @@ pub const Link = struct {
const e = &c.r.echo.stats;
var w: std.Io.Writer = .fixed(&buf);
w.print("inputs {d} at_once {d} stopped {d} units {d} fast {d} neutral {d} mispredicted {d} foreign {d} visible {d} rtt_us {d} spec_us {d}/{d} copy_us {d}/{d} rollback_us {d}/{d} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d} echo {d}/{d} confirmed {d} missed {d}", .{
- st.inputs, st.speculated, st.stopped, st.units, st.fast,
- c.r.neutral, st.mispredicted, st.foreign, c.visible, c.r.rtt_ns / 1000,
- pctOf(st.spec_ns.items, 50) / 1000, pctOf(st.spec_ns.items, 99) / 1000, pctOf(st.copy_ns.items, 50) / 1000, pctOf(st.copy_ns.items, 99) / 1000, pctOf(st.rollback_ns.items, 50) / 1000,
- pctOf(st.rollback_ns.items, 99) / 1000, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk,
- c.join.sent_bytes >> 10, e.predicted, e.predicted + e.unpredicted, e.confirmed, e.mispredicted,
+ st.inputs, st.speculated, st.stopped, st.units, st.fast,
+ c.r.neutral, st.mispredicted, st.foreign, c.visible, c.r.rtt_ns / 1000,
+ pctOf(st.spec_ns.items, 50) / 1000, pctOf(st.spec_ns.items, 99) / 1000, pctOf(st.copy_ns.items, 50) / 1000, pctOf(st.copy_ns.items, 99) / 1000, pctOf(st.rollback_ns.items, 50) / 1000,
+ pctOf(st.rollback_ns.items, 99) / 1000, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk,
+ c.join.sent_bytes >> 10, e.predicted, e.predicted + e.unpredicted, e.confirmed, e.mispredicted,
}) catch return;
// What stopped its inputs, last: a word a pair before it.
w.writeAll(" stops ") catch return;
@@ -3009,30 +3009,6 @@ pub const Link = struct {
}
};
-test "a message past the wire's bound is named, never read as the link's end" {
- var fds: [2]c_int = undefined;
- try std.testing.expectEqual(0, libc.pipe(&fds));
- const head = wire.header(@backingInt(wire.ServerTag.unit), wire.max_payload + 1);
- _ = libc.write(fds[1], &head, head.len);
- _ = libc.close(fds[1]);
- var fr: Framer = .{};
- defer fr.deinit(std.testing.allocator);
- fr.run(std.testing.allocator, fds[0], {}, struct {
- fn take(_: void, _: u8, _: []const u8) bool {
- return true;
- }
- }.take);
- _ = libc.close(fds[0]);
- try std.testing.expectEqual(wire.max_payload + 1, fr.overlong().?);
-}
-
-test "a client joining again after a divergence is told at once that its session is gone" {
- var why: [600]u8 = undefined;
- const t0 = nowNs();
- try std.testing.expect(redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false, 1) == null);
- try std.testing.expect(nowNs() - t0 < 100 * std.time.ns_per_ms);
-}
-
fn appendTo(path: [:0]const u8, line: []const u8) void {
const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644));
if (fd < 0) return;
@@ -3056,6 +3032,43 @@ const TestHost = struct {
}
};
+/// A host with ptys whose pump polls for a frame, as a client's does.
+const PtyHost = struct {
+ fn resized(_: ?*anyopaque, _: u8, _: u16, _: u16) void {}
+ fn poll(_: ?*anyopaque) void {}
+ const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .pty_resize = resized, .poll_frame = poll };
+ fn host() pardes.Host {
+ return .{ .vtable = &vtable };
+ }
+};
+
+/// A kind as a library brings one, for the Shadow's test: a counter its
+/// keys move, painted as `count N`.
+const Counter = struct {
+ const pa = pardes.Kind.c;
+ fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque {
+ return pardes.Kind.api.alloc.?(4);
+ }
+ fn paint(st: ?*anyopaque, win: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {
+ const n: *u32 = @ptrCast(@alignCast(st.?));
+ var line: [16]u8 = undefined;
+ const text = std.fmt.bufPrint(&line, "count {d}", .{n.*}) catch unreachable;
+ for (text, 0..) |ch, i| win.*.cells[i] = std.mem.zeroInit(pa.pa_cell, .{ .text = .{ ch, 0, 0, 0, 0, 0, 0, 0 }, .len = 1, .flags = pa.PA_PAINTED, .fg = pa.PA_DEFAULT, .bg = pa.PA_DEFAULT });
+ }
+ fn press(st: ?*anyopaque, _: [*c]const pa.pa_key) callconv(.c) c_int {
+ const n: *u32 = @ptrCast(@alignCast(st.?));
+ n.* += 1;
+ return 1;
+ }
+ const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Counter", .opens = 1, .doc = "a counter" })};
+ const ops = std.mem.zeroInit(pardes.Kind.Ops, .{
+ .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "counter".* ++ @as([9]u8, @splat(0)), .version = 1, .exts = "", .words = &words, .nwords = 1 }),
+ .open = open,
+ .paint = paint,
+ .key = press,
+ });
+};
+
/// An edit's file time is the clock's second, which a replica guesses: a
/// second's turn between a key and the authority's take of it is a fair
/// misprediction (11 runs in 40 of a loaded Debug window build's test
@@ -3075,6 +3088,196 @@ fn key(r: *Replica, cp: u21, sent: *std.ArrayListUnmanaged([]u8)) !void {
try sent.append(std.testing.allocator, try std.testing.allocator.dupe(u8, msg));
}
+/// What the session sends a peer: its terminals' changes (`.flush`), or,
+/// around a step's unit (Peers.input), the rows the step read that the
+/// peer lacks (`.fill`), after which the caller gives terminals the sizes
+/// the step gave them (`terminal.resizeDeferred`), then the unit.
+fn deliver(a: *Authority, screens: *Screens, peer: u16, r: *Replica, comptime what: enum { flush, fill }) !void {
+ const gpa = std.testing.allocator;
+ var msgs: std.ArrayListUnmanaged([]u8) = .empty;
+ defer {
+ for (msgs.items) |m| gpa.free(m);
+ msgs.deinit(gpa);
+ }
+ switch (what) {
+ .flush => try screens.flush(a.core, peer, r.view, &msgs),
+ .fill => {
+ try screens.fill(a.core, peer, &msgs);
+ screens.filled(a.core);
+ },
+ }
+ for (msgs.items) |m| if (r.screen(m)) |reply| {
+ _ = try screens.take(a.core, peer, reply);
+ };
+}
+
+/// The blob cache of a test, in its own directory (never the user's).
+fn testCache(tmp: *std.testing.TmpDir, buf: *[4200]u8, cap: u64) !Cache {
+ var dir_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
+ return Cache.of(try std.fmt.bufPrint(buf, "{s}/" ++ Cache.name, .{dir}), cap);
+}
+
+/// A far join over a socket pair, the session's writer and reader threads
+/// against the client's `joinOn`, as the daemon hands a connection over:
+/// its hello read off first.
+fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined {
+ var fds: [2]c_int = undefined;
+ if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket;
+ var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty;
+ const state = pardes.dump.dumpJoin(core, &blobs) catch |err| {
+ _ = libc.close(fds[0]);
+ _ = libc.close(fds[1]);
+ return err;
+ };
+ var whole = state.len;
+ for (blobs.items) |b| whole += b.bytes.len;
+ const peer = try ps.gpa.create(Peer);
+ peer.* = .{ .peers = ps, .fd = fds[0], .id = 1, .view = 0, .dump = state, .blobs = blobs, .dump_gpa = core.gpa };
+ peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = 1, .session = "t", .dump_len = whole, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len;
+ const Serve = struct {
+ fn run(p: *Peer) void {
+ var hello: [wire.header_len + 14]u8 = undefined;
+ var got: usize = 0;
+ while (got < hello.len) {
+ const n = libc.read(p.fd, hello[got..].ptr, hello.len - got);
+ if (n <= 0) return p.peers.post(.{ .gone = p });
+ got += @intCast(n);
+ }
+ p.readLoop();
+ }
+ };
+ peer.writer = try std.Thread.spawn(.{}, Peer.writeLoop, .{peer});
+ peer.reader = try std.Thread.spawn(.{}, Serve.run, .{peer});
+ defer ps.letGo(peer);
+ var why: [300]u8 = @splat(0);
+ return joinOn(std.testing.allocator, fds[1], "t", &why, have, null) orelse {
+ std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)});
+ return error.JoinFailed;
+ };
+}
+
+fn endJoin(j: *Joined, have: ?*Have) void {
+ _ = libc.close(j.fd);
+ j.rest.deinit(std.testing.allocator);
+ if (have) |h| {
+ h.store(std.testing.allocator, j);
+ h.wait();
+ } else {
+ std.testing.allocator.free(j.dump);
+ std.heap.c_allocator.free(j.got);
+ }
+}
+
+/// A replica joined from the session's dump as it is now, between units,
+/// as `Peers.join` sends it: its state, its blobs put back where they go.
+fn joinedNow(a: *Authority, id: u16, view: u8) !*Replica {
+ const gpa = std.testing.allocator;
+ const was = Stepping.begin(&a.t, null);
+ 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);
+ was.end();
+ var whole: std.ArrayListUnmanaged(u8) = .empty;
+ defer whole.deinit(gpa);
+ var pos: usize = 0;
+ for (blobs.items) |b| {
+ try whole.appendSlice(gpa, state[pos..b.at]);
+ try whole.appendSlice(gpa, b.bytes);
+ pos = b.at;
+ }
+ try whole.appendSlice(gpa, state[pos..]);
+ return Replica.initDump(gpa, id, view, whole.items, .{ .cols = 80, .rows = 24 });
+}
+
+fn agrees(a: *Authority, r: *Replica) !void {
+ if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why});
+ try std.testing.expect(r.diverged == null);
+ try std.testing.expect(r.quiet());
+ try std.testing.expectEqual(digestOf(a), r.digest());
+}
+
+fn inputOf(buf: []u8, client: u16, seq: u32, ev: pardes.Event) ![]const u8 {
+ const n = (try wire.encodeClient(buf[head_len..], .{ .event = ev })).len;
+ header(buf, client, seq);
+ return buf[0 .. head_len + n];
+}
+
+/// An input of a replica's in the session's order, as `Peers.input` takes
+/// it: the terminals flushed to every peer, the step, the rows it read that
+/// each peer holds as holes (`fill`), then its unit to every replica.
+fn takeAll(a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, msg: []const u8) !void {
+ const gpa = std.testing.allocator;
+ for (rs, ps) |r, peer| try deliver(a, screens, peer, r, .flush);
+ const u = try gpa.dupe(u8, try a.take(msg));
+ defer gpa.free(u);
+ var rows: std.ArrayListUnmanaged([]u8) = .empty;
+ defer rows.deinit(gpa);
+ for (rs, ps) |r, peer| {
+ rows.clearRetainingCapacity();
+ try screens.fill(a.core, peer, &rows);
+ for (rows.items) |m| {
+ _ = r.screen(m);
+ gpa.free(m);
+ }
+ }
+ screens.filled(a.core);
+ for (rs) |r| {
+ r.confirm(u);
+ r.settle();
+ }
+}
+
+fn sendAs(r: *Replica, a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, ev: pardes.Event) !void {
+ const msg = try std.testing.allocator.dupe(u8, try r.input(ev));
+ defer std.testing.allocator.free(msg);
+ try takeAll(a, screens, rs, ps, msg);
+}
+
+fn shows(p: *Pardes, text: []const u8) !bool {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const s = try p.render(arena.allocator());
+ var row: [512]u8 = undefined;
+ for (0..s.rows) |y| {
+ var n: usize = 0;
+ for (0..s.cols) |x| {
+ const g = s.at(@intCast(x), @intCast(y)).grapheme();
+ if (n + g.len > row.len) break;
+ @memcpy(row[n..][0..g.len], g);
+ n += g.len;
+ }
+ if (std.mem.indexOf(u8, row[0..n], text) != null) return true;
+ }
+ return false;
+}
+
+test "a message past the wire's bound is named, never read as the link's end" {
+ var fds: [2]c_int = undefined;
+ try std.testing.expectEqual(0, libc.pipe(&fds));
+ const head = wire.header(@backingInt(wire.ServerTag.unit), wire.max_payload + 1);
+ _ = libc.write(fds[1], &head, head.len);
+ _ = libc.close(fds[1]);
+ var fr: Framer = .{};
+ defer fr.deinit(std.testing.allocator);
+ fr.run(std.testing.allocator, fds[0], {}, struct {
+ fn take(_: void, _: u8, _: []const u8) bool {
+ return true;
+ }
+ }.take);
+ _ = libc.close(fds[0]);
+ try std.testing.expectEqual(wire.max_payload + 1, fr.overlong().?);
+}
+
+test "a client joining again after a divergence is told at once that its session is gone" {
+ var why: [600]u8 = undefined;
+ const t0 = nowNs();
+ try std.testing.expect(redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false, 1) == null);
+ try std.testing.expect(nowNs() - t0 < 100 * std.time.ns_per_ms);
+}
+
test "a replica's own keys apply at once, confirm on the fast path, and end at the authority's digest" {
const gpa = std.testing.allocator;
const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
@@ -3417,29 +3620,6 @@ test "a terminal sized again by a client's key: the step reads the grid it had,
try std.testing.expectEqual(digestOf(a), r.digest());
}
-/// What the session sends a peer: its terminals' changes (`.flush`), or,
-/// around a step's unit (Peers.input), the rows the step read that the
-/// peer lacks (`.fill`), after which the caller gives terminals the sizes
-/// the step gave them (`terminal.resizeDeferred`), then the unit.
-fn deliver(a: *Authority, screens: *Screens, peer: u16, r: *Replica, comptime what: enum { flush, fill }) !void {
- const gpa = std.testing.allocator;
- var msgs: std.ArrayListUnmanaged([]u8) = .empty;
- defer {
- for (msgs.items) |m| gpa.free(m);
- msgs.deinit(gpa);
- }
- switch (what) {
- .flush => try screens.flush(a.core, peer, r.view, &msgs),
- .fill => {
- try screens.fill(a.core, peer, &msgs);
- screens.filled(a.core);
- },
- }
- for (msgs.items) |m| if (r.screen(m)) |reply| {
- _ = try screens.take(a.core, peer, reply);
- };
-}
-
test "pardes's own keys over a terminal's history apply at once: its view moves here, a delta while they wait puts them on top again, the authority's ends the same" {
const gpa = std.testing.allocator;
const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true);
@@ -3467,7 +3647,7 @@ test "pardes's own keys over a terminal's history apply at once: its view moves
// Normal mode in the shell, up a few rows, then half a screen and more:
// the cursor leaves the view, and the view follows it back.
const keys = [_]pardes.Key{
- .{ .cp = a.core.opts.tty_toggle, .ctrl = true }, .{ .cp = 'k', .text = "k" }, .{ .cp = 'k', .text = "k" },
+ .{ .cp = a.core.opts.tty_toggle, .ctrl = true }, .{ .cp = 'k', .text = "k" }, .{ .cp = 'k', .text = "k" },
.{ .cp = 'u', .ctrl = true }, .{ .cp = 'u', .ctrl = true }, .{ .cp = 'k', .text = "k" },
};
for (keys) |k| try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .key = k })));
@@ -3620,13 +3800,6 @@ test "a join's dump, its blobs put back where they go, is the session's dump" {
try std.testing.expectEqualSlices(u8, whole, joined.items);
}
-/// The blob cache of a test, in its own directory (never the user's).
-fn testCache(tmp: *std.testing.TmpDir, buf: *[4200]u8, cap: u64) !Cache {
- var dir_buf: [std.fs.max_path_bytes]u8 = undefined;
- const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
- return Cache.of(try std.fmt.bufPrint(buf, "{s}/" ++ Cache.name, .{dir}), cap);
-}
-
test "a cached blob is hashed again as it is read: one kept cut short, corrupted, planted or longer is a miss, and goes" {
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
@@ -3718,57 +3891,6 @@ test "a blob cache that is no database is started afresh" {
try std.testing.expect(c.get(io, &hash, &out));
}
-/// A far join over a socket pair, the session's writer and reader threads
-/// against the client's `joinOn`, as the daemon hands a connection over:
-/// its hello read off first.
-fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined {
- var fds: [2]c_int = undefined;
- if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket;
- var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty;
- const state = pardes.dump.dumpJoin(core, &blobs) catch |err| {
- _ = libc.close(fds[0]);
- _ = libc.close(fds[1]);
- return err;
- };
- var whole = state.len;
- for (blobs.items) |b| whole += b.bytes.len;
- const peer = try ps.gpa.create(Peer);
- peer.* = .{ .peers = ps, .fd = fds[0], .id = 1, .view = 0, .dump = state, .blobs = blobs, .dump_gpa = core.gpa };
- peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = 1, .session = "t", .dump_len = whole, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len;
- const Serve = struct {
- fn run(p: *Peer) void {
- var hello: [wire.header_len + 14]u8 = undefined;
- var got: usize = 0;
- while (got < hello.len) {
- const n = libc.read(p.fd, hello[got..].ptr, hello.len - got);
- if (n <= 0) return p.peers.post(.{ .gone = p });
- got += @intCast(n);
- }
- p.readLoop();
- }
- };
- peer.writer = try std.Thread.spawn(.{}, Peer.writeLoop, .{peer});
- peer.reader = try std.Thread.spawn(.{}, Serve.run, .{peer});
- defer ps.letGo(peer);
- var why: [300]u8 = @splat(0);
- return joinOn(std.testing.allocator, fds[1], "t", &why, have, null) orelse {
- std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)});
- return error.JoinFailed;
- };
-}
-
-fn endJoin(j: *Joined, have: ?*Have) void {
- _ = libc.close(j.fd);
- j.rest.deinit(std.testing.allocator);
- if (have) |h| {
- h.store(std.testing.allocator, j);
- h.wait();
- } else {
- std.testing.allocator.free(j.dump);
- std.heap.c_allocator.free(j.got);
- }
-}
-
test "a far join is sent each blob once: joined again with them on disk or in the core it leaves, the state alone; a cached file gone bad, that blob again" {
const gpa = std.testing.allocator;
const io = std.testing.io;
@@ -3867,36 +3989,6 @@ test "a far join is sent each blob once: joined again with them on disk or in th
endJoin(&j, &have);
}
-/// A replica joined from the session's dump as it is now, between units,
-/// as `Peers.join` sends it: its state, its blobs put back where they go.
-fn joinedNow(a: *Authority, id: u16, view: u8) !*Replica {
- const gpa = std.testing.allocator;
- const was = Stepping.begin(&a.t, null);
- 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);
- was.end();
- var whole: std.ArrayListUnmanaged(u8) = .empty;
- defer whole.deinit(gpa);
- var pos: usize = 0;
- for (blobs.items) |b| {
- try whole.appendSlice(gpa, state[pos..b.at]);
- try whole.appendSlice(gpa, b.bytes);
- pos = b.at;
- }
- try whole.appendSlice(gpa, state[pos..]);
- return Replica.initDump(gpa, id, view, whole.items, .{ .cols = 80, .rows = 24 });
-}
-
-fn agrees(a: *Authority, r: *Replica) !void {
- if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why});
- try std.testing.expect(r.diverged == null);
- try std.testing.expect(r.quiet());
- try std.testing.expectEqual(digestOf(a), r.digest());
-}
-
test "a replica joined after a pane closed: the session's jump to it, its Panes and Recent rows, asked by either" {
const gpa = std.testing.allocator;
const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
@@ -3968,22 +4060,6 @@ test "a replica's Del of a file its session's Recent knows asks the host what th
try agrees(a, r);
}
-/// A host with ptys whose pump polls for a frame, as a client's does.
-const PtyHost = struct {
- fn resized(_: ?*anyopaque, _: u8, _: u16, _: u16) void {}
- fn poll(_: ?*anyopaque) void {}
- const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .pty_resize = resized, .poll_frame = poll };
- fn host() pardes.Host {
- return .{ .vtable = &vtable };
- }
-};
-
-fn inputOf(buf: []u8, client: u16, seq: u32, ev: pardes.Event) ![]const u8 {
- const n = (try wire.encodeClient(buf[head_len..], .{ .event = ev })).len;
- header(buf, client, seq);
- return buf[0 .. head_len + n];
-}
-
test "a client leaves, and one joins: what the leave queued (a terminal's resize) is done in its unit, on every side, before the join's dump" {
const gpa = std.testing.allocator;
const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, PtyHost.host(), true);
@@ -4116,37 +4192,6 @@ test "a client joined from a dump makes a `/` query's rows as the session does,
}
}
-/// An input of a replica's in the session's order, as `Peers.input` takes
-/// it: the terminals flushed to every peer, the step, the rows it read that
-/// each peer holds as holes (`fill`), then its unit to every replica.
-fn takeAll(a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, msg: []const u8) !void {
- const gpa = std.testing.allocator;
- for (rs, ps) |r, peer| try deliver(a, screens, peer, r, .flush);
- const u = try gpa.dupe(u8, try a.take(msg));
- defer gpa.free(u);
- var rows: std.ArrayListUnmanaged([]u8) = .empty;
- defer rows.deinit(gpa);
- for (rs, ps) |r, peer| {
- rows.clearRetainingCapacity();
- try screens.fill(a.core, peer, &rows);
- for (rows.items) |m| {
- _ = r.screen(m);
- gpa.free(m);
- }
- }
- screens.filled(a.core);
- for (rs) |r| {
- r.confirm(u);
- r.settle();
- }
-}
-
-fn sendAs(r: *Replica, a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, ev: pardes.Event) !void {
- const msg = try std.testing.allocator.dupe(u8, try r.input(ev));
- defer std.testing.allocator.free(msg);
- try takeAll(a, screens, rs, ps, msg);
-}
-
test "a step that reads a terminal's rows as the core keeps them sends a new client the ones it holds as holes" {
const gpa = std.testing.allocator;
const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, PtyHost.host(), true);
@@ -4184,51 +4229,6 @@ test "a step that reads a terminal's rows as the core keeps them sends a new cli
for ([_]*Replica{ r1, r2 }) |r| try agrees(a, r);
}
-/// A kind as a library brings one, for the Shadow's test: a counter its
-/// keys move, painted as `count N`.
-const Counter = struct {
- const pa = pardes.Kind.c;
- fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque {
- return pardes.Kind.api.alloc.?(4);
- }
- fn paint(st: ?*anyopaque, win: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {
- const n: *u32 = @ptrCast(@alignCast(st.?));
- var line: [16]u8 = undefined;
- const text = std.fmt.bufPrint(&line, "count {d}", .{n.*}) catch unreachable;
- for (text, 0..) |ch, i| win.*.cells[i] = std.mem.zeroInit(pa.pa_cell, .{ .text = .{ ch, 0, 0, 0, 0, 0, 0, 0 }, .len = 1, .flags = pa.PA_PAINTED, .fg = pa.PA_DEFAULT, .bg = pa.PA_DEFAULT });
- }
- fn press(st: ?*anyopaque, _: [*c]const pa.pa_key) callconv(.c) c_int {
- const n: *u32 = @ptrCast(@alignCast(st.?));
- n.* += 1;
- return 1;
- }
- const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Counter", .opens = 1, .doc = "a counter" })};
- const ops = std.mem.zeroInit(pardes.Kind.Ops, .{
- .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "counter".* ++ @as([9]u8, @splat(0)), .version = 1, .exts = "", .words = &words, .nwords = 1 }),
- .open = open,
- .paint = paint,
- .key = press,
- });
-};
-
-fn shows(p: *Pardes, text: []const u8) !bool {
- var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
- defer arena.deinit();
- const s = try p.render(arena.allocator());
- var row: [512]u8 = undefined;
- for (0..s.rows) |y| {
- var n: usize = 0;
- for (0..s.cols) |x| {
- const g = s.at(@intCast(x), @intCast(y)).grapheme();
- if (n + g.len > row.len) break;
- @memcpy(row[n..][0..g.len], g);
- n += g.len;
- }
- if (std.mem.indexOf(u8, row[0..n], text) != null) return true;
- }
- return false;
-}
-
test "a replica with no code for a kind draws its pane from the authority's frames, and its keys go to the authority" {
const gpa = std.testing.allocator;
const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);