summaryrefslogtreecommitdiff
path: root/test/liar.zig
blob: e7bc2763207b5f0b7e87590d0f63384031f55a6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! 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);
pub const panic = addon.panic;
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);
}