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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! Barriers allow tests to granularly observe and control the execution of
//! source code by injecting observability and control hooks into source code.
//!
//! Barriers allow construction of complex tests which otherwise may rely on
//! timing conditions (which are difficult to write, flaky, and hard to
//! maintain) or monitoring and control of the network layer.
//!
//! Barriers are designed for Turmoil simulation tests. They allow test code
//! to step the simulation until a barrier is triggered, and optionally suspend
//! source code execution until the test is ready to proceed.
//!
//! # Architecture
//!
//! ```text
//! ┌──────────────┐ ┌─────────────────────┐
//! │ Source Code │ │ Test Code │
//! │ │ │ │
//! │ ┌─────────┐ │ ┌──────────────────┐ │ │
//! │ │ Trigger ┼─┼─────►│ Barrier Repo │◄───┼── Barrier::build() │
//! │ └─────────┘ │ │ (Thread Local) │ │ │
//! │ ┌─────────┐ │ ┌───┼ ├────┼─► Barrier::wait() │
//! │ │ Resumer │◄┼──┘ └──────────────────┘ │ │
//! │ └─────────┘ │ │ │
//! │ │ │ │
//! └──────────────┘ └─────────────────────┘
//! ```
//!
//! A barrier consists of two halves: a `Trigger` which defines the condition
//! a barrier is waiting for, and a `Resumer` which controls when the source
//! code in a barrier is released. Interesting points of source code may be
//! annotated with triggers; these triggers will no-op if test code is not
//! interested and are conditionally compiled out of non-test code.
//!
//! When test code creates a barrier, the condition and resumer is registered
//! in the barrier repo. Most barriers are 'observe-only' and do not control
//! execution (typically test code is simply driving simulation forward until
//! a Barrier is triggered). However, test code may cause a future hitting a
//! barrier to suspend until the test code resumes it. It can also cause the
//! code to panic, if testing how panics are handled is desired.
//!
//! Triggers are type-safe Rust structs. Source code may define triggers as any
//! type desired. Barrier conditions are defined as closures that match against
//! a trigger. Reactions are built as an enum of well-defined actions; arbitrary
//! reaction code is not allowed to curtail insane usage.
//!
//! Source code can use either [`trigger()`] (async, supports suspension) or
//! [`trigger_noop()`] (sync, only for observation) depending on whether the
//! execution flow needs to be potentially suspended by test code.
//!
//! Note: Each trigger event wakes at most one barrier and processes in order
//! of registration. Avoid registering multiple barriers for the same triggers
//! to avoid confusion.
//!
//! # Example
//!
//! ```ignore
//! // In source code (conditionally compiled for simulation)
//! async fn handle_prepare_ack(prepare_ack: PrepareAck) {
//! // Processing...
//!
//! #[cfg(feature = "turmoil-barriers")]
//! turmoil::barriers::trigger(
//! MyBarriers::PrepareAckReceived(prepare_ack.tx_id)
//! ).await;
//!
//! // Continue processing
//! }
//!
//! // In test code:
//! #[test]
//! fn test_prepare_ack_handling() {
//! let mut sim = turmoil::Builder::new().build();
//!
//! // Register a barrier which will suspend when condition matches
//! let mut barrier = Barrier::build(
//! Reaction::Suspend,
//! move |t: &MyBarriers| {
//! matches!(t, MyBarriers::PrepareAckReceived(id) if *id == expected_tx_id)
//! }
//! );
//!
//! sim.client("test", async move {
//! // Trigger the function being tested
//! handle_prepare_ack(PrepareAck { tx_id: expected_tx_id }).await;
//! Ok(())
//! });
//!
//! // Step simulation until barrier is triggered
//! // Source code is now suspended at the trigger point
//! let triggered = barrier.step_until_triggered(&mut sim).unwrap();
//!
//! // When ready to continue source execution, drop triggered
//! drop(triggered);
//!
//! sim.run().unwrap();
//! }
//! ```
use ;
use ;
use Uuid;
thread_local!
/// Trigger a barrier (if any registered) with the given value.
///
/// Use this function when you need to give test code the ability to suspend
/// source execution at trigger points. Supports both observation ([`Reaction::Noop`])
/// and suspension ([`Reaction::Suspend`]) of execution flow.
///
/// If you only need to notify without suspension capability, use [`trigger_noop()`]
/// instead.
///
/// This function is a no-op if no barrier is registered for the given trigger type.
pub async
/// Synchronously trigger a barrier with the given value.
///
/// Use this function when you need to notify barriers about events without
/// suspending execution. Only supports [`Reaction::Noop`] reactions and will
/// panic if used with a barrier configured with [`Reaction::Suspend`].
///
/// For suspension capability, use [`trigger()`] instead.
///
/// This function is a no-op if no barrier is registered for the given trigger type.
type Condition = dyn Fn ;
/// A barrier that waits for source code to trigger a specific condition.
///
/// Create barriers using [`Barrier::new()`] for observation-only barriers,
/// or [`Barrier::build()`] to specify a custom reaction.
///
/// # Example
///
/// ```ignore
/// // Wait for a specific event
/// let mut barrier = Barrier::new(|t: &MyEvent| {
/// matches!(t, MyEvent::SomethingHappened)
/// });
///
/// // ... run simulation ...
///
/// let triggered = barrier.wait().await.unwrap();
/// println!("Event occurred: {:?}", *triggered);
/// ```
/// A handle to a triggered barrier.
///
/// This struct holds the trigger value and controls when suspended source code
/// is released. For [`Reaction::Suspend`] barriers, dropping this handle will
/// resume the suspended source code.
///
/// Use [`Deref`] to access the trigger value.
/// The reaction when a barrier is triggered.
type Waker = ;