summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/behaviour/addons.zig13
-rw-r--r--test/behaviour/plumber.zig43
-rw-r--r--test/monkey9p.zig25
-rw-r--r--test/replicas.zig114
4 files changed, 184 insertions, 11 deletions
diff --git a/test/behaviour/addons.zig b/test/behaviour/addons.zig
index 9b96a3d6..451cb1e3 100644
--- a/test/behaviour/addons.zig
+++ b/test/behaviour/addons.zig
@@ -185,7 +185,7 @@ test "addons: the image addon opens the pictures a Look names, fitted, with the
try std.testing.expect(has(try s.read("/screen"), "\"src\":[0,0,4,2]"));
}
-test "addons: a Plumb rule to a loaded kind opens that kind's pane, is refused naming one not loaded, and with its kind gone the look falls through to text" {
+test "addons: a plumbing rule to a loaded kind opens that kind's pane; to one not loaded, or with its kind gone, the look falls through to text" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
try load(&s, Session.image_addon);
@@ -196,9 +196,14 @@ test "addons: a Plumb rule to a loaded kind opens that kind's pane, is refused n
// A PNG under a name no kind claims.
const png = "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf\xc0\x00\x00\x03\x01\x01\x00\xc9\xfe\x92\xef\x00\x00\x00\x00IEND\xaeB`\x82";
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "red.pic", .data = png });
- try std.testing.expect(try s.refused("/ctl", "Plumb suffix .pic to nokind\n") != 0);
- try std.testing.expect(has(s.said, "Plumb: invalid port: url, dir, file or a loaded kind"));
- try s.write("/ctl", "Plumb suffix .pic to image\n");
+ // A port no kind reads is no refusal (plumber(4) never refuses one):
+ // nothing reads it, the rule set starts nothing, and Look looks itself.
+ const rule = "type is text\ndata matches '.*\\.pic'\narg isfile $data\nplumb to {s}\n\ninclude basic\n";
+ try s.replace("/plumb/rules", s.at(rule, .{"nokind"}));
+ try s.write("/look", s.at("{s}/red.pic\n", .{dir}));
+ try std.testing.expect(has(try s.read(s.at("/pane/{d}/tag", .{try s.focused()})), "red.pic"));
+ try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n");
+ try s.replace("/plumb/rules", s.at(rule, .{"image"}));
try s.write("/look", s.at("{s}/red.pic\n", .{dir}));
try std.testing.expectEqual(try paneOf(&s, "image"), try s.focused());
try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n");
diff --git a/test/behaviour/plumber.zig b/test/behaviour/plumber.zig
index f87c0449..4726714b 100644
--- a/test/behaviour/plumber.zig
+++ b/test/behaviour/plumber.zig
@@ -14,7 +14,7 @@ test "plumber: plumb/rules reads the rules in force, a write adds rule sets, > r
try std.testing.expect(has(try s.read("/plumb/rules"), "type\tis\ttext\ndata\tmatches\t'https?://.*'\nplumb\tto\tweb\n\n"));
// Added, the last rule set at the close.
try s.write("/plumb/rules", "type is text\ndata matches 'gopher://.*'\nplumb to web\n");
- try std.testing.expect(std.mem.endsWith(u8, try s.read("/plumb/rules"), "plumb\tto\tedit\n\ntype\tis\ttext\ndata\tmatches\t'gopher://.*'\nplumb\tto\tweb\n\n"));
+ try std.testing.expect(std.mem.endsWith(u8, try s.read("/plumb/rules"), "attr\tadd\taction=showdiff\nplumb\tto\tedit\n\ntype\tis\ttext\ndata\tmatches\t'gopher://.*'\nplumb\tto\tweb\n\n"));
// A rule set refused at the write it is whole in, said with its line;
// the rules stay as they were.
const before = try s.read("/plumb/rules");
@@ -63,3 +63,44 @@ test "plumber: plumb/send takes a message in its six lines and data, written in
try s.write("/plumb/send", s.at("test\n\n{s}\ntext\n\n{d}\nplumbed.txt:2", .{ dir, "plumbed.txt:2".len }));
try std.testing.expect(has(try s.read("/index"), "/plumbed.txt"));
}
+
+test "plumber: a port's file is read by programs: each open made before a send gets its own copy, one made after none, and a client rule's message waits for the port's first reader" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "seen.txt", .data = "one\ntwo\n" });
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ // A file for each port the rules name, read-only, as plumber(4)'s.
+ try std.testing.expectEqual(@as(u16, 0o444), (try s.stat("/plumb/web")).mode & 0o777);
+ try std.testing.expectEqual(@as(u16, 0o444), (try s.stat("/plumb/edit")).mode & 0o777);
+ // Two readers of edit, then a look: each reads the look's message,
+ // and the editor opens the file all the same.
+ const a = try s.hold("/plumb/edit", 0);
+ const b = try s.hold("/plumb/edit", 0);
+ try s.write("/look", s.at("{s}/seen.txt\n", .{dir}));
+ const want = s.at("pardes\nedit\n{s}\ntext\n\n{d}\n{s}/seen.txt", .{ "/", dir.len + "/seen.txt".len, dir });
+ try std.testing.expectEqualStrings(want, try s.readFid(a));
+ try std.testing.expectEqualStrings(want, try s.readFid(b));
+ try std.testing.expect(has(try s.read("/index"), "/seen.txt"));
+ // An open made after the send has nothing: its read waits.
+ const late = try s.hold("/plumb/edit", 0);
+ try std.testing.expectError(error.Refused, s.readFid(late));
+ s.clunk(late);
+ s.clunk(a);
+ s.clunk(b);
+ // A port only a program reads, its rule a client: the message waits
+ // for the port's first reader, the program started read nothing.
+ try s.write("/plumb/rules", "type is text\ndata matches 'letter-[0-9]+'\nplumb to seemail\nplumb client true\n\n");
+ try s.write("/plumb/send", "test\n\n/\ntext\n\n9\nletter-42");
+ const first = try s.hold("/plumb/seemail", 0);
+ try std.testing.expectEqualStrings("test\nseemail\n/\ntext\n\n9\nletter-42", try s.readFid(first));
+ try std.testing.expectError(error.Refused, s.readFid(first));
+ // Once read, a port's message goes to its readers, no program started.
+ try s.write("/plumb/send", "test\n\n/\ntext\n\n9\nletter-43");
+ try std.testing.expectEqualStrings("test\nseemail\n/\ntext\n\n9\nletter-43", try s.readFid(first));
+ s.clunk(first);
+ // Not to be written.
+ try std.testing.expectError(error.Refused, s.hold("/plumb/seemail", 1));
+}
diff --git a/test/monkey9p.zig b/test/monkey9p.zig
index c9dd5083..a4bf92a3 100644
--- a/test/monkey9p.zig
+++ b/test/monkey9p.zig
@@ -92,7 +92,7 @@ const builtins = [_][]const u8{
"Theme", "Get", "Put", "Undo", "Redo", "Zerox", "Snarf", "Paste", "Cut", "Sort", "Font", "Local",
};
const pane_files = [_][]const u8{ "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", "sel", "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", "pty", "pty/run", "pty/data", "pty/ctl", "pty/status" };
-const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "" };
+const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "plumb", "plumb/rules", "plumb/send", "plumb/edit", "plumb/web", "plumb/seemail", "" };
const long_x: [300]u8 = @splat('x');
const col_files = [_][]const u8{ "tag", "ctl", "exec" };
const writable = [_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "body", "data", "xdata", "addr", "dot", "limit", "name", "sel", "dirty", "mark", "scroll", "errors", "event", "pty/run", "pty/data", "pty/ctl", "log" };
@@ -199,7 +199,7 @@ fn pickCol(r: Random, a: Allocator) []const u8 {
fn randomPath(r: Random, a: Allocator, write: bool) []const u8 {
const x = chance(r);
if (x < 0.6) return print(a, "/pane/{s}/{s}", .{ pickSerial(r, a), if (write) pick(r, &writable) else pick(r, &pane_files) });
- if (x < 0.75) return cat(a, &.{ "/", if (write) pick(r, &[_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "log" }) else pick(r, &root_files) });
+ if (x < 0.75) return cat(a, &.{ "/", if (write) pick(r, &[_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "log", "plumb/rules", "plumb/send", "plumb/edit" }) else pick(r, &root_files) });
if (x < 0.85) return print(a, "/col/{s}/{s}", .{ pickCol(r, a), pick(r, &col_files) });
if (x < 0.93) return print(a, "/pane/{s}/{s}", .{ pickSerial(r, a), weirdName(r, a) });
var out: []const u8 = "";
@@ -230,6 +230,10 @@ fn dataFor(r: Random, a: Allocator, path: []const u8) []const u8 {
return cat(a, &.{ shellSafe(a, pick(r, &[_][]const u8{ "alpha", "alpha.txt", "alpha.txt:2", "utf8.txt:#3", "nothere", "bad.txt", "crlf.txt:1", "gamma", "beta" })), "\n" });
if (is(b, &.{ "dirty", "mark", "scroll" })) return pick(r, &[_][]const u8{ "0", "1", "0\n", "1\n", "2\n", "", "yes\n" });
if (std.mem.eql(u8, b, "log")) return pick(r, &[_][]const u8{ "follow\n", "follow new\n", "follow", "nope\n" });
+ // The plumber's: rule sets that start no program, half ones, bad ones;
+ // messages whole, short of their data, malformed.
+ if (std.mem.eql(u8, b, "rules")) return pick(r, &[_][]const u8{ "type is text\ndata matches 'letter-[0-9]+'\nplumb to seemail\n\n", "include basic\n", "type is text\narg isfile $data\nplumb to edit\n", "data isnt x\n", "plumb to seemail\n\n", "x='[a-z]+'\n", "" });
+ if (std.mem.eql(u8, b, "send")) return pick(r, &[_][]const u8{ "monkey\n\n/\ntext\n\n9\nletter-42", "monkey\n\n/\ntext\n\n9\nalpha.txt", "monkey\nseemail\n/\ntext\n\n1\nx", "monkey\n\n/\ntext\n\n20\nshort", "a\nb\n" });
return payload(r, a, 200);
}
@@ -704,7 +708,8 @@ fn hostPath(path: []const u8) bool {
fn isHeld(path: []const u8) bool {
return std.mem.eql(u8, std.mem.trimEnd(u8, path, "/"), "/log") or std.mem.endsWith(u8, path, "/event") or
- std.mem.endsWith(u8, path, "/pty/data") or std.mem.endsWith(u8, path, "/pty/run");
+ std.mem.endsWith(u8, path, "/pty/data") or std.mem.endsWith(u8, path, "/pty/run") or
+ (std.mem.startsWith(u8, path, "/plumb/") and !std.mem.endsWith(u8, path, "/rules") and !std.mem.endsWith(u8, path, "/send"));
}
/// An open the rule names: pane/new's (it makes a pane) and a truncation
@@ -931,7 +936,7 @@ fn exec(c: *Ctx, a: Allocator, res: *Res, op: Op) anyerror!void {
}
const closed = try clunk(c, a, res, &f, o.path);
// What the open holds when it closes: the last line written, if unended.
- if (opened and o.mode & 3 != 0 and sent.len > 0 and !std.mem.endsWith(u8, sent, "\n"))
+ if (opened and o.mode & 3 != 0 and restAtClose(o.path, sent))
res.add(a, .{ .kind = .close_runs, .path = o.path, .ok = closed });
},
.read => |o| {
@@ -1014,7 +1019,7 @@ fn exec(c: *Ctx, a: Allocator, res: *Res, op: Op) anyerror!void {
}
var h = c.handles.orderedRemove(i % c.handles.items.len);
const ok = try clunk(c, a, res, &h.fid, h.path);
- if (h.fid.mode & 3 != 0 and h.last.len > 0 and !std.mem.endsWith(u8, h.last, "\n"))
+ if (h.fid.mode & 3 != 0 and restAtClose(h.path, h.last))
res.add(a, .{ .kind = .close_runs, .path = h.path, .ok = ok });
},
.restore => |data| {
@@ -1322,7 +1327,15 @@ fn oneFailureRule(c: *Ctx, a: Allocator, res: *Res) ?[]const u8 {
return null;
}
-const line_files = [_][]const u8{ "look", "exec", "tagexec", "ctl" };
+const line_files = [_][]const u8{ "look", "exec", "tagexec", "ctl", "rules" };
+
+/// What an open that wrote `sent` last leaves to its close: a line with no
+/// newline yet, or of plumb/rules' text a rule set with no blank line
+/// after it (plumber(4)'s, read at the close).
+fn restAtClose(path: []const u8, sent: []const u8) bool {
+ if (sent.len == 0) return false;
+ return !std.mem.endsWith(u8, sent, if (std.mem.endsWith(u8, path, "plumb/rules")) "\n\n" else "\n");
+}
/// docs/typ/reference.typ: 'A write of command lines -- to look, exec,
/// tagexec, a ctl of the root, a pane or a column, or a column's exec --
diff --git a/test/replicas.zig b/test/replicas.zig
index c100a8c1..c41a6cac 100644
--- a/test/replicas.zig
+++ b/test/replicas.zig
@@ -544,6 +544,119 @@ fn diffCase(dir: []const u8) !bool {
return ok;
}
+/// Looks the plumber takes (src/plumbing), each as the authority's input
+/// and as a replica's own: a URL (its web port: no disk asked, so a
+/// replica's own look speculates whole and is confirmed with no rollback),
+/// and a commit hash in a git work tree (Diff's `git show`, its `.git`
+/// asked at a read the unit carries). The replica's digest is the
+/// authority's either way.
+fn plumbCase(dir: []const u8) !bool {
+ var host: Host = .{};
+ defer {
+ for (host.echo.items) |e| gpa.free(e.bytes);
+ host.echo.deinit(gpa);
+ }
+ var gbuf: [256]u8 = undefined;
+ try std.Io.Dir.createDirAbsolute(std.Io.Threaded.global_single_threaded.io(), try std.fmt.bufPrint(&gbuf, "{s}/.git", .{dir}), .default_dir);
+ var nbuf: [256]u8 = undefined;
+ const notes = try std.fmt.bufPrintSentinel(&nbuf, "{s}/notes.txt", .{dir}, 0);
+ try writeText(notes, "fixed in 3f2a9c1, see https://ziglang.org\n");
+ var obuf: [300]u8 = undefined;
+ const open = try std.fmt.bufPrint(&obuf, "Look {s}", .{notes});
+ var ok = true;
+ for ([_][]const u8{ "Look https://ziglang.org", "Look 3f2a9c1" }) |look| for ([_]bool{ false, true }) |own| {
+ const a = try replica.Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty }, host.host(), true);
+ defer a.deinit();
+ const first = try gpa.dupe(u8, try a.unit(0, 0));
+ defer gpa.free(first);
+ const rp = try replica.Replica.init(gpa, 1, first, true);
+ defer rp.deinit();
+ rp.confirm(try a.local(.{ .command = open }));
+ rp.settle();
+ const before = rp.stats;
+ const url = std.mem.indexOf(u8, look, "://") != null;
+ if (own) {
+ const msg = try gpa.dupe(u8, try rp.input(.{ .command = look }));
+ defer gpa.free(msg);
+ if (url and (rp.stats.stopped != before.stopped or rp.stats.speculated == before.speculated)) {
+ std.debug.print("plumb: {s}: the replica's own look did not speculate whole ({d} stopped, {d} applied)\n", .{ look, rp.stats.stopped, rp.stats.speculated });
+ ok = false;
+ }
+ if (try a.pending()) |batch| rp.confirm(batch);
+ rp.confirm(try a.take(msg));
+ } else rp.confirm(try a.local(.{ .command = look }));
+ rp.settle();
+ const who = if (own) "its own look" else "the authority's look";
+ if (!url) {
+ const paged = for (a.core.panes) |slot| {
+ if ((slot orelse continue).command_pages) break true;
+ } else false;
+ if (!paged) {
+ std.debug.print("plumb: {s}: {s}: no git show paged\n", .{ look, who });
+ ok = false;
+ }
+ }
+ if (rp.diverged) |why| {
+ std.debug.print("plumb: {s}: {s}: the replica diverged: {s}\n", .{ look, who, why });
+ ok = false;
+ } else if (!rp.quiet() or rp.digest() != replica.digestOf(a)) {
+ std.debug.print("plumb: {s}: {s}: the replica's digest is not the authority's\n", .{ look, who });
+ ok = false;
+ }
+ if (rp.stats.mispredicted + rp.stats.foreign != 0) {
+ std.debug.print("plumb: {s}: {s}: {d} rollbacks\n", .{ look, who, rp.stats.mispredicted + rp.stats.foreign });
+ ok = false;
+ }
+ };
+ // A rule to a port a program reads (plumb/seemail), of a URL, which a
+ // look reads no disk for: whether the port is read is the authority's
+ // 9P state, asked through the tape. A replica's own look waits for the
+ // first answer, then guesses the last; the second look speculates whole
+ // and is confirmed with no rollback.
+ {
+ // The rule from the session's plumbing file, read at boot.
+ var pbuf: [256]u8 = undefined;
+ try writeText(try std.fmt.bufPrintSentinel(&pbuf, "{s}/plumbing", .{dir}, 0), "type is text\ndata matches 'https://letters/.*'\nplumb to seemail\n\ninclude basic\n");
+ const a = try replica.Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty, .config_dir = dir }, host.host(), true);
+ defer a.deinit();
+ const first = try gpa.dupe(u8, try a.unit(0, 0));
+ defer gpa.free(first);
+ const rp = try replica.Replica.init(gpa, 1, first, true);
+ defer rp.deinit();
+ rp.confirm(try a.local(.{ .command = open }));
+ rp.settle();
+ if (pardes.Plumb.portIndex(rp.core(), "seemail") == null) {
+ std.debug.print("plumb: a look to a port: the plumbing file's port is not the replica's\n", .{});
+ ok = false;
+ }
+ for (0..2) |n| {
+ const before = rp.stats;
+ const msg = try gpa.dupe(u8, try rp.input(.{ .command = "Look https://letters/42" }));
+ defer gpa.free(msg);
+ if (n == 1 and (rp.stats.stopped != before.stopped or rp.stats.speculated == before.speculated)) {
+ std.debug.print("plumb: a look to a port: the replica's second look did not speculate whole ({d} stopped, {d} applied)\n", .{ rp.stats.stopped - before.stopped, rp.stats.speculated - before.speculated });
+ ok = false;
+ }
+ if (try a.pending()) |batch| rp.confirm(batch);
+ rp.confirm(try a.take(msg));
+ rp.settle();
+ }
+ if (rp.diverged) |why| {
+ std.debug.print("plumb: a look to a port: the replica diverged: {s}\n", .{why});
+ ok = false;
+ } else if (!rp.quiet() or rp.digest() != replica.digestOf(a)) {
+ std.debug.print("plumb: a look to a port: the replica's digest is not the authority's\n", .{});
+ ok = false;
+ }
+ if (rp.stats.mispredicted + rp.stats.foreign != 0) {
+ std.debug.print("plumb: a look to a port: {d} rollbacks\n", .{rp.stats.mispredicted + rp.stats.foreign});
+ ok = false;
+ }
+ }
+ std.debug.print("plumb: a URL, a commit hash and a look to a port read by programs, by the authority and as a replica's own input: {s}\n", .{if (ok) "the same session" else "FAILED"});
+ return ok;
+}
+
fn push(q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, m: Msg) !void {
var mm = m;
mm.n = nn.*;
@@ -1512,6 +1625,7 @@ pub fn main(init: std.process.Init) !void {
return;
}
var failed = !try diffCase(dir);
+ if (!try plumbCase(dir)) failed = true;
std.debug.print("{d} seeds x {d} inputs from each of {d} clients, {s}, a TUI then {d} MB of yes in the shell; costs in us (frame {d})\n", .{ seeds, steps, clients, if (shared) "sharing one view" else "a view each", flood_mb, frame_us });
std.debug.print(" rtt inputs at-once stopped view units fast rollback(mis+foreign) rate rebase spec p50/p99 copy p50/p99 (KB) rollback p50/p99 diverged unequal\n", .{});
var term_lines: std.ArrayList(u8) = .empty;