summaryrefslogtreecommitdiff
path: root/test/replicas.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/replicas.zig')
-rw-r--r--test/replicas.zig126
1 files changed, 120 insertions, 6 deletions
diff --git a/test/replicas.zig b/test/replicas.zig
index e5e719b3..48eaa857 100644
--- a/test/replicas.zig
+++ b/test/replicas.zig
@@ -336,7 +336,7 @@ const Totals = struct {
/// search (`/`, a pattern, Enter), a sweep of B1 over the PDF and its
/// wheel; a yank to the system clipboard and a paste from it; now and
/// then a client's Detach.
-fn nextEvent(random: std.Random, buf: *[64]u8, aim: Aim, looks: []const []const u8, script: *Script, far: bool) pardes.Event {
+fn nextEvent(random: std.Random, buf: *[64]u8, aim: Aim, looks: []const []const u8, script: *Script, far: bool, spare: bool) pardes.Event {
const typed = "abcdefghijklmnopqrstuvwxyz0123456789 -_=+,:;()";
const normal = "hjklwbe0$xpuyoOiaAIdcv.nN%sC";
if (script.at < script.n) {
@@ -346,13 +346,19 @@ fn nextEvent(random: std.Random, buf: *[64]u8, aim: Aim, looks: []const []const
switch (random.uintLessThan(u8, 46)) {
0...15 => {
buf[0] = typed[random.uintLessThan(usize, typed.len)];
+ if (buf[0] == 'q' and script.space) buf[0] = 'r';
+ script.space = buf[0] == ' ';
return .{ .key = .{ .cp = buf[0], .text = buf[0..1] } };
},
16...27 => {
buf[0] = normal[random.uintLessThan(usize, normal.len)];
+ script.space = false;
return .{ .key = .{ .cp = buf[0], .text = buf[0..1] } };
},
- 28...31 => return .{ .key = .{ .cp = Key.escape } },
+ 28...31 => {
+ script.space = false;
+ return .{ .key = .{ .cp = Key.escape } };
+ },
32 => return .{ .key = .{ .cp = Key.enter } },
33 => return .{ .key = .{ .cp = Key.backspace } },
34 => return .{ .key = .{ .cp = switch (random.uintLessThan(u8, 4)) {
@@ -374,7 +380,9 @@ fn nextEvent(random: std.Random, buf: *[64]u8, aim: Aim, looks: []const []const
4 => "Search Panes",
5 => "Search Recent",
6, 7 => looks[random.uintLessThan(usize, looks.len)],
- 8 => "Del",
+ // Not the session's last panes: a Del of its last ends it, and
+ // the run with it (`Soak.spare`).
+ 8 => if (spare) "Del" else "Undo",
// A tab's width: every pane's layout.
9 => "Tab 2",
10 => "Tab 8",
@@ -431,6 +439,9 @@ const Script = struct {
evs: [3]pardes.Event = undefined,
n: u8 = 0,
at: u8 = 0,
+ /// Its last key was a space: Space then q is Exit (config.zig's
+ /// leader), which ends the session and the run with it.
+ space: bool = false,
};
fn writeFile(path: [:0]const u8, lines: usize) !void {
@@ -873,6 +884,62 @@ const Watchdog = struct {
}
};
+/// At a client's mismatch only: its digest of every field, as the mismatch
+/// left its core, and the unit it parted on. The witness names the fields
+/// when its own replay of the kept stream comes to that unit (`judge`):
+/// nothing more on the session's step or the wire, so the run's timing is
+/// the untraced one.
+const Named = struct {
+ const Parted = struct { c: usize, origin: u16, seq: u32, parts: []u64, terms: [pardes.MAX_PANES]Term };
+ /// A terminal pane's: its screen's digest without its view and with
+ /// it, and where its view and cursor stand.
+ const Term = struct { content: u64 = 0, viewed: u64 = 0, grid: pardes.Term.Grid = .{} };
+
+ fn terms(p: *const pardes.Pardes) [pardes.MAX_PANES]Term {
+ var out: [pardes.MAX_PANES]Term = @splat(.{});
+ for (p.panes, &out) |slot, *t| {
+ const state = (slot orelse continue).terminal orelse continue;
+ var a = pardes.walk.Hasher.init(0);
+ state.digestScreen(&a, false);
+ var b = pardes.walk.Hasher.init(0);
+ state.digestScreen(&b, true);
+ t.* = .{ .content = a.final(), .viewed = b.final(), .grid = pardes.Term.grid(state) };
+ }
+ return out;
+ }
+ var parted: std.ArrayListUnmanaged(Parted) = .empty;
+
+ fn reset() void {
+ for (parted.items) |x| gpa.free(x.parts);
+ parted.clearRetainingCapacity();
+ }
+
+ /// Client `c` parted on `unit`: kept for the witness, said once.
+ fn say(r: Run, c: usize, rp: *replica.Replica, unit: []const u8) void {
+ const u = replica.parseUnit(unit) orelse return;
+ const mine = gpa.create(pardes.tape.Parts) catch return;
+ defer gpa.destroy(mine);
+ const got = pardes.tape.digestParts(rp.core(), mine);
+ parted.append(gpa, .{ .c = c, .origin = u.origin, .seq = u.seq, .parts = gpa.dupe(u64, got) catch return, .terms = terms(rp.core()) }) catch return;
+ std.debug.print("seed {d} rtt {d} ms: client {d} parted on unit {d}/{d} ({d} bytes; {d} applied on top, {d} pending): its fields kept for the witness\n", .{ r.seed, r.rtt_ms, c + 1, u.origin, u.seq, u.tape.len, rp.applied, rp.pending.items.len });
+ }
+
+ /// The witness at the end of unit `u`: each client that parted there,
+ /// its fields against the witness's (the session's stream replayed).
+ fn atUnit(tag: anytype, u: replica.Unit, rp: *replica.Replica, w: *std.Io.Writer) void {
+ for (parted.items) |x| if (x.origin == u.origin and x.seq == u.seq) {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const theirs = arena.allocator().create(pardes.tape.Parts) catch return;
+ const want = pardes.tape.digestParts(rp.core(), theirs);
+ w.print("seed {d} rtt {d} ms: client {d} names: at unit {d}/{d}, against the witness's replay: {s}\n", tag ++ .{ x.c + 1, u.origin, u.seq, pardes.tape.partsDiffer(x.parts, want, arena.allocator()) }) catch {};
+ for (x.terms, terms(rp.core()), 0..) |a, b, id| if (a.viewed != b.viewed or a.content != b.content) {
+ w.print(" pane {d}'s terminal: content {s}, view {s}; the client's {any}; the witness's {any}\n", .{ id, if (a.content == b.content) "equal" else "DIFFERS", if (a.viewed == b.viewed) "equal" else "DIFFERS", a.grid, b.grid }) catch {};
+ };
+ };
+ }
+};
+
const Witness = struct {
const Sent = struct { blob: bool, bytes: []u8 };
const Dump = struct { c: usize, id: u16, view: u8, cols: u16, rows: u16, join_seq: u32, bytes: []u8, start: ?usize = null };
@@ -1050,6 +1117,7 @@ const Witness = struct {
if (u.origin == 0) last0 = u.seq;
rp.since_check = if (parted == null) 1 << 20 else 0;
rp.confirm(m.bytes);
+ if (parted == null) Named.atUnit(tag, u, rp, w);
if (rp.diverged) |why| {
if (parted == null) {
parted = i;
@@ -1123,6 +1191,15 @@ const Soak = struct {
l: *SessionLoop = undefined,
/// What failed inside the loop (its wait returns nothing): the run's.
err: ?anyerror = null,
+ /// `finish` ran: the session's loop ended at the run's end, not before.
+ finished: bool = false,
+ /// The session's `quit` after its last step (`sent`): an Exit's end.
+ quit: bool = false,
+ /// The last inputs the typists drew (who, as `describe` says them),
+ /// said with an early end.
+ drawn: [8]struct { who: u16 = 0, text: [48]u8 = undefined, len: u8 = 0 } = @splat(.{}),
+ drawn_n: usize = 0,
+
// ---- its host ----
/// A pty that echoes what it is written, 2 ms later, as a shell's.
@@ -1354,7 +1431,22 @@ const Soak = struct {
/// clients' inputs, joins and leaves taken in): what a Peer's writer
/// would write, down each link; a join taken or refused; a link the
/// session shut; the echoes its steps asked of the shell.
+ fn drew(s: *Soak, who: u16, ev: pardes.Event) void {
+ const d = &s.drawn[s.drawn_n % s.drawn.len];
+ s.drawn_n += 1;
+ d.who = who;
+ d.len = @intCast(describe(&d.text, ev).len);
+ }
+
+ /// Panes enough that the Dels in flight (a typist's each) leave one.
+ fn spare(s: *const Soak) bool {
+ var n: usize = 0;
+ for (s.l.core.panes) |slot| n += @intFromBool(slot != null);
+ return n > 3;
+ }
+
pub fn sent(s: *Soak) void {
+ defer s.quit = s.l.core.quit;
s.sentOrFail() catch |err| {
if (s.err == null) s.err = err;
s.l.core.quit = true;
@@ -1811,7 +1903,8 @@ const Soak = struct {
switch (m.kind) {
.input => {
var buf: [64]u8 = undefined;
- const ev = nextEvent(s.random, &buf, s.aim, s.looks, &cl.script, net.throttled == @as(?usize, c));
+ const ev = nextEvent(s.random, &buf, s.aim, s.looks, &cl.script, net.throttled == @as(?usize, c), s.spare());
+ s.drew(@intCast(c + 1), ev);
cl.loop.input(ev);
cl.left -= 1;
if (cl.left > 0) try net.send(.{ .at = m.at + typistGap(s.random), .n = 0, .kind = .input, .who = m.who, .bytes = &.{} }) else {
@@ -1829,7 +1922,9 @@ const Soak = struct {
// what its unit cost, on this process's clock.
const reloads = pardes.Term.reloads.n;
const t0 = monoNs();
+ const whole = rp.diverged == null;
cl.loop.take(.{ .unit = try std.heap.c_allocator.dupe(u8, m.bytes) });
+ if (whole and rp.diverged != null and !cl.loop.link.put_off) Named.say(s.r, c, rp, m.bytes);
if (pardes.Term.reloads.n != reloads) try s.totals.join_client_ns.append(gpa, monoNs() - t0);
const behind = if (net.throttled == @as(?usize, c)) &s.totals.far_behind else &s.totals.near_behind;
behind.* = @max(behind.*, cl.loop.link.behind);
@@ -1890,7 +1985,8 @@ const Soak = struct {
s.moved_at = s.now;
return;
}
- if (s.now - s.moved_at < stuck_us) return;
+ // The soak's clock is a message's time: one due before it steps it back.
+ if (s.now -| s.moved_at < stuck_us) return;
std.debug.print("seed {d} rtt {d} ms: watchdog: nothing stepped or checked for {d} s of the soak's clock (since {d} us)\n", .{ s.r.seed, s.r.rtt_ms, stuck_us / std.time.us_per_s, s.moved_at });
s.report();
return error.Hung;
@@ -1918,7 +2014,8 @@ const Soak = struct {
const core = s.l.core;
s.clock();
var buf: [64]u8 = undefined;
- const ev = nextEvent(s.random, &buf, s.aim, s.looks, &s.own_script, false);
+ const ev = nextEvent(s.random, &buf, s.aim, s.looks, &s.own_script, false, s.spare());
+ s.drew(0, ev);
core.toView(.at(0));
core.update(ev);
s.own_left -= 1;
@@ -1988,6 +2085,7 @@ const Soak = struct {
/// At quiescence: every client's digest the session's, its terminals
/// the session's cell for cell, its files' texts byte for byte.
fn finish(s: *Soak) !void {
+ s.finished = true;
const totals = s.totals;
const core = s.l.core;
totals.flood_us += s.flood_end -| flood_at;
@@ -2073,6 +2171,8 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void {
if (trace) std.debug.print("seed {d} rtt {d} ms\n", .{ r.seed, r.rtt_ms });
Witness.reset();
defer Witness.reset();
+ Named.reset();
+ defer Named.reset();
var prng = std.Random.DefaultPrng.init(r.seed *% 0x9e3779b97f4a7c15 +% r.rtt_ms);
const random = prng.random();
var paths: [3][256:0]u8 = undefined;
@@ -2168,6 +2268,20 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void {
// The session: THE session loop, from its start to its end.
try SessionLoop.run(init, .{ .cols = 100, .rows = 30, .boot = .tty }, &soak);
if (soak.err) |err| return err;
+ // The loop ends without the run's end too (runtime/session.zig): every
+ // client gone at a boundary, the last by a close; or the session ended
+ // (Exit, the last pane's Del). Said, and the run failed.
+ if (!soak.finished) {
+ std.debug.print("seed {d} rtt {d} ms: the session's loop ended before the run did: {s}; the clients:", .{ r.seed, r.rtt_ms, if (soak.quit) "the session ended (Exit, the last pane's Del)" else "every client gone at a boundary, the last by a close" });
+ for (soak.clients, 1..) |cl, k| std.debug.print(" {d} {s}{s}{s};", .{ k, if (cl.r == null) "none" else "joined", if (cl.dialling) " dialling" else "", if (cl.shut) " shut" else "" });
+ std.debug.print(" typing {d}, at {d} us; the last drawn (0 the session's own):", .{ soak.typing, soak.now });
+ for (0..@min(soak.drawn_n, soak.drawn.len)) |k| {
+ const d = soak.drawn[(soak.drawn_n - 1 - k) % soak.drawn.len];
+ std.debug.print(" {d}: {s};", .{ d.who, d.text[0..d.len] });
+ }
+ std.debug.print("\n", .{});
+ return error.EndedEarly;
+ }
}
/// What `questions`'s kitty query and `images`'s four are answered with.