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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! Property tests for the decoder-failure fallbacks.
//!
//! The fixed cases in [`super`] document the boundary examples; these
//! properties hold for any order of decoded frames and decoder failures, and
//! for any drop budget. A failing case prints the generated sequence, so the
//! shrinker can report the shortest ordering that breaks a fallback.
use std::io;
use proptest::prelude::*;
use super::super::{test_support::ScriptedHook, *};
use crate::codec::RecoveryPolicy;
/// One step of a generated recovery sequence.
#[derive(Clone, Copy, Debug)]
enum Step {
/// A frame decoded, which restarts the drop streak.
Frame,
/// A decoder failure the hook classified as `policy`.
Failure {
/// Whether the failure came from `decode_eof`.
eof: bool,
/// Bytes the decoder consumed before failing.
consumed: usize,
/// Policy the hook selected.
policy: RecoveryPolicy,
},
}
impl Step {
/// The policy this step's hook answers with, when it fails.
const fn policy(self) -> Option<RecoveryPolicy> {
match self {
Self::Frame => None,
Self::Failure { policy, .. } => Some(policy),
}
}
/// Whether the connection could drop this failure if the streak allows it.
const fn is_droppable(self) -> bool {
matches!(
self,
Self::Failure {
eof: false,
consumed,
policy: RecoveryPolicy::Drop,
} if consumed > 0
)
}
}
/// Any of the three policies a hook may select.
fn policy_strategy() -> impl Strategy<Value = RecoveryPolicy> {
prop_oneof![
Just(RecoveryPolicy::Drop),
Just(RecoveryPolicy::Quarantine),
Just(RecoveryPolicy::Disconnect),
]
}
/// A decoded frame, or a failure with any origin, consumption and policy.
fn step_strategy() -> impl Strategy<Value = Step> {
prop_oneof![
Just(Step::Frame),
(any::<bool>(), 0usize..3, policy_strategy()).prop_map(|(eof, consumed, policy)| {
Step::Failure {
eof,
consumed,
policy,
}
}),
]
}
proptest! {
#![proptest_config(ProptestConfig { cases: 32, ..Default::default() })]
/// The documented fallbacks hold for any order of frames and failures.
#[test]
fn fallbacks_hold_over_generated_event_sequences(
max_drops in 1_u32..5,
steps in prop::collection::vec(step_strategy(), 1..24),
) {
let policies = steps.iter().filter_map(|step| step.policy()).collect();
let hook = ScriptedHook::new(policies);
let config = RecoveryConfig::default().max_consecutive_drops(max_drops);
let mut coordinator = RecoveryCoordinator::new(Some(&hook), config);
// Applied drops since the last decoded frame, as the sequence saw them.
let mut drops = 0_u32;
let mut just_reset = false;
for step in steps {
let Step::Failure { eof, consumed, .. } = step else {
coordinator.record_decoded_frame();
drops = 0;
just_reset = true;
continue;
};
let origin = if eof {
DecodeOrigin::DecodeEof
} else {
DecodeOrigin::Decode
};
let failure = DecodeFailure::new(
io::Error::new(io::ErrorKind::InvalidData, "malformed"),
origin,
consumed,
);
let action = coordinator.record_failure(failure).action;
if eof {
prop_assert_eq!(action, RecoveryAction::Disconnect, "an EOF failure disconnects");
}
if consumed == 0 {
prop_assert!(
!matches!(action, RecoveryAction::Drop | RecoveryAction::Quarantine(_)),
"a failure that consumed nothing cannot be discarded"
);
}
if step.is_droppable() {
if just_reset {
prop_assert_eq!(action, RecoveryAction::Drop, "a frame resets the streak");
}
if drops >= max_drops {
prop_assert_eq!(
action,
RecoveryAction::Disconnect,
"the failure after the budget must disconnect"
);
} else {
prop_assert_eq!(action, RecoveryAction::Drop, "a budgeted drop is applied");
drops += 1;
}
}
just_reset = false;
}
}
}