summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig132
1 files changed, 125 insertions, 7 deletions
diff --git a/core/replica.zig b/core/replica.zig
index 14757d9a..bf665a50 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -2230,7 +2230,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 {
@@ -2576,13 +2579,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 = &.{} };
@@ -2950,7 +2954,8 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime
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
@@ -2971,7 +2976,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 = &.{};
@@ -4389,6 +4394,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;