asupersync 0.3.1

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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
#![allow(clippy::all)]
//! Metamorphic Testing: Bulkhead isolation under burst load
//!
//! This module implements metamorphic relations (MRs) to verify that bulkhead
//! resource isolation and concurrency limiting maintains critical invariants
//! under high contention, burst load, and failure conditions.
//!
//! # Metamorphic Relations
//!
//! - **MR1 (Isolation Invariant)**: N bulkheads with N workers each handle
//!   N*N concurrent requests with no cross-contamination
//! - **MR2 (Rejection Accuracy)**: Rejection count matches queue-full events
//!   exactly - no phantom rejections or missed overflows
//! - **MR3 (Cancel Propagation)**: Cancel of outer scope cancels all bulkhead
//!   in-flight operations consistently
//! - **MR4 (Metrics Accuracy)**: Metrics remain accurate under high contention
//!   and concurrent access patterns
//! - **MR5 (Deterministic Behavior)**: Bulkhead behavior is deterministic
//!   under LabRuntime with controlled scheduling
//!
//! # Property Coverage
//!
//! These MRs ensure that:
//! - Resource isolation prevents cascading failures across bulkheads
//! - Queue management accurately tracks and limits concurrent operations
//! - Cancellation protocol preserves bulkhead invariants
//! - Metrics provide reliable observability under all conditions
//! - Behavior is reproducible and testable in controlled environments
//!
//! # Testing Strategy
//!
//! Uses property-based testing with configurable burst patterns, controlled
//! concurrency levels, and systematic exploration of failure injection
//! scenarios to verify invariants hold across all execution paths.

#![allow(dead_code)]

use crate::combinator::bulkhead::{Bulkhead, BulkheadError, BulkheadPolicy};
use crate::types::Time;
use crate::util::DetRng;
use proptest::prelude::*;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};
use std::time::Duration;

// ============================================================================
// Test Infrastructure
// ============================================================================

/// Test work unit that can be configured for various load patterns
#[derive(Debug, Clone)]
struct TestWorkUnit {
    /// Unique identifier for this work unit
    id: u64,
    /// Weight (permits required)
    weight: u32,
    /// Expected processing time (simulated)
    processing_time_ms: u64,
    /// Whether this work should be cancelled
    should_cancel: bool,
    /// Priority for ordering (lower = higher priority)
    priority: u32,
}

impl TestWorkUnit {
    fn new(id: u64, weight: u32, processing_time_ms: u64) -> Self {
        Self {
            id,
            weight,
            processing_time_ms,
            should_cancel: false,
            priority: 0,
        }
    }

    fn with_priority(mut self, priority: u32) -> Self {
        self.priority = priority;
        self
    }

    fn with_cancel(mut self) -> Self {
        self.should_cancel = true;
        self
    }
}

/// Global state tracker for bulkhead operations across all instances
#[derive(Debug, Default)]
struct GlobalBulkheadState {
    /// Work units processed per bulkhead
    processed_per_bulkhead: parking_lot::Mutex<HashMap<String, Vec<u64>>>,
    /// Work units rejected per bulkhead
    rejected_per_bulkhead: parking_lot::Mutex<HashMap<String, Vec<u64>>>,
    /// Work units cancelled per bulkhead
    cancelled_per_bulkhead: parking_lot::Mutex<HashMap<String, Vec<u64>>>,
    /// Total permit acquisitions across all bulkheads
    total_acquisitions: AtomicU64,
    /// Total permit releases across all bulkheads
    total_releases: AtomicU64,
    /// Peak concurrent workers across all bulkheads
    peak_concurrent_workers: AtomicU32,
    /// Cross-contamination detection (work on wrong bulkhead)
    contamination_events: AtomicU32,
    /// Queue overflow events
    queue_overflow_events: AtomicU32,
    /// Cancellation events
    cancellation_events: AtomicU32,
}

impl GlobalBulkheadState {
    fn new() -> Arc<Self> {
        Arc::new(Self::default())
    }

    fn record_processed(&self, bulkhead_name: &str, work_id: u64) {
        self.processed_per_bulkhead
            .lock()
            .entry(bulkhead_name.to_string())
            .or_default()
            .push(work_id);
    }

    fn record_rejected(&self, bulkhead_name: &str, work_id: u64) {
        self.rejected_per_bulkhead
            .lock()
            .entry(bulkhead_name.to_string())
            .or_default()
            .push(work_id);
        self.queue_overflow_events.fetch_add(1, Ordering::SeqCst);
    }

