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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! Shared chaos-harness utilities for failure-injection
//! integration tests. Supports FAILURE_PATH_HARDENING_PLAN
//! Stage 3 — the (subprotocol × phase × failure-mode) matrix.
//!
//! Five hand-authored failure-injection tests (`peer_death_*`,
//! `migration_target_failure_*`, `rendezvous_coordinator`'s
//! staleness case, etc.) each reimplement their own polling
//! loops, config builders, and setup primitives. This module
//! centralizes the patterns so new matrix cells are cheap to
//! add and the assertions are uniform: one consistent
//! `await_*` / `chaos_*` / `drive_*` vocabulary across every
//! failure-injection test.
//!
//! # Usage
//!
//! Each integration test file under `tests/` that wants the
//! harness adds:
//!
//! ```ignore
//! mod common;
//! use common::*;
//! ```
//!
//! Cargo's integration-test model treats subdirectories under
//! `tests/` as shared modules rather than separate test
//! binaries, so `tests/common/mod.rs` is compiled into each
//! test that `mod common;`-imports it but not run as its own
//! test.
//!
//! # What's here
//!
//! - **Setup**: [`fast_fd_config`], [`build_fast_node`],
//! [`connect_pair`] — the standard 100 ms / 500 ms
//! heartbeat / session-timeout recipe that the existing
//! failure-injection tests all use, extracted once.
//! - **Polling**: [`await_condition`], [`await_peer_failed`],
//! [`await_peer_recovered`], [`await_capability_index_evicts`],
//! [`await_peer_count`].
//! - **Chaos injection**: [`chaos_partition`], [`chaos_heal`],
//! [`drive_failure_detection`].
//!
//! # What's NOT here (Stage 3 blockers)
//!
//! The full Stage-3 matrix calls for `wire-packet-delay`,
//! `wire-packet-reorder`, `wire-packet-duplicate`, and
//! `clock-jump-*` failure modes. None are implementable with
//! the crate's current public API — they need either a
//! dispatch-layer interception hook or a mock-time substitution.
//! See `docs/FAILURE_PATH_HARDENING_PLAN.md` §Stage 3 for the
//! blocker discussion. This harness covers
//! `peer-crash-mid-phase`, `partition-split`,
//! `partition-heal-mid-phase`, and `slow-heartbeat` — the
//! cells that are reachable today.
use SocketAddr;
use Arc;
use Duration;
use ;
/// Default socket buffer for all chaos-harness tests. 256 KiB
/// is comfortably above the OS defaults on Linux + macOS so
/// tests don't silently drop packets under the chaos load.
pub const CHAOS_BUFFER_SIZE: usize = 256 * 1024;
/// Shared PSK across every chaos-harness node. Identical to
/// the inline constant used in the pre-harness tests.
pub const CHAOS_PSK: = ;
/// Build a `MeshNodeConfig` tuned for fast failure detection:
/// heartbeat every 100 ms, session timeout 500 ms → peer
/// transitions to Failed after ~1.5 s of silence (3×
/// session_timeout, per [`FailureDetectorConfig`]'s miss
/// threshold of 3).
///
/// The existing `peer_death_clears_capability_index`,
/// `peer_death_evicts_peer_map`, and
/// `migration_target_failure_mid_chunking` tests all
/// independently picked the same 100/500 recipe. Centralized
/// here so any tuning decision (e.g., CI running slow and
/// needing 200/1000) happens once.
/// Build a node with [`fast_fd_config`]. Use [`build_node_with`]
/// for custom configs.
pub async
/// Build a node against a caller-supplied config.
pub async
/// Connect A→B via the handshake + accept pattern every
/// failure-injection test uses. Leaves both nodes started.
pub async
// ─────────────────────────────────────────────────────────────
// Polling helpers
// ─────────────────────────────────────────────────────────────
/// Poll `check` until it returns `true` or `limit` elapses.
/// On timeout, panics with a message including `description`
/// so the test failure points at the specific invariant that
/// didn't converge rather than a generic "assertion failed."
///
/// 50 ms poll interval is the same cadence the pre-harness
/// tests used; tight enough that FD transitions are caught
/// promptly, loose enough that CPU cost is negligible.
pub async
/// Wait for `observer.failure_detector()` to mark `target_id`
/// as `NodeStatus::Failed`. Drives the detector on each poll
/// iteration (via `check_all()`) because production-config
/// `check_all` runs on a heartbeat cadence that tests shouldn't
/// rely on firing within the window.
pub async
/// Wait for `observer.failure_detector()` to mark `target_id`
/// as `NodeStatus::Healthy`. Used after partition heal to pin
/// the recovery path.
pub async
/// Wait until `observer`'s capability index no longer has an
/// entry for `target_id`. This is the P1-5 three-way-agreement
/// invariant — a peer the FD has failed must be evicted from
/// every derived map.
pub async
/// Wait until `observer.peer_count()` reaches `expected`.
/// Useful for teardown assertions ("after failure, the peers
/// map should have shrunk by 1").
pub async
// ─────────────────────────────────────────────────────────────
// Chaos injection
// ─────────────────────────────────────────────────────────────
/// Full bilateral partition: `a` and `b` each drop every
/// packet from the other. Neither node sees the other's
/// heartbeats; both FD's will eventually mark the other
/// Failed. Symmetric by construction — use this for the
/// "peer crashed hard" pattern.
/// Undo [`chaos_partition`]. Both sides must unblock — a
/// one-sided unblock leaves the partition up from the
/// still-blocking side. Heartbeats resume on the next
/// heartbeat-interval tick.
/// One-sided block: `observer` drops every packet to/from
/// `target`, but `target` is unaware and keeps sending.
/// Effectively a full partition from `observer`'s perspective;
/// `target`'s FD will also eventually mark `observer` Failed
/// because `observer` stops sending heartbeats (it's filtering
/// its own outbound traffic too).
/// Wait past the failure threshold, then explicitly drive the
/// detector. Use after [`chaos_partition`] when the test
/// expects the peer to be marked Failed. Returns the list of
/// newly-failed node IDs from the explicit `check_all()`.
///
/// `wait` should be `>= miss_threshold × session_timeout` —
/// with [`fast_fd_config`] that's 3 × 500 ms = 1.5 s, so
/// 2 s gives a comfortable margin.
pub async