test: expand snapshot coverage (#4024)

This commit is contained in:
Zhengchao An
2026-06-29 07:56:37 +08:00
committed by GitHub
parent 1104d630d1
commit e1c1d8edcb
30 changed files with 633 additions and 8 deletions
+1
View File
@@ -27,6 +27,7 @@ thiserror = { workspace = true }
tracing = { workspace = true }
[dev-dependencies]
insta = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["test-util","macros","rt-multi-thread"] }
@@ -0,0 +1,24 @@
---
source: crates/concurrency/src/workload.rs
expression: "serde_json::to_value(&registry).expect(\"workload admission registry should serialize\")"
---
{
"entries": [
{
"active": null,
"class": "scanner",
"limit": null,
"queued": null,
"reason": null,
"state": "unknown"
},
{
"active": 3,
"class": "foreground_read",
"limit": 8,
"queued": 1,
"reason": null,
"state": "open"
}
]
}
@@ -0,0 +1,12 @@
---
source: crates/concurrency/src/workload.rs
expression: encoded
---
{
"active": 0,
"class": "foreground_write",
"limit": 0,
"queued": 0,
"reason": "foreground write admission not exposed",
"state": "disabled"
}
+25 -2
View File
@@ -181,7 +181,25 @@ pub trait WorkloadAdmissionSnapshotProvider {
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use serde_json::{Value, json};
fn sorted_json_value(value: Value) -> Value {
match value {
Value::Array(values) => Value::Array(values.into_iter().map(sorted_json_value).collect()),
Value::Object(object) => {
let mut entries: Vec<_> = object.into_iter().collect();
entries.sort_by(|(left, _), (right, _)| left.cmp(right));
Value::Object(
entries
.into_iter()
.map(|(key, value)| (key, sorted_json_value(value)))
.collect(),
)
}
value => value,
}
}
#[test]
fn workload_contract_covers_required_classes() {
@@ -211,6 +229,10 @@ mod tests {
);
let registry = WorkloadAdmissionRegistrySnapshot::new(vec![unknown.clone(), open.clone()]);
insta::assert_json_snapshot!(
"workload_admission_registry_snapshot",
sorted_json_value(serde_json::to_value(&registry).expect("workload admission registry should serialize"))
);
assert_eq!(registry.entries(), &[unknown, open]);
assert_eq!(
registry.get(WorkloadClass::Scanner).map(|snapshot| snapshot.state),
@@ -262,8 +284,9 @@ mod tests {
let snapshot = WorkloadAdmissionSnapshot::new(WorkloadClass::ForegroundWrite, AdmissionState::Disabled)
.with_counts(Some(0), Some(0), Some(0))
.with_reason("foreground write admission not exposed");
let encoded = serde_json::to_value(&snapshot).expect("serialize workload admission snapshot");
let encoded = sorted_json_value(serde_json::to_value(&snapshot).expect("serialize workload admission snapshot"));
insta::assert_json_snapshot!("workload_admission_snapshot", encoded);
assert_eq!(encoded["class"], json!("foreground_write"));
assert_eq!(encoded["state"], json!("disabled"));