cover runtime file json helpers

This commit is contained in:
Nanaloveyuki
2026-06-14 04:11:57 +08:00
parent db5d95576d
commit bd75deb879
+59 -7
View File
@@ -864,6 +864,12 @@ test "configured logger exposes file sink observability helpers" {
}
test "file state json helpers stringify stable snapshots" {
let rotation_json = file_rotation_config_to_json(file_rotation(64, max_backups=2))
let rotation_obj = rotation_json.as_object().unwrap()
inspect(@json_parser.stringify(rotation_json), content="{\"max_bytes\":64,\"max_backups\":2}")
inspect(rotation_obj.get("max_bytes").unwrap().as_number().unwrap().to_int(), content="64")
inspect(rotation_obj.get("max_backups").unwrap().as_number().unwrap().to_int(), content="2")
let plain = file_sink_state_to_json(
FileSinkState::new(
"demo.log",
@@ -877,10 +883,22 @@ test "file state json helpers stringify stable snapshots" {
rotation_failures=4,
),
)
let plain_obj = plain.as_object().unwrap()
let plain_rotation_obj = plain_obj.get("rotation").unwrap().as_object().unwrap()
inspect(
@json_parser.stringify(plain),
content="{\"path\":\"demo.log\",\"available\":true,\"append\":false,\"auto_flush\":true,\"open_failures\":1,\"write_failures\":2,\"flush_failures\":3,\"rotation_failures\":4,\"rotation\":{\"max_bytes\":64,\"max_backups\":2}}",
)
inspect(plain_obj.get("path").unwrap().as_string().unwrap(), content="demo.log")
inspect(plain_obj.get("available").unwrap().as_bool().unwrap(), content="true")
inspect(plain_obj.get("append").unwrap().as_bool().unwrap(), content="false")
inspect(plain_obj.get("auto_flush").unwrap().as_bool().unwrap(), content="true")
inspect(plain_obj.get("open_failures").unwrap().as_number().unwrap().to_int(), content="1")
inspect(plain_obj.get("write_failures").unwrap().as_number().unwrap().to_int(), content="2")
inspect(plain_obj.get("flush_failures").unwrap().as_number().unwrap().to_int(), content="3")
inspect(plain_obj.get("rotation_failures").unwrap().as_number().unwrap().to_int(), content="4")
inspect(plain_rotation_obj.get("max_bytes").unwrap().as_number().unwrap().to_int(), content="64")
inspect(plain_rotation_obj.get("max_backups").unwrap().as_number().unwrap().to_int(), content="2")
inspect(
stringify_file_sink_state(
FileSinkState::new(
@@ -900,6 +918,33 @@ test "file state json helpers stringify stable snapshots" {
}
test "runtime file state json helpers stringify queue snapshots" {
let runtime_json = runtime_file_state_to_json(
RuntimeFileState::new(
FileSinkState::new(
"queue.log",
available=true,
append=true,
auto_flush=false,
rotation=None,
open_failures=0,
write_failures=1,
flush_failures=2,
rotation_failures=3,
),
queued=true,
pending_count=7,
dropped_count=5,
),
)
let runtime_obj = runtime_json.as_object().unwrap()
let runtime_file_obj = runtime_obj.get("file").unwrap().as_object().unwrap()
inspect(runtime_obj.get("queued").unwrap().as_bool().unwrap(), content="true")
inspect(runtime_obj.get("pending_count").unwrap().as_number().unwrap().to_int(), content="7")
inspect(runtime_obj.get("dropped_count").unwrap().as_number().unwrap().to_int(), content="5")
inspect(runtime_file_obj.get("path").unwrap().as_string().unwrap(), content="queue.log")
inspect(runtime_file_obj.get("available").unwrap().as_bool().unwrap(), content="true")
inspect(runtime_file_obj.get("rotation").unwrap() is @json_parser.JsonValue::Null, content="true")
let json = stringify_runtime_file_state(
RuntimeFileState::new(
FileSinkState::new(
@@ -925,18 +970,25 @@ test "runtime file state json helpers stringify queue snapshots" {
}
test "file sink policy json helpers stringify stable policies" {
let policy_json = file_sink_policy_to_json(
FileSinkPolicy::new(
append=false,
auto_flush=true,
rotation=Some(file_rotation(96, max_backups=3)),
),
)
let policy_obj = policy_json.as_object().unwrap()
let policy_rotation_obj = policy_obj.get("rotation").unwrap().as_object().unwrap()
inspect(
@json_parser.stringify(
file_sink_policy_to_json(
FileSinkPolicy::new(
append=false,
auto_flush=true,
rotation=Some(file_rotation(96, max_backups=3)),
),
),
policy_json,
),
content="{\"append\":false,\"auto_flush\":true,\"rotation\":{\"max_bytes\":96,\"max_backups\":3}}",
)
inspect(policy_obj.get("append").unwrap().as_bool().unwrap(), content="false")
inspect(policy_obj.get("auto_flush").unwrap().as_bool().unwrap(), content="true")
inspect(policy_rotation_obj.get("max_bytes").unwrap().as_number().unwrap().to_int(), content="96")
inspect(policy_rotation_obj.get("max_backups").unwrap().as_number().unwrap().to_int(), content="3")
inspect(
stringify_file_sink_policy(
FileSinkPolicy::new(append=true, auto_flush=false, rotation=None),