summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/replica.zig b/src/replica.zig
index 98426949..2b7590ed 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -4860,3 +4860,72 @@ test "a client joined from a dump tells a kind each view's facts as the session
joiner.confirm(try a.local(.tick));
try agrees(a, joiner);
}
+
+/// A plain kind whose key asks a job and whose job's answer moves its
+/// block: run by a client as by its session.
+const Asker = struct {
+ const pa = pardes.Kind.c;
+ fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque {
+ return pardes.Kind.api.alloc.?(4);
+ }
+ fn paint(_: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {}
+ fn press(_: ?*anyopaque, _: [*c]const pa.pa_key) callconv(.c) c_int {
+ _ = pardes.Kind.api.job.?(null, 0);
+ return 1;
+ }
+ fn run(_: [*c]const u8, _: u32, out: [*c]pa.pa_out) callconv(.c) u32 {
+ const b: [*]u8 = @ptrCast(out.*.alloc.?(out, 1) orelse return 0);
+ b[0] = 1;
+ return 1;
+ }
+ fn done(st: ?*anyopaque, _: u32, _: [*c]const u8, _: u32) callconv(.c) void {
+ const n: *u32 = @ptrCast(@alignCast(st.?));
+ n.* += 1;
+ }
+ const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Asker", .opens = 1, .doc = "a key a job" })};
+ const ops = std.mem.zeroInit(pardes.Kind.Ops, .{
+ .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "asker".* ++ @as([11]u8, @splat(0)), .version = 1, .exts = "", .flags = pa.PA_PLAIN, .words = &words, .nwords = 1 }),
+ .open = open,
+ .paint = paint,
+ .key = press,
+ .job_run = run,
+ .job_done = done,
+ });
+};
+
+/// A host whose workers are all busy (tui.zig's `job` with its 16 tasks
+/// out): a kind's job runs at once, inside the host's call.
+const BusyHost = struct {
+ var core: ?*Pardes = null;
+ var tasks: @import("selection_pipe.zig").Tasks = .{ .len = @import("selection_pipe.zig").Tasks.capacity };
+ fn job(_: ?*anyopaque, pane: u8, serial: u32, token: u32) void {
+ pardes.host_io.KindJob.start(std.testing.io, core.?, &tasks, pane, serial, token, null, never);
+ }
+ fn never(_: ?*anyopaque, j: *pardes.host_io.KindJob) void {
+ j.drop();
+ }
+ const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .job = job };
+};
+
+test "a kind's job answered at once inside its host's call (no worker free) is taken by a client as the session took it" {
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &BusyHost.vtable }, false);
+ defer a.deinit();
+ BusyHost.core = a.core;
+ const r = try Replica.init(gpa, 1, try a.unit(0, 0), false);
+ defer r.deinit();
+ var words: [64]pardes.Kind.LoadedWord = undefined;
+ const asker = pardes.Kind.loadedOf(&Asker.ops, "/x/asker.so", @splat(7), &words);
+ r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &asker } }));
+ // The client has its code (as a library of its own found by its scan).
+ r.core().kinds.ops[r.core().kinds.byName("asker").?] = &Asker.ops;
+ r.confirm(try a.local(.{ .command = "Asker" }));
+ const id = a.core.view().active;
+ // Its key asks a job, answered inside the key's unit: an input the
+ // host made inside its call, on the tape (Effect.perform with no host
+ // `job` lands it on the core's side, on no tape: a host must have one).
+ r.confirm(try a.local(.{ .key = .{ .cp = 'x', .text = "x" } }));
+ try agrees(a, r);
+ 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.?);
+}