#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing,
reason = "test code"
)]
use kernel::{AxCode, B3Hash, EventDraft, EventKind, EventRecord, Payload, RunId, Seq, TimeMs};
use serde_json::json;
use wire::{Note, note_of};
fn record(kind: EventKind, data: serde_json::Value) -> EventRecord {
let serde_json::Value::Object(map) = data else {
panic!("a payload is an object")
};
EventRecord::from_draft(
EventDraft {
run: RunId::from_bytes([3; 16]),
t: TimeMs::new(9),
who: "builder@lab.1".to_owned(),
addr: None,
kind,
data: Payload::new(map).unwrap(),
ig: false,
},
Seq::FIRST,
B3Hash::from_bytes([0; 32]),
)
}
#[test]
fn a_watchdog_back_off_reads_as_the_failure_it_waits_out() {
let line = record(
EventKind::WatchdogFired,
json!({
"action": "back_off",
"until_ms": 5000,
"code": AxCode::Provider.as_str(),
"subject": "the provider answered 503",
"corrections": 0,
"provider_failures": 1,
}),
);
let Some(Note::Refused { error, at }) = note_of(EventKind::WatchdogFired, &line) else {
panic!(
"a back-off is a refusal the run waits out: {:?}",
note_of(EventKind::WatchdogFired, &line)
);
};
assert_eq!(
(error.code(), error.subject(), at),
(&AxCode::Provider, "the provider answered 503", Seq::FIRST)
);
}
#[test]
fn a_watchdog_switch_reads_as_the_failure_the_account_met() {
let line = record(
EventKind::WatchdogFired,
json!({
"action": "switch",
"to": "b",
"code": AxCode::Provider.as_str(),
"subject": "401 Unauthorized",
"corrections": 0,
"provider_failures": 1,
}),
);
let Some(Note::Refused { error, at }) = note_of(EventKind::WatchdogFired, &line) else {
panic!(
"a switch is a refusal answered on another account: {:?}",
note_of(EventKind::WatchdogFired, &line)
);
};
assert_eq!(
(error.code(), error.subject(), error.recovery(), at),
(
&AxCode::Provider,
"401 Unauthorized",
"the watchdog calls again at once on the account b",
Seq::FIRST
)
);
}