code wiki / (root) / nx_trace_emit_test.nx

nx_trace_emit_test.nx source

↩ module page · 80 lines · 3189 B

1// nx_trace_emit_test.nx -- span emission shape verification. 2// 3// Writes spans to a memfd-like buffer (fd 3 redirected by caller in 4// real use; here we use the default fd=2 stderr so the qemu run 5// produces visible output the smoke can scrape). The test focuses 6// on API correctness: ids increment, kind validation, disable/enable. 7 8import "nx_syscalls.nx" 9import "nx_runtime.nx" 10import "nx_tier.nx" 11import "nx_trace_emit.nx" 12 13func main() -> nx_int { 14 // ===== Bootstrap config ======================================= 15 let cfg: *TraceCfg = nx_trace_cfg_alloc() 16 if cfg.fd != NX_TRACE_FD_DEFAULT { return 1 } 17 if cfg.enabled != 1 { return 2 } 18 if cfg.next_span_id != 1 { return 3 } 19 20 // ===== Span ID minting is monotonic =========================== 21 let s1: nx_int = nx_trace_mint_span_id(cfg) 22 let s2: nx_int = nx_trace_mint_span_id(cfg) 23 let s3: nx_int = nx_trace_mint_span_id(cfg) 24 if s1 != 1 { return 4 } 25 if s2 != 2 { return 5 } 26 if s3 != 3 { return 6 } 27 if cfg.next_span_id != 4 { return 7 } 28 29 // ===== Span kind validation ================================== 30 if nx_span_kind_is_valid(NX_SPAN_BATCH_OPEN) != 1 { return 10 } 31 if nx_span_kind_is_valid(NX_SPAN_CONTINUITY_CHECK) != 1 { return 11 } 32 if nx_span_kind_is_valid(0 - 1) != 0 { return 12 } 33 if nx_span_kind_is_valid(NX_SPAN_N_KINDS) != 0 { return 13 } 34 35 // ===== fd bounds ============================================== 36 if nx_trace_set_fd(cfg, 0 - 1) != 0 - 1 { return 20 } 37 if nx_trace_set_fd(cfg, NX_TRACE_FD_MAX) != 0 - 1 { return 21 } 38 if nx_trace_set_fd(cfg, 7) != 0 { return 22 } 39 if cfg.fd != 7 { return 23 } 40 41 // ===== Reset fd so we don't crash on subsequent writes ======== 42 nx_trace_set_fd(cfg, NX_TRACE_FD_DEFAULT) 43 44 // ===== Emit a span; rc 0 from simple emit ==================== 45 let r1: nx_int = nx_trace_emit_simple(cfg, 100, 1, 0 - 1, 46 NX_SPAN_BATCH_OPEN, 0, 100) 47 if r1 != 0 { return 30 } 48 49 // ===== Emit with one attr (chosen_outfit_id = 200) =========== 50 let r2: nx_int = nx_trace_emit_one_attr(cfg, 100, 2, 1, 51 NX_SPAN_OUTFIT_PICK, 52 100, 105, 53 0xCAFE, 200) 54 if r2 != 0 { return 31 } 55 56 // ===== Emit with multi-attr ================================= 57 let attrs: *i64 = (sys_mmap(48)) as *i64 58 attrs[0] = 0xAA // key 59 attrs[1] = 100 // value 60 attrs[2] = 0xBB 61 attrs[3] = 200 62 attrs[4] = 0xCC 63 attrs[5] = 300 64 let r3: nx_int = nx_trace_emit_span(cfg, 100, 3, 1, 65 NX_SPAN_CONTINUITY_CHECK, 66 105, 110, attrs, 3) 67 if r3 != 0 { return 32 } 68 69 // ===== Disable -> no emit, but rc 0 ========================== 70 nx_trace_disable(cfg) 71 if cfg.enabled != 0 { return 40 } 72 let r4: nx_int = nx_trace_emit_simple(cfg, 100, 4, 1, 73 NX_SPAN_BATCH_CLOSE, 200, 210) 74 if r4 != 0 { return 41 } 75 76 nx_trace_enable(cfg) 77 if cfg.enabled != 1 { return 42 } 78 79 return 0 80}