diff options
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/core/replica.zig b/core/replica.zig index 6c037cd2..9e6183a1 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -9203,3 +9203,115 @@ test "a client joined after its session's shell was found to have no prompt mark _ = try n.serve(.{ .tag = 4, .op = .release, .node = run, .handle = h, .opened = true }, true); try n.agree(); } + +test "a picker's preview opens and closes a plain kind's state in the pane it lands in, no pane opened or closed: a client with a copy ahead never replays it over the copy, and every state the kind opens is closed once" { + // The soak's (test/replicas.zig with the pdf addon): `allocator leaks + // detected` at seed 0 rtt 80, the documents of PDFs a picker previewed + // (Pick.showKind) on a client, opened by a unit it kept to replay over + // its copy and opened again by the replay, the first never closed. + // A plain kind that holds something of its own outside its block for + // each state it opens, as the pdf addon holds a document (its `Own`): + // each open notes its block, each close lets it go, so a state opened + // and never closed stays noted. Its files are `.own`. + const Owned = struct { + const pa = pardes.Kind.c; + var noted: [8]?*anyopaque = @splat(null); + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + const st = pardes.Kind.api.alloc.?(4); + for (¬ed) |*n| if (n.* == null) { + n.* = st; + break; + }; + return st; + } + fn close(st: ?*anyopaque) callconv(.c) void { + for (¬ed) |*n| if (n.* == st) { + n.* = null; + return; + }; + } + fn paint(_: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {} + fn held() usize { + var n: usize = 0; + for (noted) |st| n += @intFromBool(st != null); + return n; + } + 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 = "owned".* ++ @as([11]u8, @splat(0)), .version = 1, .exts = ".own", .flags = pa.PA_PLAIN }), + .open = open, + .close = close, + .paint = paint, + }); + }; + const gpa = std.testing.allocator; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.createDir(std.testing.io, ".git", .default_dir); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "paper.own", .data = "own" }); + var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + var line: [std.Io.Dir.max_path_bytes + 32]u8 = undefined; + { + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); + defer a.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const owned = pardes.Kind.loadedOf(&Owned.ops, "/x/owned.so", @splat(7), &words); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &owned } }); + _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/notes.txt", .{dir}) }); + // A client in a view of its own, running the kind's code. + const joined = (try a.join(1, null)).?; + defer gpa.free(joined.dump); + const r = try Replica.initDump(gpa, 1, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + r.core().kinds.ops[r.core().kinds.byName("owned").?] = &Owned.ops; + // SPC f f p at the session, in its view: its picker lists the tree, + // and paper.own, the one row, shows through the kind in the pane it + // lands in. The client idle meanwhile: its copy taken ahead, each + // unit replayed on top of it. + const At = struct { + fn sessionView(au: *Authority) void { + const was = Stepping.begin(&au.t, null); + au.core.toView(.at(0)); + was.end(); + } + }; + At.sessionView(a); + for (" ffp") |c| { + r.ready(); + r.confirm(try a.local(.{ .key = .{ .cp = c, .text = &.{c} } })); + r.ready(); + r.confirm(try a.local(.tick)); + } + const landing = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.addon != null) break id; + } else return error.NoPreview; + try std.testing.expect(r.core().panes[landing].?.addon != null); + // A key of the client's own on top, and the session's own key + // first: the client back to its copy, the units since replayed. + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + try key(r, 'j', &sent); + try std.testing.expectEqual(@as(usize, 1), r.applied); + const foreign = r.stats.foreign; + r.confirm(try a.local(.{ .key = .{ .cp = 'q', .text = "q" } })); + r.settle(); + try std.testing.expectEqual(foreign + 1, r.stats.foreign); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + try agrees(a, r); + // Esc in the picker's view: the preview's state closed, the pane's + // own text back. + At.sessionView(a); + r.confirm(try a.local(.{ .key = .{ .cp = pardes.Key.escape } })); + r.settle(); + try agrees(a, r); + try std.testing.expect(r.core().panes[landing].?.addon == null); + } + try std.testing.expectEqual(@as(usize, 0), Owned.held()); +} |
