asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
Documentation
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Golden artifacts test for symbol_cancel protocol lifecycle.
//!
//! This module captures the complete symbol cancellation protocol lifecycle
//! in golden strings to ensure protocol stability and detect regressions across
//! token creation, cancellation message preparation, broadcasting, deduplication,
//! and cleanup coordination.

#[cfg(test)]
mod tests {
    use super::super::symbol_cancel::{
        CancelBroadcaster, CancelMessage, CancelSink, CleanupCoordinator, CleanupHandler, PeerId,
        SymbolCancelToken,
    };
    use crate::cancel::CleanupResult;
    use crate::types::symbol::{ObjectId, Symbol};
    use crate::types::{Budget, CancelReason, Time};
    use crate::util::DetRng;
    use std::sync::{Arc, Mutex as StdMutex};

    const EXPECTED_PROTOCOL_LIFECYCLE: &str = "\
phase=token_creation
token_creation::token1_created=object_id=00000000000000000000000090abcdef, cleanup_budget=deadline_ns=Some(500000000),poll_quota=4294967295,priority=128
token_creation::token2_created=object_id=000000000000000000000000fedcba09, cleanup_budget=deadline_ns=None,poll_quota=4294967295,priority=128
phase=broadcaster_setup
broadcaster_setup::registered_tokens=2
phase=listener_registration
listener_registration::token1_listeners=registered=1
phase=cancellation_initiation
cancellation_initiation::cancel_msg1=object_id=00000000000000000000000090abcdef, kind=Timeout, at=1000000000ns, seq=0
cancellation_initiation::token1_state=cancelled=true, at_ns=Some(1000000000), reason_kind=Some(\"Timeout\")
phase=message_broadcasting
message_broadcasting::msg_dedup_check=forwarded=false
message_broadcasting::duplicate_rejected=forwarded=false
phase=cross_broadcaster_comm
cross_broadcaster_comm::cross_broadcaster_receive=forwarded=true, token3_cancelled=true, token3_reason=Some(\"Timeout\")
phase=hierarchical_cancellation
hierarchical_cancellation::child_created=parent_object_id=000000000000000000000000fedcba09, child_object_id=000000000000000000000000fedcba09
hierarchical_cancellation::hierarchical_result=parent_cancelled=true, child_cancelled=true, child_reason=Some(\"ParentCancelled\")
phase=cleanup_coordination
cleanup_coordination::cleanup_obj1=cleaned=2, bytes=8, within_budget=true, completed=true, handlers=1, errors=0
cleanup_coordination::cleanup_obj2=cleaned=0, bytes=0, within_budget=true, completed=false, handlers=0, errors=1
phase=final_statistics
final_statistics::broadcaster1_stats=initiated=1, duplicates=2, forwarded=0
final_statistics::broadcaster2_stats=initiated=0, duplicates=0, forwarded=1
final_statistics::listener_events=kind=Timeout, msg=Some(\"operation timeout\"), at=1000000000ns
final_statistics::panic_stats=token1=0, token2=0, child=0
";

    const EXPECTED_EDGE_CASES: &str = "\
phase=nonexistent_object
nonexistent_object::nonexistent_cancel=object_id=00000000000000000000000000000000, kind=User, at=2000000000ns, seq=0
phase=multiple_cancellations
multiple_cancellations::first_cancel=success=true
multiple_cancellations::second_cancel=success=false
multiple_cancellations::final_reason=Some(\"Timeout\")
phase=empty_broadcaster
empty_broadcaster::empty_stats=initiated=0, duplicates=0, forwarded=0, pending_retries=0
";

    struct NullSink;

    impl CancelSink for NullSink {
        fn send_to(
            &self,
            _peer: &PeerId,
            _msg: &CancelMessage,
        ) -> impl std::future::Future<Output = crate::error::Result<()>> + Send {
            std::future::ready(Ok(()))
        }

        fn broadcast(
            &self,
            _msg: &CancelMessage,
        ) -> impl std::future::Future<Output = crate::error::Result<usize>> + Send {
            std::future::ready(Ok(0))
        }
    }

    struct CountingCleanupHandler;

    impl CleanupHandler for CountingCleanupHandler {
        fn cleanup(
            &self,
            _object_id: ObjectId,
            symbols: Vec<Symbol>,
        ) -> crate::error::Result<usize> {
            Ok(symbols.len())
        }

        fn name(&self) -> &'static str {
            "counting"
        }
    }

    /// Golden test capturing complete symbol cancellation protocol lifecycle.
    #[test]
    fn symbol_cancel_protocol_lifecycle_golden() {
        let mut rng = DetRng::new(42);
        let mut log = ProtocolLog::new();

        log.phase("token_creation");

        let obj1 = ObjectId::new(0, 0x90ab_cdef);
        let obj2 = ObjectId::new(0, 0xfedc_ba09);
        let budget = Budget::with_deadline_ns(500_000_000);

        let token1 = SymbolCancelToken::with_budget(obj1, budget, &mut rng);
        let token2 = SymbolCancelToken::new(obj2, &mut rng);

        log.record(
            "token1_created",
            &format!(
                "object_id={}, cleanup_budget={}",
                format_object_id(obj1),
                format_budget(token1.cleanup_budget())
            ),
        );
        log.record(
            "token2_created",
            &format!(
                "object_id={}, cleanup_budget={}",
                format_object_id(obj2),
                format_budget(token2.cleanup_budget())
            ),
        );

        log.phase("broadcaster_setup");

        let broadcaster = CancelBroadcaster::new(NullSink);
        broadcaster.register_token(token1.clone());
        broadcaster.register_token(token2.clone());

        log.record("registered_tokens", "2");

        log.phase("listener_registration");

        let listener_events = Arc::new(StdMutex::new(Vec::new()));
        let listener_events_for_token = Arc::clone(&listener_events);

        token1.add_listener(move |reason: &CancelReason, at: Time| {
            listener_events_for_token
                .lock()
                .expect("listener event mutex")
                .push(format!(
                    "kind={:?}, msg={:?}, at={}ns",
                    reason.kind(),
                    reason.message(),
                    at.as_nanos()
                ));
        });
        log.record("token1_listeners", "registered=1");

        log.phase("cancellation_initiation");

        let cancel_time = Time::from_nanos(1_000_000_000);
        let reason = CancelReason::timeout()
            .with_message("operation timeout")
            .with_timestamp(cancel_time);

        let cancel_msg1 = broadcaster.prepare_cancel(obj1, &reason, cancel_time);
        log.record("cancel_msg1", &format_cancel_message(&cancel_msg1));
        log.record(
            "token1_state",
            &format!(
                "cancelled={}, at_ns={:?}, reason_kind={:?}",
                token1.is_cancelled(),
                token1.cancelled_at().map(Time::as_nanos),
                token1.reason().map(|stored| format!("{:?}", stored.kind()))
            ),
        );

        log.phase("message_broadcasting");

        let forward_msg = broadcaster.receive_message(&cancel_msg1, cancel_time);
        log.record(
            "msg_dedup_check",
            &format!("forwarded={}", forward_msg.is_some()),
        );

        let duplicate_msg = broadcaster.receive_message(&cancel_msg1, cancel_time);
        log.record(
            "duplicate_rejected",
            &format!("forwarded={}", duplicate_msg.is_some()),
        );

        log.phase("cross_broadcaster_comm");

        let broadcaster2 = CancelBroadcaster::new(NullSink);
        let token3 = SymbolCancelToken::new(obj1, &mut rng);
        broadcaster2.register_token(token3.clone());

        let received_msg = broadcaster2.receive_message(&cancel_msg1, cancel_time);
        log.record(
            "cross_broadcaster_receive",
            &format!(
                "forwarded={}, token3_cancelled={}, token3_reason={:?}",
                received_msg.is_some(),
                token3.is_cancelled(),
                token3.reason().map(|stored| format!("{:?}", stored.kind()))
            ),
        );

        log.phase("hierarchical_cancellation");

        let child_token = token2.child(&mut rng);
        log.record(
            "child_created",
            &format!(
                "parent_object_id={}, child_object_id={}",
                format_object_id(token2.object_id()),
                format_object_id(child_token.object_id())
            ),
        );

        let reason2 = CancelReason::parent_cancelled()
            .with_message("parent cancelled")
            .with_timestamp(cancel_time);
        token2.cancel(&reason2, cancel_time);

        log.record(
            "hierarchical_result",
            &format!(
                "parent_cancelled={}, child_cancelled={}, child_reason={:?}",
                token2.is_cancelled(),
                child_token.is_cancelled(),
                child_token
                    .reason()
                    .map(|stored| format!("{:?}", stored.kind()))
            ),
        );

        log.phase("cleanup_coordination");

        let cleanup_coordinator = CleanupCoordinator::new();
        let cleanup_budget = Budget::new().with_poll_quota(100);

        cleanup_coordinator.register_handler(obj1, CountingCleanupHandler);
        cleanup_coordinator.register_pending(
            obj1,
            Symbol::new_for_test(0x90ab_cdef, 0, 0, &[1, 2, 3, 4]),
            cancel_time,
        );
        cleanup_coordinator.register_pending(
            obj1,
            Symbol::new_for_test(0x90ab_cdef, 0, 1, &[5, 6, 7, 8]),
            cancel_time,
        );
        cleanup_coordinator.register_pending(
            obj2,
            Symbol::new_for_test(0xfedc_ba09, 0, 0, &[9, 10]),
            cancel_time,
        );

        let cleanup_result = cleanup_coordinator.cleanup(obj1, Some(cleanup_budget));
        log.record("cleanup_obj1", &format_cleanup_result(&cleanup_result));

        let cleanup_result2 = cleanup_coordinator.cleanup(obj2, None);
        log.record("cleanup_obj2", &format_cleanup_result(&cleanup_result2));

        log.phase("final_statistics");

        let broadcaster1_metrics = broadcaster.metrics();
        log.record(
            "broadcaster1_stats",
            &format!(
                "initiated={}, duplicates={}, forwarded={}",
                broadcaster1_metrics.initiated,
                broadcaster1_metrics.duplicates,
                broadcaster1_metrics.forwarded
            ),
        );
        let broadcaster2_metrics = broadcaster2.metrics();
        log.record(
            "broadcaster2_stats",
            &format!(
                "initiated={}, duplicates={}, forwarded={}",
                broadcaster2_metrics.initiated,
                broadcaster2_metrics.duplicates,
                broadcaster2_metrics.forwarded
            ),
        );
        log.record(
            "listener_events",
            &listener_events
                .lock()
                .expect("listener event mutex")
                .first()
                .cloned()
                .unwrap_or_else(|| "none".to_string()),
        );
        log.record(
            "panic_stats",
            &format!(
                "token1={}, token2={}, child={}",
                token1.listener_panic_count(),
                token2.listener_panic_count(),
                child_token.listener_panic_count()
            ),
        );

        assert_eq!(
            log.to_string(),
            EXPECTED_PROTOCOL_LIFECYCLE,
            "Protocol lifecycle golden mismatch"
        );
    }

    /// Structured logging for protocol lifecycle capture.
    struct ProtocolLog {
        entries: Vec<(String, String)>,
        current_phase: Option<String>,
    }

    impl ProtocolLog {
        fn new() -> Self {
            Self {
                entries: Vec::new(),
                current_phase: None,
            }
        }

        fn phase(&mut self, name: &str) {
            self.current_phase = Some(name.to_string());
            self.entries.push(("phase".to_string(), name.to_string()));
        }

        fn record(&mut self, key: &str, value: &str) {
            let prefixed_key = match &self.current_phase {
                Some(phase) => format!("{}::{}", phase, key),
                None => key.to_string(),
            };
            self.entries.push((prefixed_key, value.to_string()));
        }
    }

    impl std::fmt::Display for ProtocolLog {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            for (key, value) in &self.entries {
                writeln!(f, "{}={}", key, value)?;
            }
            Ok(())
        }
    }

    fn format_object_id(object_id: ObjectId) -> String {
        format!("{:016x}{:016x}", object_id.high(), object_id.low())
    }

    fn format_budget(budget: Budget) -> String {
        format!(
            "deadline_ns={:?},poll_quota={},priority={}",
            budget.deadline.map(Time::as_nanos),
            budget.poll_quota,
            budget.priority
        )
    }

    fn format_cancel_message(msg: &CancelMessage) -> String {
        format!(
            "object_id={}, kind={:?}, at={}ns, seq={}",
            format_object_id(msg.object_id()),
            msg.kind(),
            msg.initiated_at().as_nanos(),
            msg.sequence()
        )
    }

    fn format_cleanup_result(result: &CleanupResult) -> String {
        format!(
            "cleaned={}, bytes={}, within_budget={}, completed={}, handlers={}, errors={}",
            result.symbols_cleaned,
            result.bytes_freed,
            result.within_budget,
            result.completed,
            result.handlers_run.len(),
            result.handler_errors.len()
        )
    }

    /// Test additional edge cases for comprehensive coverage.
    #[test]
    fn symbol_cancel_edge_cases_golden() {
        let mut rng = DetRng::new(99);
        let mut log = ProtocolLog::new();

        log.phase("nonexistent_object");

        let broadcaster = CancelBroadcaster::new(NullSink);
        let nonexistent_obj = ObjectId::NIL;
        let reason = CancelReason::user("test");
        let time = Time::from_nanos(2_000_000_000);

        let msg = broadcaster.prepare_cancel(nonexistent_obj, &reason, time);
        log.record("nonexistent_cancel", &format_cancel_message(&msg));

        log.phase("multiple_cancellations");

        let obj = ObjectId::new(0xaaaa_aaaa, 0xbbbb_bbbb);
        let token = SymbolCancelToken::new(obj, &mut rng);

        let reason1 = CancelReason::user("first").with_timestamp(time);
        let reason2 = CancelReason::timeout()
            .with_message("second")
            .with_timestamp(time);

        let result1 = token.cancel(&reason1, time);
        let result2 = token.cancel(&reason2, time);

        log.record("first_cancel", &format!("success={}", result1));
        log.record("second_cancel", &format!("success={}", result2));
        log.record(
            "final_reason",
            &format!(
                "{:?}",
                token.reason().map(|stored| format!("{:?}", stored.kind()))
            ),
        );

        log.phase("empty_broadcaster");

        let empty_broadcaster = CancelBroadcaster::new(NullSink);
        let metrics = empty_broadcaster.metrics();
        log.record(
            "empty_stats",
            &format!(
                "initiated={}, duplicates={}, forwarded={}, pending_retries={}",
                metrics.initiated, metrics.duplicates, metrics.forwarded, metrics.pending_retries
            ),
        );

        assert_eq!(
            log.to_string(),
            EXPECTED_EDGE_CASES,
            "Edge cases golden mismatch"
        );
    }
}