diff --git a/test/remote_child_endpoint.zig b/test/remote_child_endpoint.zig index 379136e..47d771a 100644 --- a/test/remote_child_endpoint.zig +++ b/test/remote_child_endpoint.zig @@ -18,6 +18,16 @@ const cbor = @import("cbor"); const framing = @import("framing"); const protocol = @import("protocol"); +var trace_file: ?std.fs.File = null; +var trace_buf: [4096]u8 = undefined; +var trace_file_writer: std.fs.File.Writer = undefined; + +fn trace_handler(buf: tp.message.c_buffer_type) callconv(.c) void { + if (trace_file == null) return; + cbor.toJsonWriter(buf.base[0..buf.len], &trace_file_writer.interface, .{}) catch return; + trace_file_writer.interface.writeByte('\n') catch return; +} + // --------------------------------------------------------------------------- // EchoActor (wire ID 2) // --------------------------------------------------------------------------- @@ -150,8 +160,6 @@ const StdioEndpoint = struct { } fn init(args: Args) !void { - tp.trace_to_json_file("remote_child_endpoint_trace.json"); - tp.env.get().enable_all_channels(); const fd_stdin = try tp.file_descriptor.init("stdin", 0); const self = try args.allocator.create(@This()); self.* = .{ @@ -282,6 +290,17 @@ pub fn main() !void { defer _ = gpa.deinit(); const allocator = gpa.allocator(); + var initial_env: ?tp.env = null; + if (std.posix.getenv("TRACE") != null) { + const f = try std.fs.cwd().createFile("remote_child_endpoint_trace.json", .{}); + trace_file = f; + trace_file_writer = f.writer(&trace_buf); + var e = tp.env.init(); + e.on_trace(&trace_handler); + e.enable_all_channels(); + initial_env = e; + } + var ctx = try tp.context.init(allocator); defer ctx.deinit(); @@ -299,10 +318,17 @@ pub fn main() !void { StdioEndpoint.start, "stdio_endpoint", &exit_handler, - null, + if (initial_env) |*e| e else null, ); ctx.run(); + if (initial_env) |e| { + trace_file_writer.interface.flush() catch {}; + trace_file.?.close(); + trace_file = null; + e.deinit(); + } + std.process.exit(if (exit_ok) 0 else 1); } diff --git a/test/remote_endpoint_id_test.zig b/test/remote_endpoint_id_test.zig index 5a6dd5a..267dee6 100644 --- a/test/remote_endpoint_id_test.zig +++ b/test/remote_endpoint_id_test.zig @@ -11,9 +11,20 @@ /// 5. TestActor receives ["done"] → exits "success". const std = @import("std"); const thespian = @import("thespian"); +const cbor = @import("cbor"); const endpoint = @import("endpoint"); const build_options = @import("build_options"); +var trace_file: ?std.fs.File = null; +var trace_buf: [4096]u8 = undefined; +var trace_file_writer: std.fs.File.Writer = undefined; + +fn trace_handler(buf: thespian.message.c_buffer_type) callconv(.c) void { + if (trace_file == null) return; + cbor.toJsonWriter(buf.base[0..buf.len], &trace_file_writer.interface, .{}) catch return; + trace_file_writer.interface.writeByte('\n') catch return; +} + const Allocator = std.mem.Allocator; const result = thespian.result; const unexpected = thespian.unexpected; @@ -34,8 +45,6 @@ const TestActor = struct { } fn init(args: Args) !void { - thespian.trace_to_json_file("remote_endpoint_id_trace.json"); - thespian.env.get().enable_all_channels(); thespian.env.get().proc_set("test_receiver", thespian.self_pid().ref()); const argv = try args.allocator.dupe(u8, message.fmt(.{build_options.remote_child_endpoint_path}).buf); @@ -94,6 +103,24 @@ const TestActor = struct { test "remote: inbound proxy table, from-substitution, outbound ID table, and send-by-ID routing" { const allocator = std.testing.allocator; + + var initial_env: ?thespian.env = null; + if (std.posix.getenv("TRACE") != null) { + const f = try std.fs.cwd().createFile("remote_endpoint_id_trace.json", .{}); + trace_file = f; + trace_file_writer = f.writer(&trace_buf); + var e = thespian.env.init(); + e.on_trace(&trace_handler); + e.enable_all_channels(); + initial_env = e; + } + defer if (initial_env) |e| { + trace_file_writer.interface.flush() catch {}; + trace_file.?.close(); + trace_file = null; + e.deinit(); + }; + var ctx = try thespian.context.init(allocator); defer ctx.deinit(); @@ -109,7 +136,7 @@ test "remote: inbound proxy table, from-substitution, outbound ID table, and sen TestActor.start, "test_actor", &exit_handler, - null, + if (initial_env) |*e| e else null, ); ctx.run();