lib/trace/src/store/reader/test.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const std = @import("std");
 2 const sys = @import("sys");
 3 const subject = @import("root.zig");
 4 const event = @import("../../root.zig").event;
 5 const format = @import("../format/root.zig");
 6 const writer = @import("../writer/root.zig");
 7 
 8 const Limits = subject.Limits;
 9 const Reader = subject.Reader;
10 const Storage = subject.Storage;
11 
12 test "trace reader state does not embed caller storage" {
13     try std.testing.expect(@sizeOf(Reader) < format.max_manifest_bytes);
14 }
15 
16 test "trace reader remains allocator-independent through SQL verification" {
17     comptime {
18         @stardustClaim(
19             @import("alloc_phase").capacity.witness(@import("./root.zig").TraceReaderStorage, "trace_trace_reader_sealed"),
20             null,
21             null,
22             null,
23             null,
24             null,
25             null,
26         );
27     }
28 
29     const allocator = std.testing.allocator;
30     var tmp = std.testing.tmpDir(.{});
31     defer tmp.cleanup();
32     const root = try tmp.parent_dir.realPathFileAlloc(sys.fs.debugIo(), tmp.sub_path[0..], allocator);
33     defer allocator.free(root);
34     const path = try sys.path.join(allocator, &.{ root, "sealed-reader.trace" });
35     defer allocator.free(path);
36     const trace_limits = format.Limits{ .max_event_bytes = 512, .max_chunk_bytes = 512 };
37     const manifest = format.Manifest{ .build_id = "sealed-reader" };
38     var writer_storage = try writer.Storage.init(
39         allocator,
40         writer.Limits.fromOpen(path, manifest, trace_limits),
41     );
42     defer writer_storage.deinit(allocator);
43     writer_storage.activate();
44     var trace_writer: writer.Writer = .{};
45     try trace_writer.open(&writer_storage, path, manifest, trace_limits);
46     defer trace_writer.deinit();
47     for (0..40) |index| {
48         try trace_writer.append(event.Event.user(
49             .{ .thread_id = 1, .seq = index + 1 },
50             "sealed",
51             "production-shaped-payload-0123456789abcdef",
52         ));
53     }
54     try trace_writer.finish();
55 
56     var failing = std.testing.FailingAllocator.init(allocator, .{});
57     var reader_storage = try Storage.init(failing.allocator(), Limits.fromOpen(path, trace_limits));
58     defer reader_storage.deinit(failing.allocator());
59     reader_storage.activate();
60     failing.fail_index = failing.alloc_index;
61     failing.resize_fail_index = failing.resize_index;
62     var trace_reader: Reader = .{};
63     try trace_reader.open(&reader_storage, path);
64     defer trace_reader.deinit();
65     const result = try trace_reader.verify();
66     try std.testing.expectEqual(@as(u64, 40), result.event_count);
67     try std.testing.expect(result.chunk_count > 1);
68     try std.testing.expect(!failing.has_induced_failure);
69 }