    fn record_cancelled(&self, bulkhead_name: &str, work_id: u64) {
        self.cancelled_per_bulkhead
            .lock()
            .entry(bulkhead_name.to_string())
            .or_default()
            .push(work_id);
        self.cancellation_events.fetch_add(1, Ordering::SeqCst);
    }

    fn record_acquisition(&self) {
        self.total_acquisitions.fetch_add(1, Ordering::SeqCst);
    }

    fn record_release(&self) {
        self.total_releases.fetch_add(1, Ordering::SeqCst);
    }

    fn record_contamination(&self) {
        self.contamination_events.fetch_add(1, Ordering::SeqCst);
    }

    fn update_peak_workers(&self, current: u32) {
        let mut peak = self.peak_concurrent_workers.load(Ordering::SeqCst);
        while current > peak {
            match self.peak_concurrent_workers.compare_exchange_weak(
                peak,
                current,
                Ordering::SeqCst,
                Ordering::SeqCst,
            ) {
                Ok(_) => break,
                Err(actual) => peak = actual,
            }
        }
    }

    /// Check for cross-contamination between bulkheads
    fn verify_isolation(&self) -> bool {
        self.contamination_events.load(Ordering::SeqCst) == 0
    }

    /// Get total processed work across all bulkheads
    fn total_processed(&self) -> usize {
        self.processed_per_bulkhead
            .lock()
            .values()
            .map(|v| v.len())
            .sum()
    }

    /// Get total rejected work across all bulkheads
    fn total_rejected(&self) -> usize {
        self.rejected_per_bulkhead
            .lock()
            .values()
            .map(|v| v.len())
            .sum()
    }

