diff options
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/core/replica.zig b/core/replica.zig index 342913ba..bc5eafd1 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -6159,3 +6159,121 @@ test "a far client's click into a shell whose queue is backed up: the session's try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); try agrees(a, r); } + +// A `/` in a file groups its rows under the declarations they are in once +// a parse of the text exists (Query.ownerDecls): the pane's context, a +// worker's answer, or the locations cache's (a Diff's). Both were kept out +// of the dump as made again from the text, so a client joined since had +// neither, and its replay of the session's next row asked a parse the +// session never asked: the context is dumped now, the cache's no longer read. +test "a client joined from a dump makes a `/` query's rows as the session does, though only the session has the owner's declarations from a worker's parse" { + // A session's host whose parses and searches run on a worker (a tty's, + // Stream.start), their batches posted to an inbox; `land` hands them to + // the core as the session's inputs, as its loop does. + const WorkerSearch = struct { + const Inbox = @import("dev").host_io.Inbox; + var q: Inbox = undefined; + var core: ?*Pardes = null; + fn search(_: ?*anyopaque, gen: u32) void { + pardes.Stream.start(std.testing.io, std.testing.allocator, core.?, gen, &q, Inbox.pushSearch); + } + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .search = search }; + fn init(a: *Authority) void { + q = .{ .gpa = std.testing.allocator, .lsp_allocator = std.testing.allocator, .io = std.testing.io }; + core = a.core; + } + // The batches posted until the parse asked has answered (a second's + // wait at most), each a unit the replica takes. + fn land(a: *Authority, r: ?*Replica) !void { + var waited: u32 = 0; + while (a.core.stream.parse != null) : (waited += 1) { + if (waited == 1000) return error.Timeout; + var batch: Inbox.Batch = .{}; + q.takeInto(&batch); + for (batch.slice()) |m| switch (m) { + .search => |b| { + const u = try a.local(.{ .search = b }); + if (r) |c| { + c.confirm(u); + c.settle(); + } + b.deinit(std.testing.allocator); + }, + else => m.deinit(std.testing.allocator, std.testing.allocator), + }; + if (a.core.stream.parse != null) { + const rest: std.c.timespec = .{ .sec = 0, .nsec = std.time.ns_per_ms }; + _ = std.c.nanosleep(&rest, null); + } + } + } + }; + if (comptime !pardes.syntax.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, .{ .vtable = &WorkerSearch.vtable }, false); + defer a.deinit(); + WorkerSearch.init(a); + defer WorkerSearch.q.deinit(); + // The session's own `/`: its parse asked of the worker, its answer an + // input (the rows grouped once it lands). + _ = try a.local(.{ .command = "Search /pub" }); + try WorkerSearch.land(a, null); + // A client joined now, and a key typed into the query: both its rows. + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + try std.testing.expectEqual(a.core.panes[0].?.file.?.context.declarations.len, r.core().panes[0].?.file.?.context.declarations.len); + const u = try gpa.dupe(u8, try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + defer gpa.free(u); + r.confirm(u); + r.settle(); + try agrees(a, r); +} + +// A text's places show as ticks on its rail (Places.addTicks), made again +// only when `gen` moves (buildTicks), by a frame or by a step's rail click +// (tickHit), whichever asks first; a dump keeps neither. A frame's ticks +// that a click read after the places changed under them would land it +// elsewhere on a core that drew than on one that did not. +test "a client joined from a dump lands a click on a rail's tick as the session does, the session's ticks drawn before the places changed" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + _ = try a.local(.{ .command = "Search /pub" }); + const owner: usize = 0; + try std.testing.expect(a.core.panes[owner].?.places.hits.items.len > 1); + // The session draws: its ticks, of `/pub`'s places. + { + const was = Stepping.begin(&a.t, null); + defer was.end(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = try a.core.render(arena.allocator()); + } + const ticks_gen = a.core.panes[owner].?.places.ticks_gen; + try std.testing.expectEqual(a.core.panes[owner].?.places.gen, ticks_gen); + const r = try joinedFresh(a); + defer r.deinit(); + // Its places changed (`/publ`), then a click on each row of the rail. + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'l', .text = "l" } }))); + const rect = r.core().view().rects[owner]; + const pane = r.core().panes[owner].?; + for (rect.y + pane.tag_rows..rect.y + rect.h) |row| for ([_]pardes.Mouse.Kind{ .press, .release }) |kind| { + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .mouse = .{ .button = .left, .kind = kind, .col = rect.x, .row = @intCast(row) } }))); + }; + try std.testing.expect(a.core.panes[owner].?.places.ticks_gen == ticks_gen); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + // (The key may be mispredicted: a step's places are a frame's time's + // worth, `tape.over`, the session's verdict, and its drawn caches let + // it find more in that time than the client's speculation did.) + try agrees(a, r); + try std.testing.expectEqual(a.core.panes[owner].?.places.current, r.core().panes[owner].?.places.current); +} |
