diff options
Diffstat (limited to 'test/liar.zig')
| -rw-r--r-- | test/liar.zig | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/test/liar.zig b/test/liar.zig new file mode 100644 index 00000000..f564614b --- /dev/null +++ b/test/liar.zig @@ -0,0 +1,45 @@ +//! A test addon that breaks its promise: it says PA_PLAIN, and its tick +//! reads the clock, so a client that runs it comes to another state than +//! the session's. The client is to find that at the tick and join again +//! without it (Kind.zig `Call.end`), never to show a wrong picture. +const std = @import("std"); +const addon = @import("addon"); + +pub const Liar = extern struct { + ticks: u64, + noise: u64, + next: addon.Deadline, + + pub const head: addon.Head = .{ .name = "liar", .version = 1, .plain = true, .words = &.{ + .{ .name = "Liar", .opens = true, .doc = "a test addon whose step reads the clock, breaking PA_PLAIN" }, + } }; + + pub fn tick(s: *Liar, now: u64, _: addon.Flags) u64 { + var ts: std.c.timespec = undefined; + _ = std.c.clock_gettime(.MONOTONIC, &ts); + s.noise +%= @intCast(ts.nsec); + s.ticks += 1; + return s.next.every(now, 100 * std.time.ns_per_ms); + } + + pub fn paint(s: *const Liar, w: addon.Window, _: addon.Paint) void { + var line: [64]u8 = undefined; + w.put(0, 0, std.fmt.bufPrint(&line, "liar: {d} ticks, noise {x}", .{ s.ticks, s.noise }) catch "liar", .{}); + } + + pub const dump = addon.dumpBytes; + pub const load = addon.loadBytes; +}; + +pub const pardes_addon = addon.ops(Liar); +comptime { + addon.exportLib(&pardes_addon); +} + +test "two ticks at the same time come out apart: it reads the clock" { + var a = std.mem.zeroInit(Liar, .{}); + var b = a; + _ = a.tick(1, .{ .focused = false, .framed = false, ._ = 0 }); + _ = b.tick(1, .{ .focused = false, .framed = false, ._ = 0 }); + try std.testing.expect(a.noise != b.noise); +} |
