summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig104
1 files changed, 104 insertions, 0 deletions
diff --git a/core/replica.zig b/core/replica.zig
index 5b4afbb6..42aae524 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -5975,3 +5975,107 @@ test "a far client's drag in a kind's pane it grabbed, the pane since too short
try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted);
try agrees(a, r);
}
+
+// A draw's token is its slot's count of draws, which the rollback copy
+// holds with the slot: a frame drawn under a speculation asks token T, the
+// rollback takes the count back, and the next frame's draw (of the state
+// the replay came to) is given T again. Its answer and the discarded one's
+// both come to job_done as T, in the order the workers finish.
+test "a far client's frame drawn under a speculation that rolls back asks a picture whose token the next draw is never given again: the kind keeps the picture of the state it shows" {
+ // A plain kind whose picture is a draw of its count (api.draw from its
+ // paint, when the picture asked for is of another count), its answer kept
+ // for the token it asked last, once: a kind with one draw outstanding.
+ // What it shows is this process's (a kind's own memory, never its block).
+ const Drawn = struct {
+ const pa = pardes.Kind.c;
+ var want: u32 = 0;
+ var asked: ?u32 = null;
+ var shown: ?u32 = null;
+ fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque {
+ const n: *u32 = @ptrCast(@alignCast(pardes.Kind.api.alloc.?(4)));
+ n.* = 0;
+ return n;
+ }
+ fn paint(st: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {
+ const n: *const u32 = @ptrCast(@alignCast(st.?));
+ if (asked == n.*) return;
+ asked = n.*;
+ want = pardes.Kind.api.draw.?(n, 4);
+ }
+ fn press(st: ?*anyopaque, _: [*c]const pa.pa_key) callconv(.c) c_int {
+ const n: *u32 = @ptrCast(@alignCast(st.?));
+ n.* = n.* * 2 + 1;
+ return 1;
+ }
+ fn jobRun(in: [*c]const u8, n: u32, out: [*c]pa.pa_out) callconv(.c) u32 {
+ const o: [*]u8 = @ptrCast(out.*.alloc.?(out, n) orelse return 0);
+ @memcpy(o[0..n], in[0..n]);
+ return n;
+ }
+ fn jobDone(_: ?*anyopaque, token: u32, out: [*c]const u8, n: u32) callconv(.c) void {
+ if (token != want or n != 4) return;
+ want = 0;
+ shown = std.mem.readInt(u32, out[0..4], .little);
+ }
+ const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Drawn", .opens = 1, .doc = "a picture of a count" })};
+ 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 = "drawn".* ++ @as([11]u8, @splat(0)), .version = 1, .exts = "", .flags = pa.PA_PLAIN, .words = &words, .nwords = 1 }),
+ .open = open,
+ .paint = paint,
+ .key = press,
+ .job_run = jobRun,
+ .job_done = jobDone,
+ });
+ };
+
+ // A frontend's workers for draws, run when the test says, each landing
+ // when it says (host_io.KindJob: run on a worker, landed on the loop).
+ const HeldDraws = struct {
+ var held: [4]*@import("dev").host_io.KindJob = undefined;
+ var n: usize = 0;
+ fn go(_: ?*anyopaque, j: *@import("dev").host_io.KindJob) bool {
+ j.run();
+ held[n] = j;
+ n += 1;
+ return true;
+ }
+ };
+ 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 drawn = pardes.Kind.loadedOf(&Drawn.ops, "/x/drawn.so", @splat(9), &words);
+ _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &drawn } });
+ _ = try a.local(.{ .command = "Drawn" });
+ const kid = a.core.view().active;
+ const r = try joinedFresh(a);
+ defer r.deinit();
+ try runsHere(r, kid, &Drawn.ops);
+ Drawn.want = 0;
+ Drawn.asked = null;
+ Drawn.shown = null;
+ HeldDraws.n = 0;
+ const was = pardes.Kind.drawer;
+ pardes.Kind.drawer = .{ .ctx = null, .go = HeldDraws.go };
+ defer pardes.Kind.drawer = was;
+ const none: pardes.views.Present = .{ .ctx = null, .f = null };
+ // Its own key: 0 -> 1, drawn (a picture of 1 asked).
+ const msg = try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'x', .text = "x" } }));
+ defer gpa.free(msg);
+ _ = r.draw(none);
+ try std.testing.expectEqual(@as(?u32, 1), Drawn.asked);
+ // The session's own key first: rolled back, its own applied again on
+ // top (0 -> 1 -> 3), drawn (a picture of 3 asked).
+ r.confirm(try a.own(.{ .key = .{ .cp = 'x', .text = "x" } }));
+ r.settle();
+ _ = r.draw(none);
+ try std.testing.expectEqual(@as(?u32, 3), Drawn.asked);
+ try std.testing.expectEqual(@as(usize, 2), HeldDraws.n);
+ // The discarded picture's worker done first, then the other's; a frame.
+ for (HeldDraws.held[0..2]) |j| pardes.Kind.landDraw(r.core(), j);
+ _ = r.draw(none);
+ try std.testing.expectEqual(@as(?u32, 3), Drawn.shown);
+ r.confirm(try a.take(msg));
+ r.settle();
+ try agrees(a, r);
+}