summaryrefslogtreecommitdiff
path: root/test/behaviour/root_ctl.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 14:42:45 -0300
committerGabriel Schneider <[email protected]>2026-11-05 19:16:37 -0300
commit15a1b94eaa73747afaa299262a2d6e4fd6af42fd (patch)
tree46bff4f35b642521eead62bc3b5657902e5fee0b /test/behaviour/root_ctl.zig
parent4e096f6dc6db043caa630514fa75bd18438cb15b (diff)
downloadpardes-15a1b94eaa73747afaa299262a2d6e4fd6af42fd.tar.gz
pardes-15a1b94eaa73747afaa299262a2d6e4fd6af42fd.zip
dumps: a Dump is a labelled entry of the state file (src/Db.zig, architecture §14.3), one fsynced transaction: `dumps/<label>` holds the session as a database of its own, which the walk writes by field name into xitdb's maps (walk.Put: a struct a map keyed by its fields' names, a union a map of its one field, records a list, plain numbers one bytes value tagged with their kind and width, an enum its name, a number a uint or an int, an optional an absent key, a type with dumpWrite its bytes, a .blob text of 4 KiB and up its BLAKE3 hash) and another build reads by name (walk.Get: defaults, notes, a pane it cannot read left out, as the byte loader did), with the hashes of the texts it keeps once under `blobs/`, so an unchanged text is written once across dumps; labels are `pardes-<date>-<time>` (`-2` and on in one second), `last` names the newest, the newest 32 are kept and the texts no kept one names are dropped in the same transaction, and at a session's end the file is compacted into `pardes.xitdb.new` and renamed over once it is past twice what it holds plus 64 MiB (another session notices the new file by its inode); a dump travels as an image, a database in memory holding it alone, deterministic (std.Io.failing's zero ids, so the replay's fixpoint holds): what Dump hands its host, what `Dump export <path>` and $PARDES_DUMP write whole (the snapshot goldens' `Restore <path>` unchanged), what a Restore of a label is read into, what a handover carries (`handover-<seq>`), and what `-l` and `Restore <path>` read; Restore takes a label or a path (bare: this session's last dump, else the newest), a label the file keeps none of refused in words (`Restore: no dump labelled X`, asked of the authority as the closed panes are); `/dumps` lists them, `<label> <date> <size> <panes>`, the newest first; Upgrade commits `upgrade-<pid>` before the exec, its record naming the state file and the label (a record without one, an older build's, names its `.dump` file, read as before), and the label stays; the byte dump stays for the join, the closed panes and the old `.dump` files (dump.byteDump), and dump.outPath, uniqueIn and upgradePath go for dump.label; measured (Debug, a session with a 3.2 MB file and three scratch panes, 20 dumps): the image on the loop 64-66 ms (59 ms of it BLAKE3 of the file, 0.65 ms in ReleaseFast) where the byte dump was 0.7 ms, the commit 9-16 ms with its fsyncs where the file was written in 2, a Restore 1.8 ms to read and 11.4 to load, and the file 3.55 MB after the first dump and 379 KB a dump after it with the big text unchanged (3.58 MB a dump with it edited each time, as twenty `.dump` files would be); tests: dump.zig's 'two dumps in one second get names of their own' and 'a dump makes every directory missing on the way to its DumpDir' become Db.zig's 'two dumps with one label get labels of their own' and 'a Dump makes every directory missing on the way to its DumpDir, and the file there', 'a session read by name, field by field as another build reads it, is the same session' becomes 'a session's byte dump read by name...' beside the new 'a session's image, read by name as every load reads it, is the same session, and dumps to the same bytes again', two replica tests compare the join's stream with dump.byteDump, the Handover tests read the carried image, upgrade's record test carries a label, the root listing tests and the monkey list `dumps`, the behaviour suite gains five root-ctl tests (a Dump and Restore round trip through 9P with `/dumps` and the tag's word, a label kept none of refused in words, the 33rd dump dropping the first, an unchanged text written once across two dumps by the file's size, a killed Dump leaving the one before it whole), the test host performs a Dump's write and a Restore as a host does, the monkey lets go of its log follower at a Restore, and daemon.py's upgrade case finds the kept dump in `/dumps`
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'test/behaviour/root_ctl.zig')
-rw-r--r--test/behaviour/root_ctl.zig123
1 files changed, 123 insertions, 0 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);
+}