diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/behaviour/root_ctl.zig | 123 | ||||
| -rw-r--r-- | test/daemon.py | 6 | ||||
| -rw-r--r-- | test/monkey9p.zig | 10 | ||||
| -rw-r--r-- | test/process/tree.zig | 2 | ||||
| -rw-r--r-- | test/session.zig | 20 |
5 files changed, 155 insertions, 6 deletions
diff --git a/test/behaviour/root_ctl.zig b/test/behaviour/root_ctl.zig index b9909eef..7cf3025e 100644 --- a/test/behaviour/root_ctl.zig +++ b/test/behaviour/root_ctl.zig @@ -140,3 +140,126 @@ test "root-ctl: Joincol with no column to the right and Theme with no such theme try s.write("/ctl", "Theme DRACULA\n"); try std.testing.expect(has(try s.read("/ctl"), "\nTheme dracula\n")); } + +/// The labels /dumps lists, newest first. +fn labels(s: *Session) ![]const []const u8 { + var out: std.ArrayList([]const u8) = .empty; + var lines = std.mem.tokenizeScalar(u8, try s.read("/dumps"), '\n'); + while (lines.next()) |line| try out.append(s.arena.allocator(), line[0..std.mem.indexOfScalar(u8, line, ' ').?]); + return out.items; +} + +fn stateSize(dir: []const u8) !u64 { + var buf: [4200]u8 = undefined; + return (try std.Io.Dir.cwd().statFile(std.testing.io, try std.fmt.bufPrint(&buf, "{s}/pardes.xitdb", .{dir}), .{})).size; +} + +test "root-ctl: Dump keeps the session in the state file under a label /dumps lists and the workspace tag names, and Restore of it brings it back" { + var s = try Session.initFile(std.testing.allocator, "dumped text\n"); + defer s.deinit(); + const pane = try s.focused(); + try s.write("/ctl", "Dump\n"); + const listed = try labels(&s); + try std.testing.expectEqual(@as(usize, 1), listed.len); + try std.testing.expect(std.mem.startsWith(u8, listed[0], "pardes-")); + // `<label> <date> <size> <panes>`: one pane, its text in its size. + var words = std.mem.tokenizeScalar(u8, std.mem.trimEnd(u8, try s.read("/dumps"), "\n"), ' '); + _ = words.next(); + try std.testing.expect(std.mem.endsWith(u8, words.next().?, "Z")); + try std.testing.expect(try std.fmt.parseInt(u64, words.next().?, 10) > "dumped text\n".len); + try std.testing.expectEqualStrings("1", words.next().?); + try std.testing.expect(has(try s.read("/tag"), s.at("Restore {s}", .{listed[0]}))); + try s.write(s.at("/pane/{d}/body", .{pane}), "changed\n"); + // Unsaved text is asked about once; the second Restore goes ahead. + if (try s.refused("/ctl", s.at("Restore {s}\n", .{listed[0]})) != 0) try s.write("/ctl", s.at("Restore {s}\n", .{listed[0]})); + try s.expect(s.at("/pane/{d}/body", .{pane}), "dumped text\n"); + try std.testing.expect(has(try s.read("/log"), s.at("restore {s}\n", .{listed[0]}))); + // Bare, the last one this session made. + try s.write("/ctl", "Restore\n"); + try s.expect(s.at("/pane/{d}/body", .{pane}), "dumped text\n"); +} + +test "root-ctl: a Restore of a label the state file keeps none of is refused in words, changing nothing" { + var s = try Session.initFile(std.testing.allocator, "kept\n"); + defer s.deinit(); + try std.testing.expect(try s.refused("/ctl", "Restore pardes-19990101-000000\n") != 0); + try std.testing.expect(has(s.said, "Restore: no dump labelled pardes-19990101-000000")); + try std.testing.expect(try s.refused("/ctl", "Restore\n") != 0); + try std.testing.expect(has(s.said, "Restore: no dump yet; name one")); + try s.expect(s.at("/pane/{d}/body", .{try s.focused()}), "kept\n"); +} + +test "root-ctl: the state file keeps the newest 32 dumps: the 33rd drops the first" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Dump\n"); + const first = (try labels(&s))[0]; + for (0..32) |_| try s.write("/ctl", "Dump\n"); + const kept = try labels(&s); + try std.testing.expectEqual(@as(usize, 32), kept.len); + for (kept) |l| try std.testing.expect(!std.mem.eql(u8, l, first)); + try std.testing.expect(try s.refused("/ctl", s.at("Restore {s}\n", .{first})) != 0); + try std.testing.expect(has(s.said, "no dump labelled")); +} + +test "root-ctl: a text unchanged since the last Dump is not written again: the state file grows by the session, not the text" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var dir_buf: [std.fs.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + var s = try Session.initShared(std.testing.allocator, dir); + defer s.deinit(); + const pane = try s.newPane(); + const text = try s.arena.allocator().alloc(u8, 1 << 20); + for (text, 0..) |*b, i| b.* = if (i % 64 == 63) '\n' else 'a' + @as(u8, @intCast(i % 26)); + try s.write(s.at("/pane/{d}/body", .{pane}), text); + const before = try stateSize(dir); + try s.write("/ctl", "Dump\n"); + const once = try stateSize(dir); + try std.testing.expect(once - before > text.len); + try s.write("/ctl", "Dump\n"); + // The session's own maps, a few hundred KB: not the text again. + const twice = try stateSize(dir); + try std.testing.expect(twice - once < text.len / 2); + try std.testing.expectEqual(@as(usize, 2), (try labels(&s)).len); +} + +test "root-ctl: a Dump killed in the middle leaves the one before it whole, and the next one is made" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var dir_buf: [std.fs.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + var label_buf: [64]u8 = undefined; + var label: []const u8 = ""; + { + var s = try Session.initShared(std.testing.allocator, dir); + defer s.deinit(); + try s.write(s.at("/pane/{d}/body", .{try s.newPane()}), "the dump before\n"); + try s.write("/ctl", "Dump\n"); + const listed = try labels(&s); + label = label_buf[0..listed[0].len]; + @memcpy(label_buf[0..listed[0].len], listed[0]); + } + // What a Dump killed before its commit leaves: its bytes past the end + // the file's header says is committed. + { + const file = try tmp.dir.openFile(std.testing.io, "pardes.xitdb", .{ .mode = .read_write }); + defer file.close(std.testing.io); + const end = try file.length(std.testing.io); + var junk: [70000]u8 = undefined; + for (&junk, 0..) |*b, i| b.* = @truncate(i *% 131 +% 7); + try file.writePositionalAll(std.testing.io, &junk, end); + } + var s = try Session.initShared(std.testing.allocator, dir); + defer s.deinit(); + try std.testing.expectEqualStrings(label, (try labels(&s))[0]); + try s.write("/ctl", s.at("Restore {s}\n", .{label})); + try std.testing.expect(has(try s.read("/index"), "+New")); + var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + const scratch = while (rows.next()) |row| { + if (has(row, "+New")) break row[0..std.mem.indexOfScalar(u8, row, ' ').?]; + } else return error.NoScratch; + try s.expect(s.at("/pane/{s}/body", .{scratch}), "the dump before\n"); + try s.write("/ctl", "Dump\n"); + try std.testing.expectEqual(@as(usize, 2), (try labels(&s)).len); +} diff --git a/test/daemon.py b/test/daemon.py index 52e0bccd..0c4c2f3c 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -1589,8 +1589,10 @@ def upgrades(old, new): client.write(f'/pane/{shell}/exec', b'Upgrade\n') client.socket.close() client = saying(sock, 'brings the rest back') - kept = root / '.local' / 'share' / 'pardes' / f'pardes-upgrade-{session}.dump' - assert kept.exists(), list((root / '.local' / 'share' / 'pardes').iterdir()) + # The dump stays in the state file, under its label (a second + # upgrade of one pid is `upgrade-<pid>-2`). + kept = client.read('/dumps').decode() + assert any(line.split()[0].startswith(f'upgrade-{session}') for line in kept.splitlines()), kept assert alive(shell_pid) and ppid(shell_pid) == session pids = {p: run(client, p, b'echo $$\n') for p in terminals(client)} adopted = [p for p, out in pids.items() if out.split(b'\n')[1:2] == [str(shell_pid).encode()]] diff --git a/test/monkey9p.zig b/test/monkey9p.zig index d6568d1e..f33c58f8 100644 --- a/test/monkey9p.zig +++ b/test/monkey9p.zig @@ -15,8 +15,9 @@ //! directories and working directory, so a panic or a hang is a failure //! the parent reports and shrinks like any other. //! -//! What T1 leaves out: Session performs no effects (no shell runs, nothing -//! is saved or dumped, a Restore hangs nothing up), and no 9P engine stands +//! What T1 leaves out: Session performs few effects (no shell runs, nothing +//! is saved; a Dump goes to its state in memory, and a Restore puts a new +//! core in the old one's place, its fids gone as a hang-up's), and no 9P engine stands //! in front of the core, so the engine's own refusals (a write on an open //! for reading, a directory opened to write, a mode bit missing) and //! OTRUNC's truncation first are made here, as cloud9's fs.zig makes them. @@ -78,7 +79,7 @@ const builtins = [_][]const u8{ "Sort", "Font", "Local", }; const pane_files = [_][]const u8{ "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", "sel", "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", "pty", "pty/run", "pty/data", "pty/ctl", "pty/status" }; -const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "" }; +const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "" }; const long_x: [300]u8 = @splat('x'); const col_files = [_][]const u8{ "tag", "ctl", "exec" }; const writable = [_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "body", "data", "xdata", "addr", "dot", "limit", "name", "sel", "dirty", "mark", "scroll", "errors", "event", "pty/run", "pty/data", "pty/ctl", "log" }; @@ -931,6 +932,9 @@ fn exec(c: *Ctx, a: Allocator, res: *Res, op: Op) anyerror!void { for (c.handles.items) |*h| _ = c.s().ask(&h.fid.w, .clunk, .{}); c.handles.clearRetainingCapacity(); c.marker = null; + // The log's follower with them: the new core's log is its own. + c.follower = null; + c.lines.clearRetainingCapacity(); }, .host => |o| if (o.data) |data| try putFile(a, c.root, o.name, data) else removeFile(a, c.root, o.name), .seq => |ops| for (ops) |sub| try exec(c, a, res, sub), diff --git a/test/process/tree.zig b/test/process/tree.zig index 70b2a89c..0136cc80 100644 --- a/test/process/tree.zig +++ b/test/process/tree.zig @@ -105,7 +105,7 @@ fn discovery(gpa: std.mem.Allocator, exe: [:0]const u8) !void { const top = try c.list(a, "/"); // A set: which files the root holds, not the order a listing gives. - const want = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "pane", "col", "os" }; + const want = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "pane", "col", "os" }; var others: usize = 0; for (top) |n| { if (contains(&.{ "src", "shaders" }, n)) continue; diff --git a/test/session.zig b/test/session.zig index 71d723a4..71627dad 100644 --- a/test/session.zig +++ b/test/session.zig @@ -196,12 +196,17 @@ pub const Session = struct { // As the server does (src/9p_io.zig): a clunk's held lines run then. if (q.op == .release) pardes.ctlfs.runClosedLines(pardesOf(s)); s.drain(); + // A Restore asked for is performed once its write is answered (the + // write's reply holds nothing of the core it replaces). + s.restoreIfAsked(); return r; } fn drain(s: *Session) void { while (pardesOf(s).nextEffect()) |e| switch (e) { .save_file, .save_text => s.saved += 1, + // Into the session's state (a database in memory, or its file). + .write_dump => pardes.host_io.writeDump(pardesOf(s), pardesOf(s).dump_out.?), .lsp => |q| if (s.languages) |io| s.askServer(io, .{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }), else => {}, }; @@ -224,6 +229,21 @@ pub const Session = struct { p.update(.{ .lsp_resp = .{ .id = req.id, .rows = rows } }); } + /// A Restore the write asked for, performed as a host performs it after + /// the write is answered (host_io.restoreCore): the dump read, and the + /// new core in the old one's place. + fn restoreIfAsked(s: *Session) void { + const p = pardesOf(s); + const gpa = p.gpa; + const name = p.takeRestore() orelse return; + const bytes = pardes.dump.restoreBytes(p, gpa, name) catch |err| return p.reportError(p.active, "Restore", err); + defer gpa.free(bytes); + const nc = pardes.dump.restore(p, bytes, name) catch |err| return p.reportError(p.active, "Restore", err); + p.deinit(); + s.core = nc; + s.drain(); + } + fn pardesOf(s: *Session) *Pardes { return @ptrCast(@alignCast(s.core)); } |
