diff options
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/main.zig | 3 | ||||
| -rw-r--r-- | runtime/upgrade.zig | 102 |
2 files changed, 104 insertions, 1 deletions
diff --git a/runtime/main.zig b/runtime/main.zig index 8558e25f..7c2998f5 100644 --- a/runtime/main.zig +++ b/runtime/main.zig @@ -52,6 +52,9 @@ pub const debug = struct { pardes.replica.dying(); @import("vaxis").recover(); } + // A fault in C code (an addon's library, MuPDF, tree-sitter) leaves + // a record as a panic does. + @import("runtime").crash.record(name); return std.debug.defaultHandleSegfault(addr, name, opt_ctx); } }; diff --git a/runtime/upgrade.zig b/runtime/upgrade.zig index b385556c..12163743 100644 --- a/runtime/upgrade.zig +++ b/runtime/upgrade.zig @@ -452,7 +452,7 @@ pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Op // keep its blocks for as long as this session runs. if (rec.old >= 0) _ = libc.close(rec.old); rec.old = -1; - p.setMessage(.at(p.view().active), if (rec.refused.len > 0) + tellEveryView(p, if (rec.refused.len > 0) std.fmt.bufPrint(&buf, "Upgrade: the new build could not load this session ({s}); it goes on as {s}", .{ rec.refused[0..@min(rec.refused.len, 200)], version_line }) catch "Upgrade: the new build could not load this session; it goes on as before" else "Upgrade: now " ++ version_line); @@ -479,6 +479,106 @@ pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Op } } +/// An Upgrade is the session's news, not the asking view's: said in every +/// view at the pane its keyboard is on (each client reads it on its own +/// screen), a pane two views are on once; the live view is only the one +/// whose input came last before the dump. +fn tellEveryView(p: *pardes.Pardes, text: []const u8) void { + var told: [pardes.MAX_PANES]bool = @splat(false); + for (p.views) |v| if (v) |view| if (!told[view.active]) { + told[view.active] = true; + p.setMessage(.at(view.active), text); + }; +} + +test "an upgrade's outcome is said in every view, at the pane its keyboard is on, whichever view's input came last" { + const gpa = std.testing.allocator; + const p = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + _ = try p.setTestFile("before the upgrade\n"); + const asked = p.view().active; + // A second client's view, its keyboard on a pane of its own, the input + // that came last its own (as a client's keys before an Upgrade over 9P). + const k = p.openView() orelse return error.NoView; + p.toView(k); + try std.testing.expect(p.executeBuiltinLine(p.view().active, "New")); + try std.testing.expect(p.view().active != asked); + const q = try upgradedForTest(gpa, p); + defer q.deinit(); + var seen: usize = 0; + for (q.views, 0..) |v, i| if (v != null) { + try std.testing.expect(saysOutcome(q, i)); + seen += 1; + }; + try std.testing.expectEqual(@as(usize, 2), seen); + try std.testing.expect(q.views[0].?.active != q.views[k.index()].?.active); +} + +test "an upgrade's outcome waits in each view kept for its client until that client is back for it: no other view's key and no clock take it" { + const gpa = std.testing.allocator; + const p = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + defer p.deinit(); + _ = try p.setTestFile("before the upgrade\n"); + // Two clients' views, each its keyboard on a pane of its own, kept for + // their clients as an Upgrade keeps them (Peers.refuseAll: away). + var kept: [2]pardes.Pardes.View.Id = undefined; + for (&kept) |*k| { + k.* = p.openView() orelse return error.NoView; + p.toView(k.*); + try std.testing.expect(p.executeBuiltinLine(p.view().active, "New")); + } + p.toView(.at(0)); + for (kept) |k| p.awayView(k); + const q = try upgradedForTest(gpa, p); + defer q.deinit(); + // As the daemon's tests run (MessageLinger 0): a dismissal is at once. + q.settings.message_linger_ms = 0; + const key: pardes.Event = .{ .key = .{ .cp = 'j', .text = "j" } }; + // A key in the session's own view: its outcome goes, not theirs. + q.update(key); + try std.testing.expect(!saysOutcome(q, q.live)); + for (kept) |k| try std.testing.expect(saysOutcome(q, k.index())); + // Nobody attached: the clock takes nothing a kept view waits for. + q.unattended = true; + q.update(.tick); + for (kept) |k| try std.testing.expect(saysOutcome(q, k.index())); + q.unattended = false; + // A client back for its view has it (its join's dump); the next key, + // anyone's, takes it. + try std.testing.expect(q.rejoinView(kept[0])); + try std.testing.expect(saysOutcome(q, kept[0].index())); + q.update(key); + try std.testing.expect(!saysOutcome(q, kept[0].index())); + try std.testing.expect(saysOutcome(q, kept[1].index())); + // One never back: once its view's kept time is up, as anyone's. + q.advance(q.views[kept[1].index()].?.away_ns + pardes.views.away_kept_ns); + q.update(key); + try std.testing.expect(!saysOutcome(q, kept[1].index())); +} + +/// Whether view `k`'s keyboard pane says the Upgrade's outcome. +fn saysOutcome(q: *const pardes.Pardes, k: usize) bool { + const pane = q.panes[q.views[k].?.active].?; + return std.mem.startsWith(u8, pane.msg[0..pane.msg_len], "Upgrade: now pardes "); +} + +/// `p` dumped and loaded as an Upgrade's new process loads it (`load`). +fn upgradedForTest(gpa: std.mem.Allocator, p: *pardes.Pardes) !*pardes.Pardes { + const bytes = try pardes.dump.byteDump(p); + defer gpa.free(bytes); + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pardes-upgrade-1.dump", .data = bytes }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + var text_buf: [4300]u8 = undefined; + var rec = try Record.decode(try std.fmt.bufPrint(&text_buf, "link -1\nstub -1\nold -1\nwindow 0\nexe /x/pardes\ndump {s}/pardes-upgrade-1.dump\n", .{dir})); + const q = try load(gpa, gpa, .{ .tty_only = true }, &rec); + errdefer q.deinit(); + try std.testing.expect(!rec.fresh); + return q; +} + test "an upgrade's record reads back as it was written, and refuses one cut short" { var rec: Record = .{ .link = 5, .stub = 7, .old = 9, .window = true, .size = "1402x1488", .exe = "/a b/pardes", .dump = "/home/u/.local/share/pardes/pardes.xitdb", .label = "upgrade-1", .state = .kept }; rec.addPty(3, 1234, 11); |
