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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! Compact timing switches for the six client operation categories.
//!
//! [`ClientOperation`] names one of six timed categories: `connect`, `send`,
//! `receive`, `call`, `streaming`, and `close`. [`ClientTimingFlags`] stores
//! the six selections as independent bits in one copyable `u8` instead of six
//! parallel booleans, and [`TracingConfig`](super::TracingConfig) drives them
//! through its per-operation setters and reads them back via `timing_enabled`.
/// Client operation whose elapsed time can be recorded.
#[derive(Clone, Copy, Debug)]
pub(crate) enum ClientOperation {
/// Connection establishment.
Connect,
/// One-way send.
Send,
/// One-way receive.
Receive,
/// Request-response call.
Call,
/// Streaming call.
Streaming,
/// Orderly connection close.
Close,
}
impl ClientOperation {
/// Return the independent bit reserved for this operation.
const fn mask(self) -> u8 {
match self {
Self::Connect => 0b00_0001,
Self::Send => 0b00_0010,
Self::Receive => 0b00_0100,
Self::Call => 0b00_1000,
Self::Streaming => 0b01_0000,
Self::Close => 0b10_0000,
}
}
}
/// Timing selection stored as one value rather than six independent booleans.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct ClientTimingFlags(u8);
impl ClientTimingFlags {
/// Set one operation without changing the other five.
///
/// # Examples
///
/// ```plaintext
/// let timing = ClientTimingFlags::default()
/// .with_enabled(ClientOperation::Connect, true)
/// .with_enabled(ClientOperation::Close, true);
///
/// assert!(timing.is_enabled(ClientOperation::Connect));
/// assert!(timing.is_enabled(ClientOperation::Close));
/// assert!(!timing.is_enabled(ClientOperation::Call));
/// ```
pub(crate) const fn with_enabled(mut self, operation: ClientOperation, enabled: bool) -> Self {
let mask = operation.mask();
if enabled {
self.0 |= mask;
} else {
self.0 &= !mask;
}
self
}
/// Set every operation to the same timing policy.
///
/// # Examples
///
/// ```plaintext
/// let silent = ClientTimingFlags::all(false);
/// assert!(!silent.is_enabled(ClientOperation::Connect));
///
/// let verbose = ClientTimingFlags::all(true);
/// assert!(verbose.is_enabled(ClientOperation::Connect));
/// assert!(verbose.is_enabled(ClientOperation::Close));
/// ```
pub(crate) const fn all(enabled: bool) -> Self { Self(if enabled { 0b11_1111 } else { 0 }) }
/// Report whether timing is enabled for one operation.
///
/// # Examples
///
/// ```plaintext
/// let timing = ClientTimingFlags::default().with_enabled(ClientOperation::Send, true);
///
/// assert!(timing.is_enabled(ClientOperation::Send));
/// assert!(!timing.is_enabled(ClientOperation::Receive));
/// ```
pub(crate) const fn is_enabled(self, operation: ClientOperation) -> bool {
self.0 & operation.mask() != 0
}
}
#[cfg(test)]
mod tests {
//! Checks timing updates against a six-operation reference model.
use proptest::{collection::vec, prelude::*};
use super::{ClientOperation, ClientTimingFlags};
use crate::client::TracingConfig;
const OPERATIONS: [ClientOperation; 6] = [
ClientOperation::Connect,
ClientOperation::Send,
ClientOperation::Receive,
ClientOperation::Call,
ClientOperation::Streaming,
ClientOperation::Close,
];
#[derive(Clone, Copy, Debug)]
enum TimingUpdate {
Set(ClientOperation, bool),
SetAll(bool),
}
fn timing_update_strategy() -> impl Strategy<Value = TimingUpdate> {
let operation_strategy = prop_oneof![
Just(ClientOperation::Connect),
Just(ClientOperation::Send),
Just(ClientOperation::Receive),
Just(ClientOperation::Call),
Just(ClientOperation::Streaming),
Just(ClientOperation::Close),
];
prop_oneof![
(operation_strategy, any::<bool>())
.prop_map(|(operation, enabled)| TimingUpdate::Set(operation, enabled)),
any::<bool>().prop_map(TimingUpdate::SetAll),
]
}
fn set_expected_timing(expected: &mut [bool; 6], operation: ClientOperation, enabled: bool) {
let [connect, send, receive, call, streaming, close] = expected;
let selected = match operation {
ClientOperation::Connect => connect,
ClientOperation::Send => send,
ClientOperation::Receive => receive,
ClientOperation::Call => call,
ClientOperation::Streaming => streaming,
ClientOperation::Close => close,
};
*selected = enabled;
}
fn set_config_timing(
config: TracingConfig,
operation: ClientOperation,
enabled: bool,
) -> TracingConfig {
match operation {
ClientOperation::Connect => config.with_connect_timing(enabled),
ClientOperation::Send => config.with_send_timing(enabled),
ClientOperation::Receive => config.with_receive_timing(enabled),
ClientOperation::Call => config.with_call_timing(enabled),
ClientOperation::Streaming => config.with_streaming_timing(enabled),
ClientOperation::Close => config.with_close_timing(enabled),
}
}
fn flags_snapshot(flags: ClientTimingFlags) -> [bool; 6] {
OPERATIONS.map(|operation| flags.is_enabled(operation))
}
fn config_snapshot(config: &TracingConfig) -> [bool; 6] {
OPERATIONS.map(|operation| config.timing_enabled(operation))
}
proptest! {
#[test]
fn timing_updates_match_reference_after_every_action(
updates in vec(timing_update_strategy(), 1..64),
) {
let mut flags = ClientTimingFlags::default();
let mut config = TracingConfig::default();
let mut expected = [false; 6];
for (step, update) in updates.into_iter().enumerate() {
match update {
TimingUpdate::Set(operation, enabled) => {
flags = flags.with_enabled(operation, enabled);
config = set_config_timing(config, operation, enabled);
set_expected_timing(&mut expected, operation, enabled);
}
TimingUpdate::SetAll(enabled) => {
flags = ClientTimingFlags::all(enabled);
config = config.with_all_timing(enabled);
expected = [enabled; 6];
}
}
prop_assert_eq!(
flags_snapshot(flags),
expected,
"ClientTimingFlags diverged after update {}: {:?}",
step,
update,
);
prop_assert_eq!(
config_snapshot(&config),
expected,
"TracingConfig diverged after update {}: {:?}",
step,
update,
);
}
}
}
}