1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Accounting and diagnostics for a delivery the shared deadline drops.
//!
//! A delivery that outlives the connection's wait never reaches its own timeout
//! arm, so the outcome it owed is recorded where the drop happens. A final
//! chunk is also named in the warning there, because no later record of that
//! body can exist; a terminal failure is only counted, since its own path
//! carries the diagnostic.
use std::future;
use rstest::rstest;
use tokio::time::Instant;
use super::{Delivery, HandlerWait, UnsettledDelivery, race_delivery};
use crate::{
app::inbound_handler::{
stream_active::{BodyIdentity, HandlerFuture},
stream_outcome_support::{Expectation, assert_exchange, paused_block_on, recorded, wait},
},
metrics::StreamingBodyOutcome,
};
/// A delivery still pending at the connection's deadline is counted once.
///
/// The delivery future here never resolves, so the connection's own deadline is
/// necessarily the first to fire and the delivery is dropped unsettled. That
/// drop must still record the outcome the delivery owed, exactly once, rather
/// than leaving the body uncounted: the deadline and the delivery share one
/// instant, and nothing would report for the dropped future afterwards.
#[rstest]
#[test]
fn a_delivery_still_pending_at_the_deadline_is_counted_once() {
let (settled, counts) = recorded(|| {
paused_block_on(async {
let mut handler: HandlerFuture = Box::pin(future::pending());
let deadline = Instant::now() + wait();
race_delivery(
deadline,
future::pending::<Delivery>(),
&mut handler,
UnsettledDelivery::counted(StreamingBodyOutcome::Abandoned),
)
.await
})
});
assert_exchange(
settled,
&counts,
&Expectation {
reply: HandlerWait::Deadline,
recorded: StreamingBodyOutcome::Abandoned,
why: "a delivery dropped at the deadline must be counted exactly once",
},
);
}
/// A final chunk the outer deadline drops is named in a warning.
///
/// The delivery future here never resolves, so the connection's deadline is the
/// first to fire and the chunk is dropped unsettled. Its own timeout arm never
/// runs, so the warning has to be emitted where the drop happens; without it an
/// operator sees an abandoned body with no message it can be attributed to.
#[cfg(feature = "metrics")]
#[rstest]
#[test]
fn a_final_chunk_dropped_at_the_deadline_is_warned_about() {
// A bespoke identifier: the log buffer is process-wide and sibling tests
// emit the same message, so only a record naming this body counts.
let identity = BodyIdentity {
message_id: UNIQUE_MESSAGE_ID,
correlation_id: Some(11),
};
let mut handler: HandlerFuture = Box::pin(future::pending());
let deadline = Instant::now() + wait();
let mut log = wireframe_testing::logger();
log.clear();
let (settled, counts) = recorded(|| {
paused_block_on(race_delivery(
deadline,
future::pending::<Delivery>(),
&mut handler,
UnsettledDelivery::final_chunk(identity),
))
});
assert_exchange(
settled,
&counts,
&Expectation {
reply: HandlerWait::Deadline,
recorded: StreamingBodyOutcome::Abandoned,
why: "a final chunk dropped at the deadline must be counted once",
},
);
let fingerprint = format!("message_id={UNIQUE_MESSAGE_ID}");
let warnings: Vec<String> = std::iter::from_fn(|| log.pop())
.map(|record| record.args().to_owned())
.filter(|message| {
message.contains("final request body chunk was not admitted")
&& message.contains(&fingerprint)
})
.collect();
assert_eq!(
warnings.len(),
1,
"exactly one warning must name the dropped final chunk; captured: {warnings:#?}"
);
let warning = warnings
.first()
.expect("the single captured warning must be inspectable");
assert!(
warning.contains(&format!("message_id={}", identity.message_id)),
"the warning must name the message; captured: {warning}"
);
assert!(
warning.contains("error_category=\"abandoned_body\""),
"the warning must carry the body's category; captured: {warning}"
);
}
/// Message identifier that only this module's case reports under.
const UNIQUE_MESSAGE_ID: u32 = 9_991;