diff options
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 422 |
1 files changed, 396 insertions, 26 deletions
diff --git a/core/replica.zig b/core/replica.zig index 14757d9a..781bd211 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -1887,6 +1887,16 @@ pub const Peer = struct { peer.cond.signal(io); } + /// Its last message: queued, and its writer ends once it is written. + fn last(peer: *Peer, words: []const u8) void { + const io = peer.peers.io; + peer.send(&.{words}); + peer.lock.lockUncancelable(io); + peer.closing = true; + peer.cond.signal(io); + peer.lock.unlock(io); + } + fn writeLoop(peer: *Peer) void { const io = peer.peers.io; defer peer.done.store(true, .release); @@ -2230,7 +2240,10 @@ pub const Peers = struct { } } - fn join(ps: *Peers, core: *Pardes, joining: Joining) void { + /// A client on `joining.fd` joined: a dump of the session now, its + /// threads started. At a step boundary (`boundary`), or a session's + /// first client before its first effects (runtime/session.zig). + pub fn join(ps: *Peers, core: *Pardes, joining: Joining) void { const fd = joining.fd; const t0 = nowNs(); defer { @@ -2401,14 +2414,14 @@ pub const Peers = struct { if (ps.count() == 0) return; var out: [600]u8 = undefined; const words = wire.encodeServer(&out, .{ .refused = why }) catch return; - const io = ps.io; - for (ps.list) |slot| if (slot) |peer| { - peer.send(&.{words}); - peer.lock.lockUncancelable(io); - peer.closing = true; - peer.cond.signal(io); - peer.lock.unlock(io); - }; + for (ps.list) |slot| if (slot) |peer| peer.last(words); + ps.flushed200(); + // Each joins again for its view, kept in the dump (views.away). + ps.drop(core, true); + } + + /// What each client was sent goes first, for up to 200 ms. + fn flushed200(ps: *Peers) void { const until = nowNs() + 200 * std.time.ns_per_ms; while (nowNs() < until) { const busy = for (ps.list) |slot| { @@ -2418,8 +2431,6 @@ pub const Peers = struct { const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms }; _ = libc.nanosleep(&ts, null); } - // Each joins again for its view, kept in the dump (views.away). - ps.drop(core, true); } /// A join's dump and pins let go: written, or its client gone. @@ -2442,21 +2453,30 @@ pub const Peers = struct { daemon.clients(pids[0..n]); } - /// Detach typed in client view `k`'s: its link ends (its reader says - /// it is gone, and the next boundary lets it go). + /// Detach typed in client view `k`'s: told it left the session running + /// (`left`), its last word; the client goes, and its reader says so. pub fn detach(ps: *Peers, k: u8) bool { for (ps.list) |slot| if (slot) |peer| if (peer.view == k) { - _ = libc.shutdown(peer.fd, 2); + var out: [wire.max_name + 8]u8 = undefined; + peer.last(wire.encodeServer(&out, .{ .left = ps.name[0..@min(ps.name.len, wire.max_name)] }) catch unreachable); return true; }; return false; } + /// The session ends with `code`: every client told so (`ended`), its + /// last word, before the links are let go (`dropAll`). + pub fn ended(ps: *Peers, code: u8) void { + var out: [16]u8 = undefined; + const words = wire.encodeServer(&out, .{ .ended = code }) catch unreachable; + for (ps.list) |slot| if (slot) |peer| peer.last(words); + ps.flushed200(); + } + fn stop(ps: *Peers, core: *Pardes) void { if (!ps.recording) return; pardes.panes.resizeDeferred(core); ps.screens.deinit(core); - if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real; tape.on = null; core.tape = core.opts.tape orelse &tape.none; tape.release(&ps.t); @@ -2576,13 +2596,14 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, ha } /// `dial`'s, on a connection made: the hello, then the join. The -/// connection is closed if the join fails. -fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined { +/// connection is closed if the join fails. No session named: no hello, a +/// pair's (Options.pair), whose session took it as its first client. +fn joinOn(gpa: Allocator, fd: c_int, session: ?[]const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined { var out: [wire.hello_max + 16]u8 = undefined; - const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session, .view = view } }) catch { + const hello = if (session) |name| wire.encodeClient(&out, .{ .hello = .{ .session = name, .view = view } }) catch { _ = libc.close(fd); return said(why, "that session's name is too long", .{}); - }; + } else out[0..0]; // Its first word, in the same write: whether it is far. const far = wire.encodeClient(out[hello.len..], .{ .far = have != null }) catch unreachable; var j: Joined = .{ .fd = fd, .pid = 0, .client = 0, .view = 0, .gmtoff = 0, .dump = &.{} }; @@ -2915,7 +2936,7 @@ pub fn bye(fd: c_int, again: bool) void { /// What the session streams a client after its dump, as its frontend's loop /// takes it: a unit's and a terminal message's bytes and a refusal's words /// on the C heap, the loop's to free. -pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8, blob: []u8 }; +pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8, blob: []u8, ended: u8, left }; /// The session's stream off `fd`, each message handed to `post` until the /// link ends (a refusal ends it too); `post` says whether it took it (one @@ -2930,13 +2951,16 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime .blob => .{ .blob = std.heap.c_allocator.dupe(u8, payload) catch return false }, .check => .{ .checked = (wire.decodeServer(tag, payload) catch return false).check }, .refused => .{ .refused = std.heap.c_allocator.dupe(u8, (wire.decodeServer(tag, payload) catch return false).refused) catch return false }, + .ended => .{ .ended = (wire.decodeServer(tag, payload) catch return false).ended }, + .left => .left, .pong, .welcome, .dump, .blobs, .zstd => return true, }; if (!post(x, msg)) switch (msg) { .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), - .checked => {}, + .checked, .ended, .left => {}, }; - return msg != .refused; + // A last word ends the stream. + return msg == .unit or msg == .screen or msg == .checked or msg == .blob; } }; rest.run(gpa, fd, ctx, Take.take); @@ -2946,11 +2970,23 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime if (!post(ctx, .{ .refused = words })) std.heap.c_allocator.free(words); } +/// A join's failure that is its session's end, whenever it came (the +/// session or its daemon gone, or hung up before the dump was whole): a +/// client joining again then goes as at a hang-up (`.gone`), in the same +/// words, never as a join that failed. +pub fn ended(why: []const u8) bool { + const words = std.mem.sliceTo(why, 0); + for ([_][]const u8{ "the session hung up", "nothing answers at ", "no session called " }) |w| + if (std.mem.startsWith(u8, words, w)) return true; + return false; +} + /// The words a client goes with when its session runs another build now. const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; /// A replica client's join, the terminal's and the window's alike (their -/// `runClient`): the session at `path` dialled, its build checked to be this +/// `runClient`): the session at `path` dialled (or, first, the one on its +/// pair, `o.pair`), its build checked to be this /// one's, and its dump loaded into a replica checked against the session /// as it was dumped. Or, `old` given, joined again once old's link has /// ended, for old's view (`redial`): the core it leaves held meanwhile so @@ -2963,6 +2999,7 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o const t0 = nowNs(); if (old) |prev| { j.* = redial(gpa, path, session, why, have, prev.diverged == null, j.view) orelse { + if (ended(why)) return null; var words: [600]u8 = undefined; const said_ = std.mem.sliceTo(why, 0); @memcpy(words[0..said_.len], said_); @@ -2971,7 +3008,7 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o }; if (have) |h| h.left(); prev.deinit(); - } else j.* = sameBuild(gpa, dial(gpa, path, session, why, have, null) orelse return null, why) orelse return null; + } else j.* = sameBuild(gpa, (if (o.pair) |fd| joinOn(gpa, fd, null, why, have, null) else dial(gpa, path, session, why, have, null)) orelse return null, why) orelse return null; defer { if (have) |h| h.store(gpa, j) else gpa.free(j.dump); j.dump = &.{}; @@ -3073,6 +3110,9 @@ pub const Link = struct { visible: u32 = 0, /// What the session said as it let this client go (the C heap's). refused: ?[]u8 = null, + /// Its last word: it ended, with this code; or this client left it. + ended: ?u8 = null, + left: bool = false, /// A line over its frame, until its next key (`notify`): why it joined /// again. The client's own, never the session's core. notice_buf: [240]u8 = undefined, @@ -3191,6 +3231,14 @@ pub const Link = struct { c.refused = words; c.quit = true; }, + .ended => |code| { + c.ended = code; + c.quit = true; + }, + .left => { + c.left = true; + c.quit = true; + }, } if (c.r.diverged != null) c.quit = true; } @@ -3571,6 +3619,14 @@ test "a client joining again after a divergence is told at once that its session const t0 = nowNs(); try std.testing.expect(redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false, 1) == null); try std.testing.expect(nowNs() - t0 < 100 * std.time.ns_per_ms); + try std.testing.expect(ended(&why)); + // Its session ending as it joins: hung up before the dump was whole, + // its end too, said as the end it is. + var fds: [2]c_int = undefined; + try std.testing.expectEqual(0, libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM, 0, &fds)); + _ = libc.close(fds[1]); + try std.testing.expect(joinOn(std.testing.allocator, fds[0], "gone", &why, null, 1) == null); + try std.testing.expect(ended(&why)); } test "a replica's own keys apply at once, confirm on the fast path, and end at the authority's digest" { @@ -4389,6 +4445,119 @@ test "bytes: a client joins from a dump with its terminals whole, and every side try sameTerminals(b.a, late); } +test "a session's first client on the socket it starts with (Options.pair), joined there with no hello before the session's first effects: the boot shell's spawn and first bytes reach it and the digests agree" { + try firstOnPair(false); +} + +test "bytes: a session's first client on its pair, taken before its first effects, has the boot shell's first bytes with no reload of them (the session's one at the join is of its emulator before any byte, the client's none); a client after them makes every side reload" { + try firstOnPair(true); +} + +/// A session's first client on a socket pair (runtime/session.zig's, then +/// the client's first join on its end), the session's first effects after: +/// its boot shell spawned, its first bytes output; the client quiet and +/// checked. Bytes: the client loaded as the soak's bytes clients are +/// (`replica.join` loads the screen-state kind until the switch). +fn firstOnPair(bytes: bool) !void { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const Spawns = struct { + fn spawn(ctx: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void { + const n: *u32 = @ptrCast(@alignCast(ctx.?)); + n.* += 1; + } + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .spawn = spawn }; + }; + var spawns: u32 = 0; + var fds: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; + const opts: pardes.Options = .{ .cols = 80, .rows = 24, .boot = .tty }; + const core = try Pardes.init(gpa, opts); + defer core.deinit(); + core.host = .{ .ctx = &spawns, .vtable = &Spawns.vtable }; + var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .name = "pair", .bytes = bytes, .detached = true }; + defer ps.deinit(); + defer live_peers = null; + defer ps.dropAll(core); + const reloads = pardes.Term.reloads.n; + ps.join(core, .{ .fd = fds[0] }); + var j: Joined = undefined; + var why: [600]u8 = @splat(0); + const r = if (bytes) r: { + j = joinOn(gpa, fds[1], null, &why, null, null) orelse return error.JoinFailed; + defer gpa.free(j.dump); + const r = try Replica.initDump(gpa, j.client, j.view, j.dump, opts); + r.t.screen_state = false; + r.core().echo = null; + break :r r; + } else r: { + var o = opts; + o.pair = fds[1]; + break :r join(gpa, &j, "", "", o, null, &why, null) orelse { + std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)}); + return error.JoinFailed; + }; + }; + defer r.deinit(); + defer { + _ = libc.close(j.fd); + j.rest.deinit(gpa); + } + try std.testing.expect(r.loaded(j.digest)); + // The first effects, the boot shell's spawn among them; its first bytes. + while (core.nextEffect()) |e| core.perform(e); + try std.testing.expectEqual(@as(u32, 1), spawns); + const shell: u8 = for (core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + core.update(.{ .output = .{ .pane = .at(shell), .bytes = "first$ " } }); + ps.boundary(core); + // Its check, answered after every unit and terminal message before it. + try std.testing.expect(askCheck(j.fd)); + while (!ps.mail.load(.acquire)) std.Thread.yield() catch {}; + ps.boundary(core); + const Take = struct { + r: *Replica, + fd: c_int, + digest: ?u64 = null, + fn take(t: *@This(), tag: u8, payload: []const u8) bool { + switch (std.enums.fromInt(wire.ServerTag, tag) orelse return false) { + .unit => t.r.confirm(payload), + .screen => if (t.r.screen(payload)) |reply| { + _ = sendScreen(t.fd, reply); + }, + .check => { + t.digest = (wire.decodeServer(tag, payload) catch return false).check; + return false; + }, + else => {}, + } + return true; + } + }; + var take: Take = .{ .r = r, .fd = j.fd }; + j.rest.run(gpa, j.fd, &take, Take.take); + r.settle(); + if (r.diverged) |w| std.debug.print("diverged: {s}\n", .{w}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(take.digest.?, r.digest()); + // Bytes: the session's reload at the join, of an emulator that held + // nothing yet (Pardes.init made it, the spawn is a first effect); the + // client loaded it from the dump and replayed every byte since. + try std.testing.expectEqual(reloads + @intFromBool(bytes), pardes.Term.reloads.n); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const rows = try pardes.Term.viewport(r.core().panes[shell].?.terminal.?, arena.allocator(), false); + try std.testing.expect(std.mem.indexOf(u8, rows[0], "first$") != null); + if (!bytes) return; + // One that comes after the shell's bytes: every side reloads. + var late: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &late) != 0) return error.NoSocket; + defer _ = libc.close(late[1]); + ps.join(core, .{ .fd = late[0] }); + try std.testing.expectEqual(reloads + 2, pardes.Term.reloads.n); +} + test "bytes: a session's output past a unit's bound is cut into units under it, between two reads" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; @@ -5004,7 +5173,6 @@ test "a client joined from a dump moves through another view's pane as the sessi } test "a client joined from a dump makes a `/` query's rows as the session does, though only the session has drawn its owner" { - if (comptime !pardes.syntax.enabled) return error.SkipZigTest; const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); defer a.deinit(); @@ -6542,7 +6710,6 @@ test "a client joined from a dump makes a `/` query's rows as the session does, } } }; - if (comptime !pardes.syntax.enabled) return error.SkipZigTest; const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, .{ .vtable = &WorkerSearch.vtable }, false); defer a.deinit(); @@ -6703,3 +6870,206 @@ test "a far client's replay of a message its session's host stamped takes the se try agrees(a, r); try std.testing.expect(try shows(r.core(), "--:--:-- yanked x")); } + +// A script's `pty/run` in a shell (ninep/pty.zig): the open, the line, the +// marks that end it, its answer read and the open released, each a step of +// the session's, with a client that joined from a dump taking every unit. +test "a client joined from a dump takes a 9P pty/run in a shell as the session does: the line sent at the prompt, its end answered" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const RunHost = struct { + fn taken(_: ?*anyopaque, _: pardes.Pane.Id) bool { + return false; + } + fn write(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .tty_taken = taken, .pty_write = write }; + }; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &RunHost.vtable }, false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; + a.output(.{ .output = .{ .pane = .at(shell), .bytes = prompt ++ "ls\r\n\x1b]133;C;aid=pardes\x07a b\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt } }); + r.confirm((try a.pending()).?); + r.settle(); + // A script's listing open, held across the join (a reader of /index). + { + const was = Stepping.begin(&a.t, null); + _ = a.core.serveFs(.{ .tag = 9, .op = .open, .node = @backingInt(pardes.ctlfs.TopFile.index) }); + was.end(); + a.seq += 1; + r.confirm(try a.unit(0, a.seq)); + r.settle(); + } + const joined = (try a.join(2, null)).?; + defer gpa.free(joined.dump); + r.confirm(joined.unit); + const late = try Replica.initDump(gpa, 2, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); + defer late.deinit(); + late.t.screen_state = false; + late.core().echo = null; + try std.testing.expectEqual(joined.digest, late.digest()); + const node = pardes.ctlfs.Node.of(a.core.panes[shell].?.serial, .pty_run); + const Serve = struct { + fn serve(s: *Authority, rs: []const *Replica, req: pardes.ctlfs.Req) !pardes.ctlfs.Reply { + const was = Stepping.begin(&s.t, null); + const reply = s.core.serveFs(req); + s.core.pump(s.host) catch {}; + was.end(); + s.seq += 1; + const u = try s.unit(0, s.seq); + for (rs) |x| { + x.confirm(u); + x.settle(); + } + return reply; + } + fn output(s: *Authority, rs: []const *Replica, pane: u8, bytes: []const u8) !void { + s.output(.{ .output = .{ .pane = .at(pane), .bytes = bytes } }); + const u = (try s.pending()).?; + for (rs) |x| { + x.confirm(u); + x.settle(); + } + } + }; + const both = [_]*Replica{ r, late }; + const h = (try Serve.serve(a, &both, .{ .tag = 1, .op = .open, .node = node, .omode = 2 })).handle; + _ = try Serve.serve(a, &both, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "true\n" }); + try Serve.output(a, &both, shell, "true\r\n\x1b]133;C;aid=pardes\x07\x1b]133;D;0;aid=pardes\x07" ++ prompt); + const answer = try Serve.serve(a, &both, .{ .tag = 3, .op = .read, .node = node, .handle = h, .size = 64 }); + try std.testing.expectEqualStrings("exit 0\n", a.core.fsPayload(answer)); + _ = try Serve.serve(a, &both, .{ .tag = 4, .op = .release, .node = node, .handle = h, .opened = true }); + try Serve.output(a, &both, shell, "\x1b]133;A;cl=line;aid=pardes\x07"); + try agrees(a, r); + try agrees(a, late); +} + +// The same run with the terminals crossing as screen state (a session's +// clients', until the switch to bytes): a replica's emulator is the +// deltas', its marks not the session's. +test "a client of screen-state terminals takes a 9P pty/run in a shell as the session does" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const RunHost = struct { + fn taken(_: ?*anyopaque, _: pardes.Pane.Id) bool { + return false; + } + fn write(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .tty_taken = taken, .pty_write = write }; + }; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &RunHost.vtable }, true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + const Step = struct { + var second: ?*Replica = null; + fn take(s: *Authority, sc: *Screens, p: u16, x: *Replica, u: []const u8) !void { + const gpa_ = std.testing.allocator; + const kept = try gpa_.dupe(u8, u); + defer gpa_.free(kept); + // The unit first, its screens at the frame after (Peers). + x.confirm(kept); + x.settle(); + if (second) |y| { + y.confirm(kept); + y.settle(); + } + try deliver(s, sc, p, x, .flush); + if (second) |y| try deliver(s, sc, p + 1, y, .flush); + } + fn serve(s: *Authority, sc: *Screens, p: u16, x: *Replica, req: pardes.ctlfs.Req) !pardes.ctlfs.Reply { + const was = Stepping.begin(&s.t, null); + const reply = s.core.serveFs(req); + s.core.pump(s.host) catch {}; + was.end(); + s.seq += 1; + try take(s, sc, p, x, try s.unit(0, s.seq)); + return reply; + } + fn output(s: *Authority, sc: *Screens, p: u16, x: *Replica, pane: u8, bytes: []const u8) !void { + s.output(.{ .output = .{ .pane = .at(pane), .bytes = bytes } }); + try take(s, sc, p, x, (try s.pending()).?); + } + }; + const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; + try Step.output(a, &screens, peer, r, shell, prompt ++ "ls\r\n\x1b]133;C;aid=pardes\x07a b\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt); + // A second client joins now, from the session's dump. + const joined = (try a.join(2, null)).?; + defer gpa.free(joined.dump); + try Step.take(a, &screens, peer, r, joined.unit); + const late = try Replica.initDump(gpa, 2, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); + defer late.deinit(); + const peer2 = try screens.addPeer(); + try deliver(a, &screens, peer2, late, .flush); + Step.second = late; + defer Step.second = null; + const node = pardes.ctlfs.Node.of(a.core.panes[shell].?.serial, .pty_run); + for (0..2) |_| { + const h = (try Step.serve(a, &screens, peer, r, .{ .tag = 1, .op = .open, .node = node, .omode = 2 })).handle; + _ = try Step.serve(a, &screens, peer, r, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "true\n" }); + try Step.output(a, &screens, peer, r, shell, "true\r\n\x1b]133;C;aid=pardes\x07\x1b]133;D;0;aid=pardes\x07" ++ prompt); + const answer = try Step.serve(a, &screens, peer, r, .{ .tag = 3, .op = .read, .node = node, .handle = h, .size = 64 }); + try std.testing.expectEqualStrings("exit 0\n", a.core.fsPayload(answer)); + _ = try Step.serve(a, &screens, peer, r, .{ .tag = 4, .op = .release, .node = node, .handle = h, .opened = true }); + } + try agrees(a, r); + try agrees(a, late); +} + +// Del with a pane above and below asks which way the space goes, its +// question shown once a delay is up (builtins.zig): its time was the +// host's clock read in the step, an answering entry a speculation stops at. +test "a far client's Del with panes above and below is applied at once, and its question's time is the session's" { + const gpa = std.testing.allocator; + const Clocked = struct { + var now_ns: u64 = 7 * std.time.ns_per_s; + fn now(_: ?*anyopaque) u64 { + return now_ns; + } + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .now = now }; + }; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 30, .file = "mise.toml" }, .{ .vtable = &Clocked.vtable }, false); + defer a.deinit(); + _ = try a.local(.{ .command = "New" }); + const mid = a.core.view().active; + _ = try a.local(.{ .command = "New" }); + { + const was = Stepping.begin(&a.t, null); + defer was.end(); + a.core.view().active = mid; + } + const r = try joinedFresh(a); + defer r.deinit(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + Clocked.now_ns += std.time.ns_per_s; + try key(r, ' ', &sent); + try key(r, 'd', &sent); + try std.testing.expectEqual(@as(usize, 2), r.applied); + try std.testing.expect(r.core().panes[mid].?.prompt == .del_side); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); + try agrees(a, r); + // The question shows a delay after it was asked, on the session's clock. + a.pump(); + r.confirm((try a.pending()).?); + r.settle(); + try agrees(a, r); + try std.testing.expectEqual(Clocked.now_ns + pardes.config.del_question_delay_ns, r.core().panes[mid].?.question_due_ns); +} |