    /// Get stats for a specific bulkhead
    fn bulkhead_stats(&self, name: &str) -> BulkheadStats {
        let processed = self
            .processed_per_bulkhead
            .lock()
            .get(name)
            .map_or(0, |v| v.len());
        let rejected = self
            .rejected_per_bulkhead
            .lock()
            .get(name)
            .map_or(0, |v| v.len());
        let cancelled = self
            .cancelled_per_bulkhead
            .lock()
            .get(name)
            .map_or(0, |v| v.len());

        BulkheadStats {
            processed,
            rejected,
            cancelled,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
struct BulkheadStats {
    processed: usize,
    rejected: usize,
    cancelled: usize,
}

/// Configuration for bulkhead burst testing
#[derive(Debug, Clone)]
struct BurstTestConfig {
    /// Number of bulkheads to create
    bulkhead_count: u32,
    /// Workers per bulkhead
    workers_per_bulkhead: u32,
    /// Queue size per bulkhead
    queue_size_per_bulkhead: u32,
    /// Total work units to submit
    total_work_units: u32,
    /// Work distribution pattern
    work_pattern: WorkPattern,
    /// Cancellation ratio (0.0 = no cancellation, 1.0 = cancel all)
    cancellation_ratio: f32,
}

#[derive(Debug, Clone)]
enum WorkPattern {
    /// Distribute work evenly across bulkheads
    Uniform,
    /// Send most work to first bulkhead (burst load)
    Burst { burst_ratio: f32 },
    /// Random distribution
    Random { seed: u64 },
}

/// Maximum limits to prevent timeouts and resource exhaustion
const MAX_BULKHEADS: u32 = 8;
const MAX_WORKERS_PER_BULKHEAD: u32 = 16;
const MAX_WORK_UNITS: u32 = 200;
const MAX_QUEUE_SIZE: u32 = 50;

// ============================================================================
// Metamorphic Relation Tests
// ============================================================================

/// **MR1: Isolation Invariant**
///
/// N bulkheads with N workers each handle N*N concurrent requests with no cross-contamination.
/// Verifies that bulkheads provide proper resource isolation.
fn mr1_isolation_invariant(
    bulkhead_count: u32,
    workers_per_bulkhead: u32,
    total_work_units: u32,
    _seed: u64,
) -> bool {
    let global_state = GlobalBulkheadState::new();
    let mut bulkheads: Vec<(String, Arc<Bulkhead>)> = Vec::new();

    // Create N bulkheads with N workers each
    for i in 0..bulkhead_count {
        let name = format!("bulkhead_{}", i);
        let policy = BulkheadPolicy {
            name: name.clone(),
            max_concurrent: workers_per_bulkhead,
            max_queue: MAX_QUEUE_SIZE,
            queue_timeout: Duration::from_millis(100),
            weighted: false,
            on_full: None,
        };
        let bulkhead = Arc::new(Bulkhead::new(policy));
        bulkheads.push((name, bulkhead));
    }

    // Generate work units distributed across bulkheads
    let _rng = DetRng::new(_seed);
    let mut work_assignments: HashMap<String, Vec<TestWorkUnit>> = HashMap::new();

    for work_id in 0..total_work_units {
        let target_bulkhead = work_id as usize % bulkheads.len();
        let bulkhead_name = &bulkheads[target_bulkhead].0;
        let work_unit = TestWorkUnit::new(work_id as u64, 1, 10);

        work_assignments
            .entry(bulkhead_name.clone())
            .or_default()
            .push(work_unit);
    }

    // Execute work on each bulkhead
    for (bulkhead_name, bulkhead) in &bulkheads {
        if let Some(work_units) = work_assignments.get(bulkhead_name) {
            for work_unit in work_units {
                match bulkhead.try_acquire(work_unit.weight) {
                    Some(permit) => {
                        // Simulate work execution
                        global_state.record_acquisition();
                        global_state.record_processed(bulkhead_name, work_unit.id);

                        // Verify this work is processed on the correct bulkhead
                        if !bulkhead_name
                            .contains(&format!("_{}", work_unit.id % bulkhead_count as u64))
                        {
                            // This is actually expected for uniform distribution
                            // The contamination check is about work being executed on wrong bulkhead
                            // during permit acquisition, which shouldn't happen
                        }

                        // Release permit immediately for testing
                        global_state.record_release();
                        permit.release();
                    }
                    None => {
                        // Bulkhead was full - record rejection
                        global_state.record_rejected(bulkhead_name, work_unit.id);
                    }
                }
            }
        }
    }

    // **MR1 Verification**: Verify isolation (no cross-contamination)
    let isolation_maintained = global_state.verify_isolation();

    // **Additional verification**: Check that each bulkhead processed work independently
    let mut all_processed_ids = std::collections::HashSet::new();
    for (bulkhead_name, _) in &bulkheads {
        let processed = global_state.processed_per_bulkhead.lock();
        if let Some(ids) = processed.get(bulkhead_name) {
            for &id in ids {
                if !all_processed_ids.insert(id) {
                    // Same work unit processed by multiple bulkheads - contamination!
                    return false;
                }
            }
        }
    }

    crate::assert_with_log!(
        isolation_maintained,
        "MR1: Isolation invariant maintained",
        true,
        isolation_maintained
    );

    isolation_maintained
}

/// **MR2: Rejection Accuracy**
///
/// Rejection count matches queue-full events exactly.
/// Verifies that bulkhead rejection tracking is accurate.
fn mr2_rejection_accuracy(
    max_workers: u32,
    queue_size: u32,
    work_burst_size: u32,
    _seed: u64,
) -> bool {
    let global_state = GlobalBulkheadState::new();

    let policy = BulkheadPolicy {
        name: "test_bulkhead".to_string(),
        max_concurrent: max_workers,
        max_queue: queue_size,
        queue_timeout: Duration::from_millis(50),
        weighted: false,
        on_full: None,
    };
    let bulkhead = Arc::new(Bulkhead::new(policy));

    // Submit burst of work that exceeds capacity + queue
    let total_capacity = max_workers + queue_size;
    let expected_rejections = work_burst_size.saturating_sub(total_capacity);

    let mut acquired_permits = Vec::new();
    let mut actual_rejections = 0;

    for work_id in 0..work_burst_size {
        match bulkhead.try_acquire(1) {
            Some(permit) => {
                global_state.record_acquisition();
                acquired_permits.push(permit);
            }
            None => {
                // Try to enqueue
                let now = Time::from_millis(0);
                match bulkhead.enqueue(1, now) {
                    Ok(_entry_id) => {
                        // Successfully queued
                    }
                    Err(BulkheadError::Full | BulkheadError::QueueFull) => {
                        // Capacity exhausted - count as rejection
                        actual_rejections += 1;
                        global_state.record_rejected("test_bulkhead", work_id as u64);
                    }
                    Err(_other) => {
                        // Other errors (timeout, etc.) not counted as capacity rejections
                    }
                }
            }
        }
    }

    // Get metrics and verify rejection count
    let metrics = bulkhead.metrics();
    let metrics_rejections = metrics.total_rejected;

    // **MR2 Verification**: Rejection counts should match exactly
    let accuracy_maintained = actual_rejections == expected_rejections as u32
        && metrics_rejections == actual_rejections as u64;

    crate::assert_with_log!(
        accuracy_maintained,
        "MR2: Rejection accuracy maintained",
        true,
        accuracy_maintained
    );

    accuracy_maintained
}

/// **MR3: Cancel Propagation**
///
/// Cancel of outer scope cancels all bulkhead in-flight operations.
/// Verifies that cancellation protocol works correctly with bulkheads.
fn mr3_cancel_propagation(worker_count: u32, in_flight_count: u32, _seed: u64) -> bool {
    let global_state = GlobalBulkheadState::new();

    let policy = BulkheadPolicy {
        name: "cancel_test".to_string(),
        max_concurrent: worker_count,
        max_queue: in_flight_count,
        queue_timeout: Duration::from_millis(1000),
        weighted: false,
        on_full: None,
    };
    let bulkhead = Arc::new(Bulkhead::new(policy));
    let cancel_flag = Arc::new(AtomicBool::new(false));

    // Start multiple in-flight operations
    let mut permits = Vec::new();
    let mut queue_entries = Vec::new();

    // Fill up the workers
    for _work_id in 0..worker_count {
        if let Some(permit) = bulkhead.try_acquire(1) {
            global_state.record_acquisition();
            permits.push(permit);
        }
    }

    // Queue additional work
    let now = Time::from_millis(0);
    for _work_id in worker_count..worker_count + in_flight_count {
        match bulkhead.enqueue(1, now) {
            Ok(entry_id) => {
                queue_entries.push(entry_id);
            }
            Err(_) => {
                // Queue full or other error
                break;
            }
        }
    }

    let initial_metrics = bulkhead.metrics();
    let _initial_active = initial_metrics.active_permits;
    let initial_queued = initial_metrics.queue_depth;

    // Trigger cancellation
    cancel_flag.store(true, Ordering::SeqCst);

    // Cancel all queued entries
    for entry_id in &queue_entries {
        bulkhead.cancel_entry(*entry_id, now);
        global_state.record_cancelled("cancel_test", *entry_id);
    }

    // Process the queue to handle cancellations
    let _processed_after_cancel = bulkhead.process_queue(now);
    let final_metrics = bulkhead.metrics();

    // **MR3 Verification**: Cancellation should affect queued operations
    let cancellations_processed = global_state.cancellation_events.load(Ordering::SeqCst) > 0;
    let queue_reduced = final_metrics.queue_depth <= initial_queued;
    let propagation_correct = cancellations_processed && queue_reduced;

    crate::assert_with_log!(
        propagation_correct,
        "MR3: Cancel propagation correct",
        true,
        propagation_correct
    );

    // Clean up permits
    for permit in permits {
        permit.release();
    }

    propagation_correct
}

/// **MR4: Metrics Accuracy**
///
/// Metrics remain accurate under high contention and concurrent access.
/// Verifies that bulkhead metrics are reliable for observability.
fn mr4_metrics_accuracy(
    worker_count: u32,
    operation_count: u32,
    concurrency_level: u32,
    _seed: u64,
) -> bool {
    let global_state = GlobalBulkheadState::new();

    let policy = BulkheadPolicy {
        name: "metrics_test".to_string(),
        max_concurrent: worker_count,
        max_queue: operation_count,
        queue_timeout: Duration::from_millis(100),
        weighted: false,
        on_full: None,
    };
    let bulkhead = Arc::new(Bulkhead::new(policy));

    // Execute operations with controlled concurrency
    let mut executed_count = 0;
    let mut rejected_count = 0;
    let mut permits = Vec::new();

    for op_id in 0..operation_count {
        match bulkhead.try_acquire(1) {
            Some(permit) => {
                executed_count += 1;
                global_state.record_acquisition();
                permits.push(permit);

                // Release some permits to create churn
                if permits.len() >= concurrency_level as usize {
                    if let Some(old_permit) = permits.pop() {
                        old_permit.release();
                        global_state.record_release();
                    }
                }
            }
            None => {
                rejected_count += 1;
                global_state.record_rejected("metrics_test", op_id as u64);
            }
        }
    }

    // Release remaining permits
    for permit in permits {
        permit.release();
        global_state.record_release();
    }

    // Verify metrics accuracy
    let final_metrics = bulkhead.metrics();
    let global_acquisitions = global_state.total_acquisitions.load(Ordering::SeqCst);
    let global_releases = global_state.total_releases.load(Ordering::SeqCst);

    // **MR4 Verification**: Metrics should accurately reflect operations.
    //
    // Note: `total_rejected` on the bulkhead tracks `enqueue` rejections
    // (queue full or weight > capacity), not fast-path `try_acquire`
    // misses. Local `rejected_count` above counts the latter, so we do
    // not compare them directly. Instead we verify that the rejected
    // count captured here is at most the number of observed misses and
    // that executed + rejected never exceeds the total attempts.
    let executed_matches = final_metrics.total_executed == executed_count as u64;
    let rejected_consistent = final_metrics.total_rejected <= rejected_count as u64
        && executed_count + rejected_count <= operation_count;
    let permit_balance = global_acquisitions == global_releases; // All released
    let final_permits_correct = final_metrics.active_permits == 0; // All released

    let accuracy_maintained =
        executed_matches && rejected_consistent && permit_balance && final_permits_correct;

    crate::assert_with_log!(
        accuracy_maintained,
        "MR4: Metrics accuracy maintained",
        true,
        accuracy_maintained
    );

    accuracy_maintained
}

/// **MR5: Deterministic Behavior**
///
/// Bulkhead behavior is deterministic under controlled scheduling.
/// Verifies reproducibility for testing and debugging.
fn mr5_deterministic_behavior(
    worker_count: u32,
    work_sequence: Vec<u32>, // Sequence of work weights
    _seed: u64,
) -> bool {
    // Run the same sequence twice and verify identical results
    let result1 = run_deterministic_sequence(worker_count, &work_sequence, _seed);
    let result2 = run_deterministic_sequence(worker_count, &work_sequence, _seed);

    // **MR5 Verification**: Results should be identical
    let determinism_maintained = result1 == result2;

    crate::assert_with_log!(
        determinism_maintained,
        "MR5: Deterministic behavior maintained",
        true,
        determinism_maintained
    );

    determinism_maintained
}

/// Helper function to run a deterministic sequence
fn run_deterministic_sequence(
    worker_count: u32,
    work_sequence: &[u32],
    _seed: u64,
) -> DeterministicResult {
    let _global_state = GlobalBulkheadState::new();
    let _rng = DetRng::new(_seed);

    let policy = BulkheadPolicy {
        name: "deterministic_test".to_string(),
        max_concurrent: worker_count,
        max_queue: 20,
        queue_timeout: Duration::from_millis(100),
        weighted: true,
        on_full: None,
    };
    let bulkhead = Arc::new(Bulkhead::new(policy));

    let mut results = Vec::new();
    let mut permits = Vec::new();

    for (i, &weight) in work_sequence.iter().enumerate() {
        let op_result = match bulkhead.try_acquire(weight) {
            Some(permit) => {
                permits.push(permit);
                OperationResult::Acquired { weight }
            }
            None => OperationResult::Rejected { weight },
        };
        results.push(op_result);

        // Deterministically release some permits based on sequence position
        if i % 3 == 2 && !permits.is_empty() {
            permits.remove(0).release();
        }
    }

    // Release remaining permits
    for permit in permits {
        permit.release();
    }

    let final_metrics = bulkhead.metrics();
    DeterministicResult {
        operations: results,
        final_executed: final_metrics.total_executed,
        final_rejected: final_metrics.total_rejected,
        final_active: final_metrics.active_permits,
    }
}

#[derive(Debug, Clone, PartialEq)]
struct DeterministicResult {
    operations: Vec<OperationResult>,
    final_executed: u64,
    final_rejected: u64,
    final_active: u32,
}

#[derive(Debug, Clone, PartialEq)]
enum OperationResult {
    Acquired { weight: u32 },
    Rejected { weight: u32 },
}

// ============================================================================
// Property-Based Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    proptest! {
        /// Test MR1: Isolation Invariant
        #[test]
        fn test_mr1_isolation_invariant(
            bulkhead_count in 1u32..=4,
            workers_per_bulkhead in 1u32..=8,
            total_work_units in 10u32..=40,
            seed in any::<u64>(),
        ) {
            prop_assert!(mr1_isolation_invariant(
                bulkhead_count,
                workers_per_bulkhead,
                total_work_units,
                seed
            ));
        }

        /// Test MR2: Rejection Accuracy
        #[test]
        fn test_mr2_rejection_accuracy(
            max_workers in 1u32..=8,
            queue_size in 1u32..=10,
            work_burst_size in 1u32..=30,
            seed in any::<u64>(),
        ) {
            prop_assert!(mr2_rejection_accuracy(
                max_workers,
                queue_size,
                work_burst_size,
                seed
            ));
        }

        /// Test MR3: Cancel Propagation
        #[test]
        fn test_mr3_cancel_propagation(
            worker_count in 1u32..=6,
            in_flight_count in 1u32..=12,
            seed in any::<u64>(),
        ) {
            prop_assert!(mr3_cancel_propagation(
                worker_count,
                in_flight_count,
                seed
            ));
        }

        /// Test MR4: Metrics Accuracy
        #[test]
        fn test_mr4_metrics_accuracy(
            worker_count in 1u32..=8,
            operation_count in 5u32..=25,
            concurrency_level in 1u32..=5,
            seed in any::<u64>(),
        ) {
            prop_assert!(mr4_metrics_accuracy(
                worker_count,
                operation_count,
                concurrency_level,
                seed
            ));
        }

        /// Test MR5: Deterministic Behavior
        #[test]
        fn test_mr5_deterministic_behavior(
            worker_count in 1u32..=6,
            work_sequence in prop::collection::vec(1u32..=3, 5..15),
            seed in any::<u64>(),
        ) {
            prop_assert!(mr5_deterministic_behavior(
                worker_count,
                work_sequence,
                seed
            ));
        }
    }

    /// Comprehensive integration test combining all MRs
    #[test]
    fn test_bulkhead_metamorphic_integration() {
        let _global_state = GlobalBulkheadState::new();

        // Test scenario: Multiple bulkheads under mixed load
        let bulkhead_count = 3;
        let workers_per_bulkhead = 4;
        let total_work = 50;
        let seed = 12345;

        // Test all MRs in sequence
        assert!(mr1_isolation_invariant(
            bulkhead_count,
            workers_per_bulkhead,
            total_work,
            seed
        ));
        assert!(mr2_rejection_accuracy(4, 8, 20, seed));
        assert!(mr3_cancel_propagation(6, 10, seed));
        assert!(mr4_metrics_accuracy(5, 20, 3, seed));
        assert!(mr5_deterministic_behavior(4, vec![1, 2, 1, 3, 1, 2], seed));
    }

    /// Test edge cases and boundary conditions
    #[test]
    fn test_bulkhead_edge_cases() {
        // Test minimum configuration
        assert!(mr1_isolation_invariant(1, 1, 2, 1111));
        assert!(mr2_rejection_accuracy(1, 1, 5, 2222));

        // Test maximum reasonable configuration
        assert!(mr1_isolation_invariant(4, 8, 40, 3333));
        assert!(mr2_rejection_accuracy(8, 10, 30, 4444));

        // Test empty work sequence
        assert!(mr5_deterministic_behavior(2, vec![], 5555));

        // Test single operation sequence
        assert!(mr5_deterministic_behavior(3, vec![1], 6666));

        // Test all operations rejected scenario
        assert!(mr2_rejection_accuracy(1, 0, 10, 7777)); // No queue space
    }

    /// Test burst load patterns
    #[test]
    fn test_burst_load_patterns() {
        let config = BurstTestConfig {
            bulkhead_count: 2,
            workers_per_bulkhead: 3,
            queue_size_per_bulkhead: 5,
            total_work_units: 20,
            work_pattern: WorkPattern::Burst { burst_ratio: 0.8 },
            cancellation_ratio: 0.1,
        };

        // Test burst scenario with the first bulkhead getting 80% of work
        assert!(mr1_isolation_invariant(
            config.bulkhead_count,
            config.workers_per_bulkhead,
            config.total_work_units,
            8888
        ));
    }

    /// Test cancellation scenarios
    #[test]
    fn test_cancellation_scenarios() {
        // High cancellation rate
        assert!(mr3_cancel_propagation(4, 8, 9999));

        // Low cancellation rate
        assert!(mr3_cancel_propagation(6, 2, 1010));

        // All operations cancelled
        assert!(mr3_cancel_propagation(1, 5, 1212));
    }
}