use serde_json::json;
use tokio::time::{Duration, timeout};
use crate::storage::PRIMARY_RUNTIME_ID;
use super::support::{bootstrap_test_state, primary_runtime};
#[tokio::test]
async fn runtime_snapshot_for_client_falls_back_when_status_lock_is_blocked() {
let state = bootstrap_test_state().await;
let runtime = primary_runtime(&state).await;
let _status_guard = runtime.status.write().await;
let snapshot = timeout(Duration::from_secs(2), state.runtime_snapshot_for_client())
.await
.expect("runtime_snapshot_for_client 超时");
assert_eq!(snapshot.runtime_id, PRIMARY_RUNTIME_ID);
assert_eq!(snapshot.status, "unknown");
}
#[tokio::test]
async fn runtime_summaries_for_client_fall_back_when_runtime_map_is_blocked() {
let state = bootstrap_test_state().await;
let _runtime_map_guard = state.runtimes.write().await;
let summaries = timeout(Duration::from_secs(2), state.runtime_summaries_for_client())
.await
.expect("runtime_summaries_for_client 超时");
assert_eq!(summaries.len(), 1);
assert_eq!(summaries[0].runtime_id, PRIMARY_RUNTIME_ID);
assert_eq!(summaries[0].status.status, "unknown");
}
#[tokio::test]
async fn hello_payload_falls_back_when_status_lock_is_blocked() {
let state = bootstrap_test_state().await;
let runtime = primary_runtime(&state).await;
let _status_guard = runtime.status.write().await;
let (snapshot, summaries, ..) = timeout(
Duration::from_secs(2),
state.hello_payload("device-test", None),
)
.await
.expect("hello_payload 超时")
.expect("hello_payload 返回错误");
assert_eq!(snapshot.runtime_id, PRIMARY_RUNTIME_ID);
assert_eq!(snapshot.status, "unknown");
assert_eq!(summaries.len(), 1);
assert_eq!(summaries[0].runtime_id, PRIMARY_RUNTIME_ID);
}
#[tokio::test]
async fn get_runtime_status_falls_back_when_runtime_map_is_blocked() {
let state = bootstrap_test_state().await;
let _runtime_map_guard = state.runtimes.write().await;
let response = timeout(
Duration::from_secs(2),
state.handle_request(
"get_runtime_status",
json!({ "runtimeId": PRIMARY_RUNTIME_ID }),
),
)
.await
.expect("get_runtime_status 超时")
.expect("get_runtime_status 返回错误");
assert_eq!(response["runtime"]["runtimeId"], PRIMARY_RUNTIME_ID);
assert_eq!(response["runtime"]["status"]["status"], "unknown");
}
#[tokio::test]
async fn list_runtimes_falls_back_when_runtime_map_is_blocked() {
let state = bootstrap_test_state().await;
let _runtime_map_guard = state.runtimes.write().await;
let response = timeout(
Duration::from_secs(2),
state.handle_request("list_runtimes", json!({})),
)
.await
.expect("list_runtimes 超时")
.expect("list_runtimes 返回错误");
let runtimes = response["runtimes"].as_array().expect("runtimes 应为数组");
assert_eq!(runtimes.len(), 1);
assert_eq!(runtimes[0]["runtimeId"], PRIMARY_RUNTIME_ID);
assert_eq!(runtimes[0]["status"]["status"], "unknown");
}