diff options
Diffstat (limited to 'test/replicas.zig')
| -rw-r--r-- | test/replicas.zig | 76 |
1 files changed, 75 insertions, 1 deletions
diff --git a/test/replicas.zig b/test/replicas.zig index f50fb04f..038d747a 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -213,6 +213,13 @@ fn writeFile(path: [:0]const u8, lines: usize) !void { } } +fn writeText(path: [:0]const u8, text: []const u8) !void { + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return error.OpenFailed; + defer _ = libc.close(fd); + if (libc.write(fd, text.ptr, text.len) != @as(isize, @intCast(text.len))) return error.WriteFailed; +} + const Run = struct { seed: u64, rtt_ms: u64, steps: usize, clients: u16, shared: bool, flood_mb: u64 }; /// The terminal pane's screen as a hash of screen_state's dump of it. @@ -470,6 +477,73 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { } } +/// A .patch looked at opens a +Diff, whose rows are made from files read +/// in the step (hunks.zig: the patch, the file each section names, its +/// declarations). Looked at by the authority, a replica replays the reads +/// the unit carries and makes the same +Diff; looked at as the replica's own +/// input, the first read poisons the speculation, which shows nothing and +/// waits for the authority's unit (no rollback of what it showed). Either +/// way the replica's digest and the +Diff's text are the authority's. +fn diffCase(dir: []const u8) !bool { + var host: Host = .{}; + defer { + for (host.echo.items) |e| gpa.free(e.bytes); + host.echo.deinit(gpa); + } + var buf: [256]u8 = undefined; + const source = try std.fmt.bufPrintSentinel(&buf, "{s}/a.zig", .{dir}, 0); + try writeText(source, "// one\n// two\nconst S = struct {\n fn f() void {\n a();\n b();\n log();\n }\n};\n"); + var pbuf: [256]u8 = undefined; + const patch = try std.fmt.bufPrintSentinel(&pbuf, "{s}/change.patch", .{dir}, 0); + try writeText(patch, "--- a.zig\n+++ a.zig\n@@ -3,4 +3,5 @@\n fn f() void {\n- c();\n+ a();\n b();\n+ log();\n }\n"); + var lbuf: [300]u8 = undefined; + const look = try std.fmt.bufPrint(&lbuf, "Look {s}", .{patch}); + var ok = true; + for ([_]bool{ false, true }) |own| { + const a = try replica.Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty }, host.host(), true); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const rp = try replica.Replica.init(gpa, 1, first, true); + defer rp.deinit(); + if (own) { + const msg = try gpa.dupe(u8, try rp.input(.{ .command = look })); + defer gpa.free(msg); + if (rp.stats.stopped != 1) { + std.debug.print("diff: the replica's own look at a .patch was not stopped at its first read ({d} stopped, {d} applied)\n", .{ rp.stats.stopped, rp.stats.speculated }); + ok = false; + } + if (try a.pending()) |batch| rp.confirm(batch); + rp.confirm(try a.take(msg)); + } else rp.confirm(try a.local(.{ .command = look })); + rp.settle(); + const named = for (a.core.panes, 0..) |slot, pid| { + const f = (slot orelse continue).file orelse continue; + if (std.mem.endsWith(u8, f.path, "/+Diff")) break pid; + } else null; + const mine = if (named) |pid| if (rp.core().panes[pid]) |pane| pane.file else null else null; + if (named == null or mine == null or !std.mem.eql(u8, a.core.panes[named.?].?.file.?.content, mine.?.content) or + std.mem.indexOf(u8, mine.?.content, "a.zig:5 *") == null) + { + std.debug.print("diff: {s}: the replica holds no +Diff of the authority's\n", .{if (own) "its own look" else "the authority's look"}); + ok = false; + } + if (rp.diverged) |why| { + std.debug.print("diff: {s}: the replica diverged: {s}\n", .{ if (own) "its own look" else "the authority's look", why }); + ok = false; + } else if (!rp.quiet() or rp.digest() != replica.digestOf(a)) { + std.debug.print("diff: {s}: the replica's digest is not the authority's\n", .{if (own) "its own look" else "the authority's look"}); + ok = false; + } + if (rp.stats.mispredicted + rp.stats.foreign != 0) { + std.debug.print("diff: {s}: {d} rollbacks\n", .{ if (own) "its own look" else "the authority's look", rp.stats.mispredicted + rp.stats.foreign }); + ok = false; + } + } + std.debug.print("diff: a .patch looked at by the authority and as a replica's own input: {s}\n", .{if (ok) "the same +Diff" else "FAILED"}); + return ok; +} + fn push(q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, m: Msg) !void { var mm = m; mm.n = nn.*; @@ -1437,7 +1511,7 @@ pub fn main(init: std.process.Init) !void { if (!try echoMain(seeds, first, rtts)) std.process.exit(1); return; } - var failed = false; + var failed = !try diffCase(dir); std.debug.print("{d} seeds x {d} inputs from each of {d} clients, {s}, a TUI then {d} MB of yes in the shell; costs in us (frame {d})\n", .{ seeds, steps, clients, if (shared) "sharing one view" else "a view each", flood_mb, frame_us }); std.debug.print(" rtt inputs at-once stopped view units fast rollback(mis+foreign) rate rebase spec p50/p99 copy p50/p99 (KB) rollback p50/p99 diverged unequal\n", .{}); var term_lines: std.ArrayList(u8) = .empty; |
