diff options
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 106 |
1 files changed, 101 insertions, 5 deletions
diff --git a/core/replica.zig b/core/replica.zig index b8d8f436..e4ed4e0a 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -740,10 +740,12 @@ pub const Replica = struct { /// A replica of a session elsewhere, as its walker dump (core/dump.zig) /// is: what a client of a socket joins with. `opts` are this process's /// (a replica's own allocators and names); its core's allocator is the - /// rollback epoch's, but for images and PDFs, whose workers allocate on - /// threads of their own. ponytail: a picture's pixels freed while a copy - /// is held are not put off; a rollback over one shows an image a frame - /// late at worst... or reads freed pixels, which no test session has. + /// rollback epoch's, its `image_allocator` too (a kind's slot whose + /// state is not plain: a speculation that closes one frees it into the + /// epoch, put off, as any). ponytail: a picture's pixels (`Kind.heap`) + /// freed while a copy is held are not put off; a rollback over one shows + /// an image a frame late at worst... or reads freed pixels, which no test + /// session has. pub fn initDump(gpa: Allocator, id: u16, view: u8, bytes: []const u8, opts: pardes.Options) !*Replica { const r = try gpa.create(Replica); r.* = .{ .gpa = gpa, .id = id, .view = view, .t = tape.memory(gpa), .guess = true }; @@ -761,7 +763,7 @@ pub const Replica = struct { var o = opts; o.frame_allocator = epoch; o.tree_sitter_allocator = epoch; - o.image_allocator = o.image_allocator orelse std.heap.c_allocator; + o.image_allocator = epoch; // The session's closed panes and Recent are the authority's. o.state = .off; // Its emulators off the epoch, as `terminal.create` makes a screen @@ -4929,3 +4931,97 @@ test "a kind's job answered at once inside its host's call (no worker free) is t try std.testing.expectEqual(@as(u32, 1), std.mem.bytesToValue(u32, a.core.panes[id].?.addon.?.plain.?[0..4])); try std.testing.expectEqualSlices(u8, a.core.panes[id].?.addon.?.plain.?, r.core().panes[id].?.addon.?.plain.?); } + +test "a closed terminal with a kitty image brought back on a client: its image drawn under a speculation, rolled back by another's unit, is drawn again from what the emulator holds" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + 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; + // A 2x2 picture in RGB (f=24): drawn, the emulator keeps it as RGBA + // of its own (TerminalImages.converted). + a.output(.{ .output = .{ .pane = shell, .bytes = "\x1b_Ga=T,i=7,f=24,s=2,v=2;ESIzESIzESIzESIz\x1b\\" } }); + r.confirm((try a.pending()).?); + // Closed and brought back: the client loads the session's dump of it, + // its image with it. + a.core.view().active = shell; + r.confirm(try a.local(.{ .command = "Del" })); + var line: [32]u8 = undefined; + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look @p{d}", .{a.core.store.closed.items[0].serial}) })); + const back = for (r.core().panes) |slot| { + if (slot) |pane| if (pane.terminal) |state| break state; + } else return error.NotBack; + try std.testing.expect(pardes.Term.hasImages(back)); + // Typing goes to the text pane (the host's write of `active`, taped). + a.core.view().active = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.file != null) break id; + } else return error.NoTextPane; + r.confirm(try a.local(.{ .focus = true })); + const Seen = struct { + px: ?[4]u8 = null, + fn take(ctx: ?*anyopaque, s: *const pardes.Surface) void { + const seen: *@This() = @ptrCast(@alignCast(ctx.?)); + for (s.placements.items) |pl| seen.px = pl.raster.bytes()[0..4].*; + } + }; + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // Its own key, at once, a copy held; a frame drawn over it. + try key(r, 'o', &sent); + try std.testing.expect(r.copy.held); + var seen: Seen = .{}; + r.draw(.{ .ctx = &seen, .f = Seen.take }); + try std.testing.expectEqual([4]u8{ 0x11, 0x22, 0x33, 0xff }, seen.px.?); + // The session's own key first: back to the copy, the key applied again. + r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + r.settle(); + seen = .{}; + r.draw(.{ .ctx = &seen, .f = Seen.take }); + try std.testing.expectEqual([4]u8{ 0x11, 0x22, 0x33, 0xff }, seen.px.?); + r.confirm(try a.take(sent.items[0])); + r.settle(); + try agrees(a, r); +} + +test "a far client's own Del of a pane whose kind's state is not plain, rolled back by another's unit: the pane comes back whole, and is closed again with the session's order" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const counter = pardes.Kind.loadedOf(&Counter.ops, "/x/counter.so", @splat(7), &words); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &counter } }); + _ = try a.local(.{ .command = "Counter" }); + // Joined from the dump: the kind's pane is a line here (no code), its + // slot the dump's. + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + const id = r.core().view().active; + try std.testing.expect(r.core().panes[id].?.addon != null); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // Its own Del at once, and a key after it: the pane's teardown (its + // slot freed) is that key's step's (`reapPanes`). + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .command = "Del" }))); + try key(r, 'j', &sent); + try std.testing.expectEqual(@as(usize, 2), r.applied); + try std.testing.expect(r.core().panes[id] == null); + // The session's own key first: back to the copy, the pane with it. + r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + r.settle(); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + try agrees(a, r); + try std.testing.expect(r.core().panes[id] == null); +} |
