From ed6581dbf287d8a2fb1c439a88ce7380ee754f75 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 23:50:55 -0300 Subject: Bound a scripted run's memory sessions; check a script records the same twice A scripted run from fresh state records in memory, and a very long one grew without end. It stops at 256 MiB (ten hours or more of play at 60 Hz; a write the memory database cannot make, out of memory included, is the same): once, a warning, and no more frames, while what it holds still reads (marks, clips, past sessions). test-same plays test/past.script twice into two databases and compares them value by value with test/same_sessions.zig, but for a session's started and ended (the wall clock): every frame, keyframe, stretch and mark follows from the script alone. A third run from fresh state leaves its state file and no database. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/same_sessions.zig | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 test/same_sessions.zig (limited to 'test/same_sessions.zig') diff --git a/test/same_sessions.zig b/test/same_sessions.zig new file mode 100644 index 0000000..af13be0 --- /dev/null +++ b/test/same_sessions.zig @@ -0,0 +1,66 @@ +//! test-same (build.zig): `same_sessions A B` exits 0 when two sessions +//! databases hold the same values apart from the wall clock's (a session's +//! `started` and `ended`), and otherwise names the first that differs. +const std = @import("std"); +const Sessions = @import("Sessions"); +const DB = Sessions.Disk; +const Cursor = DB.Cursor(.read_only); + +pub fn main(init: std.process.Init) !void { + const io = init.io; + const gpa = init.arena.allocator(); + var args = init.minimal.args.iterate(); + _ = args.skip(); + var dbs: [2]DB = undefined; + var buffers: [2]std.Io.Writer.Allocating = undefined; + for (&dbs, &buffers) |*db, *buffer| { + const file = try std.Io.Dir.cwd().openFile(io, args.next() orelse return error.Usage, .{}); + buffer.* = .init(gpa); + db.* = try DB.init(io, .{ .file = file, .buffer = buffer, .fsync = false }); + } + if (!try same(gpa, dbs[0].rootCursor().readOnly(), dbs[1].rootCursor().readOnly(), "root")) std.process.exit(1); +} + +fn same(gpa: std.mem.Allocator, a: Cursor, b: Cursor, at: []const u8) anyerror!bool { + const tag = (try a.slot()).tag; + if (tag != (try b.slot()).tag) return differ(at, "kinds"); + switch (tag) { + .uint => if (try a.readUint() != try b.readUint()) return differ(at, "numbers"), + .bytes, .short_bytes => { + if (!std.mem.eql(u8, try a.readBytesAlloc(gpa, null), try b.readBytesAlloc(gpa, null))) return differ(at, "bytes"); + }, + .array_list => { + if (try a.count() != try b.count()) return differ(at, "lengths"); + var ia = try a.iterator(); + var ib = try b.iterator(); + var i: usize = 0; + while (try ia.next()) |ca| : (i += 1) { + if (!try same(gpa, ca, (try ib.next()).?, try std.fmt.allocPrint(gpa, "{s}[{d}]", .{ at, i }))) return false; + } + }, + .hash_map => { + const mb = try DB.HashMap(.read_only).init(b); + var n: usize = 0; + var ia = try a.iterator(); + while (try ia.next()) |kv_cursor| { + const kv = try kv_cursor.readKeyValuePair(); + n += 1; + const name = if (std.enums.fromInt(Sessions.K, kv.hash)) |k| @tagName(k) else "?"; + const there = try std.fmt.allocPrint(gpa, "{s}.{s}", .{ at, name }); + const cb = try mb.getCursor(kv.hash) orelse return differ(there, "presence"); + if (std.mem.eql(u8, name, "started") or std.mem.eql(u8, name, "ended")) continue; + if (!try same(gpa, kv.value_cursor, cb, there)) return false; + } + var ib = try b.iterator(); + while (try ib.next()) |_| n -%= 1; + if (n != 0) return differ(at, "fields"); + }, + else => return differ(at, "kinds (unknown)"), + } + return true; +} + +fn differ(at: []const u8, what: []const u8) bool { + std.debug.print("same_sessions: the databases' {s} differ at {s}\n", .{ what, at }); + return false; +} -- cgit v1.3