tests for trace-state

This commit is contained in:
Simon Cruanes 2026-04-04 03:11:16 +00:00
parent 3a7e753348
commit 9b327a9bba
2 changed files with 71 additions and 0 deletions

View file

@ -29,3 +29,18 @@ Trace_context.Traceparent.of_value "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa
Trace_context.Traceparent.to_value trace_id:"4bf92f3577b34da6a3ce929d0e0e4736" parent_id:"00f067aa0ba902b7":
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00"
of_w3c_string "" -> Ok "" (len=0)
of_w3c_string "congo=t61rcwkgmze" -> Ok "congo=t61rcwkgmze" (len=17)
of_w3c_string "congo=t61rcwkgmze,rojo=00f067aa0ba902b7" -> Ok "congo=t61rcwkgmze,rojo=00f067aa0ba902b7" (len=39)
of_w3c_string " vendor=value " -> Ok "vendor=value" (len=12)
of_w3c_string "bad key=value" -> Ok "" (len=0)
of_w3c_string "key=val,=bad,other=fine" -> Ok "key=val,other=fine" (len=18)
set/replace: "vendor=new,other=xyz"
after delete: "vendor=new"
get vendor: "new"
Tracestate.name = "tracestate"
of_w3c_string round-trip: "a=b,c=d"
of_w3c_string empty: ""

View file

@ -54,3 +54,59 @@ let () =
test_to_value
(Trace_id.of_hex "4bf92f3577b34da6a3ce929d0e0e4736")
(Span_id.of_hex "00f067aa0ba902b7")
let () = print_endline ""
(* Trace_state tests *)
let test_trace_state_rt str =
let open Format in
let result = Trace_state.of_w3c_string str in
match result with
| Ok ts ->
printf "of_w3c_string %S -> Ok %S (len=%d)@." str
(Trace_state.to_w3c_string ts)
(Trace_state.encoded_length ts)
| Error e -> printf "of_w3c_string %S -> Error %S@." str e
let () = test_trace_state_rt ""
let () = test_trace_state_rt "congo=t61rcwkgmze"
let () = test_trace_state_rt "congo=t61rcwkgmze,rojo=00f067aa0ba902b7"
let () = test_trace_state_rt " vendor=value "
let () = test_trace_state_rt "bad key=value"
let () = test_trace_state_rt "key=val,=bad,other=fine"
let () = print_endline ""
let test_trace_state_set () =
let ts = Trace_state.empty in
let ts = Trace_state.set ts "vendor" "abc" in
let ts = Trace_state.set ts "other" "xyz" in
let ts = Trace_state.set ts "vendor" "new" in
Format.printf "set/replace: %S@." (Trace_state.to_w3c_string ts);
let ts = Trace_state.delete ts "other" in
Format.printf "after delete: %S@." (Trace_state.to_w3c_string ts);
Format.printf "get vendor: %S@."
(Option.value ~default:"(none)" (Trace_state.get ts "vendor"))
let () = test_trace_state_set ()
let () = print_endline ""
let test_tracestate_header () =
let module TS = Trace_context.Tracestate in
Format.printf "Tracestate.name = %S@." TS.name;
(match TS.of_w3c_string "a=b,c=d" with
| Ok ts ->
Format.printf "of_w3c_string round-trip: %S@." (TS.to_w3c_string ts)
| Error e -> Format.printf "of_w3c_string error: %S@." e);
match TS.of_w3c_string "" with
| Ok ts -> Format.printf "of_w3c_string empty: %S@." (TS.to_w3c_string ts)
| Error e -> Format.printf "of_w3c_string error: %S@." e
let () = test_tracestate_header ()