summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig210
1 files changed, 210 insertions, 0 deletions
diff --git a/core/replica.zig b/core/replica.zig
index 9067a12a..957e6d5f 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -7596,8 +7596,218 @@ const NinepJoin = struct {
fn agree(n: *NinepJoin) !void {
try agrees(n.a, n.late.?);
}
+
+ fn opened(n: *NinepJoin, at: u64, mode: u8) !u32 {
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = at, .omode = mode }, true)).handle;
+ if (h == 0) return error.NoRecord;
+ return h;
+ }
+
+ fn wrote(n: *NinepJoin, at: u64, h: u32, data: []const u8) !void {
+ _ = try n.serve(.{ .tag = 2, .op = .write, .node = at, .handle = h, .data = data }, true);
+ }
+
+ /// A client joined now and let go: its load is the session as dumped,
+ /// at the welcome's digest, the fields that differ named.
+ fn loadsSame(n: *NinepJoin) !void {
+ const gpa = std.testing.allocator;
+ const joined = (try n.a.join(1, null)) orelse return error.NoView;
+ defer gpa.free(joined.dump);
+ const r = try Replica.initDump(gpa, 1, joined.view, joined.dump, .{ .cols = 80, .rows = 24 });
+ defer r.deinit();
+ if (!r.loaded(joined.digest)) {
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ std.debug.print("the client's load is not the session: {s}\n", .{try loadDiffers(n.a, r, arena.allocator())});
+ return error.LoadedAnother;
+ }
+ _ = try n.a.leave(1, false);
+ }
+};
+
+/// The fields a client's core has other than the session's, named
+/// (tape.partsDiffer).
+fn loadDiffers(a: *Authority, r: *Replica, arena: Allocator) ![]const u8 {
+ const want = try arena.create(tape.Parts);
+ const got = try arena.create(tape.Parts);
+ const w = tape.digestParts(a.core, want);
+ const g = parts: {
+ const was = Stepping.begin(&r.t, &r.epoch);
+ defer was.end();
+ break :parts tape.digestParts(r.core(), got);
+ };
+ return tape.partsDiffer(g, w, arena);
+}
+
+/// Each kind of state 9P requests leave in the core between two requests
+/// of a script's (fs.Namespace and the `tree.Open` records it holds: what
+/// each `What` holds, a half line, a line being discarded, a name taken, a
+/// look's results, writes held as one edit, a header's truncation), made by
+/// the requests a script makes.
+const NinepKinds = struct {
+ const top = pardes.ctlfs.TopFile;
+ fn screen(n: *NinepJoin) !void {
+ _ = try n.opened(@backingInt(top.screen), 0);
+ }
+ fn logReader(n: *NinepJoin) !void {
+ _ = try n.opened(@backingInt(top.log), 0);
+ }
+ fn listing(n: *NinepJoin) !void {
+ _ = try n.opened(@backingInt(top.index), 0);
+ }
+ fn event(n: *NinepJoin) !void {
+ _ = try n.opened(n.node(.event), 2);
+ }
+ fn lock(n: *NinepJoin) !void {
+ try n.wrote(n.node(.ctl), try n.opened(n.node(.ctl), 1), "lock\n");
+ }
+ fn halfLine(n: *NinepJoin) !void {
+ try n.wrote(n.node(.exec), try n.opened(n.node(.exec), 1), "Ne");
+ }
+ fn discarding(n: *NinepJoin) !void {
+ try n.wrote(n.node(.exec), try n.opened(n.node(.exec), 1), "a\x01b");
+ }
+ fn named(n: *NinepJoin) !void {
+ try n.wrote(n.node(.name), try n.opened(n.node(.name), 1), "/tmp/named.txt\n");
+ }
+ fn results(n: *NinepJoin) !void {
+ try n.wrote(n.node(.exec), try n.opened(n.node(.exec), 2), "Zerox\n");
+ }
+ fn heldWrites(n: *NinepJoin) !void {
+ try n.wrote(n.node(.body), try n.opened(n.node(.body), 1), "two\n");
+ }
+ fn sel(n: *NinepJoin) !void {
+ try n.wrote(n.node(.sel), try n.opened(n.node(.sel), 1), "a");
+ }
+ fn pager(n: *NinepJoin) !void {
+ try n.wrote(@backingInt(top.pager), try n.opened(@backingInt(top.pager), 1), "/\nheld text");
+ }
+ fn plumb(n: *NinepJoin) !void {
+ try n.wrote(@backingInt(top.send), try n.opened(@backingInt(top.send), 1), "src\n");
+ }
+ fn port(n: *NinepJoin) !void {
+ _ = try n.opened(pardes.ctlfs.Node.ofPort(pardes.Plumb.portIndex(n.a.core, "edit") orelse return error.NoPort), 0);
+ }
+ fn headerCut(n: *NinepJoin) !void {
+ // Its open holds no record: the truncation is the session's.
+ const h = (try n.serve(.{ .tag = 1, .op = .open, .node = @backingInt(top.tag), .omode = 1 }, true)).handle;
+ _ = try n.serve(.{ .tag = 3, .op = .setattr, .node = @backingInt(top.tag), .handle = h, .truncate = true }, true);
+ }
+ const all = .{
+ .{ "an open of /screen (What.snapshot)", screen },
+ .{ "a reader of /log (What.log)", logReader },
+ .{ "a listing frozen at its open (What.frozen)", listing },
+ .{ "a pane's event reader (What.event)", event },
+ .{ "a ctl open holding a pane's lock (What.ctl)", lock },
+ .{ "half a line held on exec (What.lines, Open.pending)", halfLine },
+ .{ "a refused line's tail being discarded (Open.discarding)", discarding },
+ .{ "a name taken by an open (Open.named)", named },
+ .{ "a look's results on its open and the session's (Open.results, Namespace.results)", results },
+ .{ "writes to a body held as one edit (What.writes, Namespace.batch)", heldWrites },
+ .{ "an open of sel writing on from its last (What.writes)", sel },
+ .{ "a /pager open holding its text (What.pager)", pager },
+ .{ "a plumb message short of its data (What.plumb)", plumb },
+ .{ "a reader of a plumb port (What.port)", port },
+ .{ "the workspace's tag truncated (Namespace.header_*)", headerCut },
+ };
};
+test "a client joined while 9P requests hold each kind of state the core keeps for them loads the session: digest(load(dump)) is the session's, kind by kind" {
+ inline for (NinepKinds.all) |case| {
+ var n = try NinepJoin.init("one\n", TestHost.host());
+ defer n.deinit();
+ const empty = walk.digest(pardes.filesystem.Namespace, &n.a.core.fs);
+ try case[1](&n);
+ // The kind is there: the core's 9P state holds something.
+ try std.testing.expect(walk.digest(pardes.filesystem.Namespace, &n.a.core.fs) != empty);
+ n.loadsSame() catch |err| {
+ std.debug.print("the kind: {s}\n", .{case[0]});
+ return err;
+ };
+ }
+}
+
+test "a client joined at any point of a script's random 9P requests (opens of every kind, writes, reads, releases, truncations) loads the session: a seeded few hundred joins" {
+ var prng = std.Random.DefaultPrng.init(0x5eed_d16e);
+ const rand = prng.random();
+ var n = try NinepJoin.init("one two\nthree\n", TestHost.host());
+ defer n.deinit();
+ const top = pardes.ctlfs.TopFile;
+ const nodes = [_]u64{
+ @backingInt(top.screen), @backingInt(top.log), @backingInt(top.index), @backingInt(top.tag),
+ @backingInt(top.pager), @backingInt(top.send), pardes.ctlfs.Node.ofPort(pardes.Plumb.portIndex(n.a.core, "edit").?), @backingInt(top.exec),
+ n.node(.exec), n.node(.body), n.node(.ctl), n.node(.event),
+ n.node(.sel), n.node(.look), n.node(.data), n.node(.addr),
+ };
+ const words = [_][]const u8{ "Ne", "w\n", "a\x01", "b\n", "lock\n", "unlock\n", "one", "\n", "src\n", "#0,#3", "x" };
+ var held: [24]struct { node: u64, h: u32 } = undefined;
+ var nheld: usize = 0;
+ var joins: usize = 0;
+ var tag: u64 = 10;
+ while (joins < 300) {
+ tag += 1;
+ const pick = rand.uintLessThan(u8, 10);
+ if (pick >= 8) {
+ try n.loadsSame();
+ joins += 1;
+ continue;
+ }
+ if (pick < 3 or nheld == 0) {
+ if (nheld == held.len) continue;
+ const node = nodes[rand.uintLessThan(usize, nodes.len)];
+ const h = (try n.serve(.{ .tag = tag, .op = .open, .node = node, .omode = rand.uintLessThan(u8, 3) }, true)).handle;
+ if (h != 0) {
+ held[nheld] = .{ .node = node, .h = h };
+ nheld += 1;
+ }
+ continue;
+ }
+ const at = rand.uintLessThan(usize, nheld);
+ const o = held[at];
+ switch (pick) {
+ 3, 4 => _ = try n.serve(.{ .tag = tag, .op = .write, .node = o.node, .handle = o.h, .data = words[rand.uintLessThan(usize, words.len)] }, true),
+ 5 => _ = try n.serve(.{ .tag = tag, .op = .read, .node = o.node, .handle = o.h, .size = 64 }, true),
+ 6 => _ = try n.serve(.{ .tag = tag, .op = .setattr, .node = o.node, .handle = o.h, .truncate = true }, true),
+ else => {
+ _ = try n.serve(.{ .tag = tag, .op = .release, .node = o.node, .handle = o.h, .opened = true }, true);
+ held[at] = held[nheld - 1];
+ nheld -= 1;
+ },
+ }
+ }
+}
+
+test "a 9P request answered while a join's dump is out in the host is the client's next unit's, not its load's: a join asks the host nothing, and the client loads the session at the welcome's digest" {
+ var n = try NinepJoin.init("one\n", TestHost.host());
+ defer n.deinit();
+ // The session's io, whose stat lets a connection task in first, as the
+ // turn's yield around a host call does (tape.out): a script's open of
+ // /screen, a request that waits for no quiet (ninep/tree.zig needsQuiet).
+ const Gap = struct {
+ var on: *NinepJoin = undefined;
+ var base: std.Io = undefined;
+ var vtable: std.Io.VTable = undefined;
+ var served: u32 = 0;
+ fn stat(ud: ?*anyopaque, dir: std.Io.Dir, path: []const u8, o: std.Io.Dir.StatFileOptions) std.Io.Dir.StatFileError!std.Io.File.Stat {
+ served += 1;
+ _ = on.a.core.serveFs(.{ .tag = 9, .op = .open, .node = @backingInt(pardes.ctlfs.TopFile.screen) });
+ return base.vtable.dirStatFile(ud, dir, path, o);
+ }
+ };
+ Gap.on = &n;
+ Gap.base = n.a.t.io;
+ Gap.vtable = n.a.t.io.vtable.*;
+ Gap.vtable.dirStatFile = Gap.stat;
+ n.a.t.io = .{ .userdata = Gap.base.userdata, .vtable = &Gap.vtable };
+ const yields = pardes.turn.yields;
+ n.loadsSame() catch |err| {
+ std.debug.print("the join's dump asked the host {d} times, gave up the turn {d} times\n", .{ Gap.served, pardes.turn.yields - yields });
+ return err;
+ };
+ try std.testing.expectEqual(yields, pardes.turn.yields);
+ try std.testing.expectEqual(0, Gap.served);
+}
+
test "a client joined while a script's open of a pane's exec holds half a line runs the line its session runs" {
var n = try NinepJoin.init("one\n", TestHost.host());
defer n.deinit();