summaryrefslogtreecommitdiff
path: root/test/behaviour/plumber.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/behaviour/plumber.zig')
-rw-r--r--test/behaviour/plumber.zig41
1 files changed, 41 insertions, 0 deletions
diff --git a/test/behaviour/plumber.zig b/test/behaviour/plumber.zig
index 28451222..4144901b 100644
--- a/test/behaviour/plumber.zig
+++ b/test/behaviour/plumber.zig
@@ -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));
+}