breaker-machines 0.7.6

High-performance circuit breaker with fallback support and rate-based thresholds
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
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
//! Circuit breaker implementation using state machines
//!
//! This module provides a complete circuit breaker with state management.

use crate::{
    StorageBackend, bulkhead::BulkheadSemaphore, callbacks::Callbacks,
    classifier::FailureClassifier, errors::CircuitError,
};
use state_machines::state_machine;
use std::sync::Arc;

/// Circuit breaker configuration
#[derive(Debug, Clone)]
pub struct Config {
    /// Number of failures required to open the circuit (absolute count)
    /// If None, only rate-based threshold is used
    pub failure_threshold: Option<usize>,

    /// Failure rate threshold (0.0-1.0) - percentage of failures to open circuit
    /// If None, only absolute count threshold is used
    pub failure_rate_threshold: Option<f64>,

    /// Minimum number of calls before rate-based threshold is evaluated
    pub minimum_calls: usize,

    /// Time window in seconds for counting failures
    pub failure_window_secs: f64,

    /// Timeout in seconds before transitioning from Open to HalfOpen
    pub half_open_timeout_secs: f64,

    /// Number of successes required in HalfOpen to close the circuit
    pub success_threshold: usize,

    /// Jitter factor for half_open_timeout (0.0 = no jitter, 1.0 = full jitter)
    /// Uses chrono-machines formula: timeout * (1 - jitter + rand * jitter)
    pub jitter_factor: f64,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            failure_threshold: Some(5),
            failure_rate_threshold: None,
            minimum_calls: 20,
            failure_window_secs: 60.0,
            half_open_timeout_secs: 30.0,
            success_threshold: 2,
            jitter_factor: 0.0,
        }
    }
}

/// Context provided to fallback closures when circuit is open
#[derive(Debug, Clone)]
pub struct FallbackContext {
    /// Circuit name
    pub circuit_name: String,
    /// Timestamp when circuit opened
    pub opened_at: f64,
    /// Current circuit state
    pub state: &'static str,
}

/// Type alias for fallback function
pub type FallbackFn<T, E> = Box<dyn FnOnce(&FallbackContext) -> Result<T, E> + Send>;

/// Options for circuit breaker calls
pub struct CallOptions<T, E> {
    /// Optional fallback function called when circuit is open
    pub fallback: Option<FallbackFn<T, E>>,
}

impl<T, E> Default for CallOptions<T, E> {
    fn default() -> Self {
        Self { fallback: None }
    }
}

impl<T, E> CallOptions<T, E> {
    /// Create new call options with no fallback
    pub fn new() -> Self {
        Self::default()
    }

    /// Set a fallback function
    pub fn with_fallback<F>(mut self, f: F) -> Self
    where
        F: FnOnce(&FallbackContext) -> Result<T, E> + Send + 'static,
    {
        self.fallback = Some(Box::new(f));
        self
    }
}

/// Type alias for callable function
pub type CallableFn<T, E> = Box<dyn FnOnce() -> Result<T, E>>;

/// Trait for converting into CallOptions - allows flexible call() API
pub trait IntoCallOptions<T, E> {
    fn into_call_options(self) -> (CallableFn<T, E>, CallOptions<T, E>);
}

/// Implement for plain closures (backward compatibility)
impl<T, E, F> IntoCallOptions<T, E> for F
where
    F: FnOnce() -> Result<T, E> + 'static,
{
    fn into_call_options(self) -> (Box<dyn FnOnce() -> Result<T, E>>, CallOptions<T, E>) {
        (Box::new(self), CallOptions::default())
    }
}

/// Implement for (closure, CallOptions) tuple
impl<T, E, F> IntoCallOptions<T, E> for (F, CallOptions<T, E>)
where
    F: FnOnce() -> Result<T, E> + 'static,
{
    fn into_call_options(self) -> (Box<dyn FnOnce() -> Result<T, E>>, CallOptions<T, E>) {
        (Box::new(self.0), self.1)
    }
}

/// Circuit breaker context - shared data across all states
#[derive(Clone)]
pub struct CircuitContext {
    pub name: String,
    pub config: Config,
    pub storage: Arc<dyn StorageBackend>,
    pub failure_classifier: Option<Arc<dyn FailureClassifier>>,
    pub bulkhead: Option<Arc<BulkheadSemaphore>>,
}

impl Default for CircuitContext {
    fn default() -> Self {
        Self {
            name: String::new(),
            config: Config::default(),
            storage: Arc::new(crate::MemoryStorage::new()),
            failure_classifier: None,
            bulkhead: None,
        }
    }
}

impl std::fmt::Debug for CircuitContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CircuitContext")
            .field("name", &self.name)
            .field("config", &self.config)
            .field("storage", &"<dyn StorageBackend>")
            .field(
                "failure_classifier",
                &self
                    .failure_classifier
                    .as_ref()
                    .map(|_| "<dyn FailureClassifier>"),
            )
            .field("bulkhead", &self.bulkhead)
            .finish()
    }
}

/// Data specific to the Open state
#[derive(Debug, Clone, Default)]
pub struct OpenData {
    pub opened_at: f64,
}

/// Data specific to the HalfOpen state
#[derive(Debug, Clone, Default)]
pub struct HalfOpenData {
    pub consecutive_successes: usize,
}

// Define the circuit breaker state machine with dynamic mode
state_machine! {
    name: Circuit,
    context: CircuitContext,
    dynamic: true,  // Enable dynamic mode for runtime state transitions

    initial: Closed,
    states: [
        Closed,
        Open(OpenData),
        HalfOpen(HalfOpenData),
    ],
    events {
        trip {
            guards: [should_open],
            transition: { from: [Closed, HalfOpen], to: Open }
        }
        attempt_reset {
            guards: [timeout_elapsed],
            transition: { from: Open, to: HalfOpen }
        }
        close {
            guards: [should_close],
            transition: { from: HalfOpen, to: Closed }
        }
    }
}

// Guards for dynamic mode - implemented on typestate machines
impl Circuit<Closed> {
    /// Check if failure threshold is exceeded (absolute count or rate-based)
    fn should_open(&self, ctx: &CircuitContext) -> bool {
        let failures = ctx
            .storage
            .failure_count(&ctx.name, ctx.config.failure_window_secs);

        // Check absolute count threshold
        if let Some(threshold) = ctx.config.failure_threshold
            && failures >= threshold
        {
            return true;
        }

        // Check rate-based threshold
        if let Some(rate_threshold) = ctx.config.failure_rate_threshold {
            let successes = ctx
                .storage
                .success_count(&ctx.name, ctx.config.failure_window_secs);
            let total = failures + successes;

            // Only evaluate rate if we have minimum calls
            if total >= ctx.config.minimum_calls {
                let failure_rate = if total > 0 {
                    failures as f64 / total as f64
                } else {
                    0.0
                };

                if failure_rate >= rate_threshold {
                    return true;
                }
            }
        }

        false
    }
}

impl Circuit<HalfOpen> {
    /// Check if failure threshold is exceeded (absolute count or rate-based)
    fn should_open(&self, ctx: &CircuitContext) -> bool {
        let failures = ctx
            .storage
            .failure_count(&ctx.name, ctx.config.failure_window_secs);

        // Check absolute count threshold
        if let Some(threshold) = ctx.config.failure_threshold
            && failures >= threshold
        {
            return true;
        }

        // Check rate-based threshold
        if let Some(rate_threshold) = ctx.config.failure_rate_threshold {
            let successes = ctx
                .storage
                .success_count(&ctx.name, ctx.config.failure_window_secs);
            let total = failures + successes;

            // Only evaluate rate if we have minimum calls
            if total >= ctx.config.minimum_calls {
                let failure_rate = if total > 0 {
                    failures as f64 / total as f64
                } else {
                    0.0
                };

                if failure_rate >= rate_threshold {
                    return true;
                }
            }
        }

        false
    }

    /// Check if enough successes to close circuit
    fn should_close(&self, ctx: &CircuitContext) -> bool {
        let data = self
            .state_data_half_open()
            .expect("HalfOpen state must have data");
        data.consecutive_successes >= ctx.config.success_threshold
    }
}

impl Circuit<Open> {
    /// Check if timeout has elapsed for Open -> HalfOpen transition
    fn timeout_elapsed(&self, ctx: &CircuitContext) -> bool {
        let data = self.state_data_open().expect("Open state must have data");
        let current_time = ctx.storage.monotonic_time();
        let elapsed = current_time - data.opened_at;

        // Apply jitter using chrono-machines if jitter_factor > 0
        let timeout_secs = if ctx.config.jitter_factor > 0.0 {
            let policy = chrono_machines::Policy {
                max_attempts: 1,
                base_delay_ms: (ctx.config.half_open_timeout_secs * 1000.0) as u64,
                multiplier: 1.0,
                max_delay_ms: (ctx.config.half_open_timeout_secs * 1000.0) as u64,
            };
            let timeout_ms = policy.calculate_delay(1, ctx.config.jitter_factor);
            (timeout_ms as f64) / 1000.0
        } else {
            ctx.config.half_open_timeout_secs
        };

        elapsed >= timeout_secs
    }
}

/// Circuit breaker public API
pub struct CircuitBreaker {
    machine: DynamicCircuit,
    context: CircuitContext,
    callbacks: Callbacks,
}

impl CircuitBreaker {
    /// Create a new circuit breaker (use builder() for more options)
    pub fn new(name: String, config: Config) -> Self {
        let storage = Arc::new(crate::MemoryStorage::new());
        let context = CircuitContext {
            name,
            config,
            storage,
            failure_classifier: None,
            bulkhead: None,
        };

        let machine = DynamicCircuit::new(context.clone());
        let callbacks = Callbacks::new();

        Self {
            machine,
            context,
            callbacks,
        }
    }

    /// Create a circuit breaker with custom context and callbacks (used by builder)
    pub(crate) fn with_context_and_callbacks(
        context: CircuitContext,
        callbacks: Callbacks,
    ) -> Self {
        let machine = DynamicCircuit::new(context.clone());

        Self {
            machine,
            context,
            callbacks,
        }
    }

    /// Create a new circuit breaker builder
    pub fn builder(name: impl Into<String>) -> crate::builder::CircuitBuilder {
        crate::builder::CircuitBuilder::new(name)
    }

    /// Execute a fallible operation with circuit breaker protection
    ///
    /// Accepts either:
    /// - A plain closure: `circuit.call(|| api_request())`
    /// - A closure with options: `circuit.call((|| api_request(), CallOptions::new().with_fallback(...)))`
    pub fn call<I, T, E: 'static>(&mut self, input: I) -> Result<T, CircuitError<E>>
    where
        I: IntoCallOptions<T, E>,
    {
        let (f, options) = input.into_call_options();

        // Try to acquire bulkhead permit if configured
        let _guard = if let Some(bulkhead) = &self.context.bulkhead {
            match bulkhead.try_acquire() {
                Some(guard) => Some(guard),
                None => {
                    return Err(CircuitError::BulkheadFull {
                        circuit: self.context.name.clone(),
                        limit: bulkhead.limit(),
                    });
                }
            }
        } else {
            None
        };

        // Check for timeout-based Open -> HalfOpen transition
        if self.machine.current_state() == "Open" {
            let _ = self.machine.handle(CircuitEvent::AttemptReset);
            if self.machine.current_state() == "HalfOpen" {
                self.callbacks.trigger_half_open(&self.context.name);
            }
        }

        // Handle based on current state
        match self.machine.current_state() {
            "Open" => {
                let opened_at = self.machine.open_data().map(|d| d.opened_at).unwrap_or(0.0);

                // If fallback is provided, use it instead of returning error
                if let Some(fallback) = options.fallback {
                    let ctx = FallbackContext {
                        circuit_name: self.context.name.clone(),
                        opened_at,
                        state: "Open",
                    };
                    return fallback(&ctx).map_err(CircuitError::Execution);
                }

                Err(CircuitError::Open {
                    circuit: self.context.name.clone(),
                    opened_at,
                })
            }
            "HalfOpen" => {
                // Check if we've reached the success threshold
                if let Some(data) = self.machine.half_open_data()
                    && data.consecutive_successes >= self.context.config.success_threshold
                {
                    return Err(CircuitError::HalfOpenLimitReached {
                        circuit: self.context.name.clone(),
                    });
                }
                self.execute_call(f)
            }
            _ => self.execute_call(f),
        }
    }

    fn execute_call<T, E: 'static>(
        &mut self,
        f: Box<dyn FnOnce() -> Result<T, E>>,
    ) -> Result<T, CircuitError<E>> {
        let start = self.context.storage.monotonic_time();

        match f() {
            Ok(val) => {
                let duration = self.context.storage.monotonic_time() - start;
                self.context
                    .storage
                    .record_success(&self.context.name, duration);

                // Handle success in HalfOpen state
                if self.machine.current_state() == "HalfOpen" {
                    if let Some(data) = self.machine.half_open_data_mut() {
                        data.consecutive_successes += 1;
                    }

                    // Try to close the circuit
                    if self.machine.handle(CircuitEvent::Close).is_ok() {
                        self.callbacks.trigger_close(&self.context.name);
                    }
                }

                Ok(val)
            }
            Err(e) => {
                let duration = self.context.storage.monotonic_time() - start;

                // Check if this error should trip the circuit using failure classifier
                let should_trip = if let Some(classifier) = &self.context.failure_classifier {
                    let ctx = crate::classifier::FailureContext {
                        circuit_name: &self.context.name,
                        error: &e as &dyn std::any::Any,
                        duration,
                    };
                    classifier.should_trip(&ctx)
                } else {
                    // No classifier - default behavior is to trip on all errors
                    true
                };

                // Only record failure and try to trip if classifier says we should
                if should_trip {
                    self.context
                        .storage
                        .record_failure(&self.context.name, duration);

                    // Try to trip the circuit
                    let result = self.machine.handle(CircuitEvent::Trip);
                    if result.is_ok() {
                        self.mark_open();
                    } else if self.machine.current_state() == "HalfOpen" {
                        // Failure did not reopen the circuit; reset consecutive successes
                        if let Some(data) = self.machine.half_open_data_mut() {
                            data.consecutive_successes = 0;
                        }
                    }
                }

                Err(CircuitError::Execution(e))
            }
        }
    }

    /// Record a successful operation and drive HalfOpen -> Closed transitions
    pub fn record_success_and_maybe_close(&mut self, duration: f64) {
        self.context
            .storage
            .record_success(&self.context.name, duration);

        if self.machine.current_state() == "HalfOpen" {
            if let Some(data) = self.machine.half_open_data_mut() {
                data.consecutive_successes += 1;
            }

            if self.machine.handle(CircuitEvent::Close).is_ok() {
                self.callbacks.trigger_close(&self.context.name);
            }
        }
    }

    /// Record a failed operation and attempt to trip the circuit
    pub fn record_failure_and_maybe_trip(&mut self, duration: f64) {
        self.context
            .storage
            .record_failure(&self.context.name, duration);

        let result = self.machine.handle(CircuitEvent::Trip);
        if result.is_ok() {
            self.mark_open();
        } else if self.machine.current_state() == "HalfOpen"
            && let Some(data) = self.machine.half_open_data_mut()
        {
            data.consecutive_successes = 0;
        }
    }

    /// Record a successful operation (for manual tracking)
    pub fn record_success(&self, duration: f64) {
        self.context
            .storage
            .record_success(&self.context.name, duration);
    }

    /// Record a failed operation (for manual tracking)
    pub fn record_failure(&self, duration: f64) {
        self.context
            .storage
            .record_failure(&self.context.name, duration);
    }

    /// Check failure threshold and attempt to trip the circuit
    /// This should be called after record_failure() when not using call()
    pub fn check_and_trip(&mut self) -> bool {
        if self.machine.handle(CircuitEvent::Trip).is_ok() {
            self.mark_open();
            true
        } else {
            false
        }
    }

    /// Check if circuit is open
    pub fn is_open(&self) -> bool {
        self.machine.current_state() == "Open"
    }

    /// Check if circuit is closed
    pub fn is_closed(&self) -> bool {
        self.machine.current_state() == "Closed"
    }

    /// Get current state name
    pub fn state_name(&self) -> &'static str {
        self.machine.current_state()
    }

    /// Clear all events and reset circuit to Closed state
    pub fn reset(&mut self) {
        self.context.storage.clear(&self.context.name);
        // Recreate machine in Closed state
        self.machine = DynamicCircuit::new(self.context.clone());
    }

    /// Apply Open-state bookkeeping (timestamp + callback)
    fn mark_open(&mut self) {
        if let Some(data) = self.machine.open_data_mut() {
            data.opened_at = self.context.storage.monotonic_time();
        }
        self.callbacks.trigger_open(&self.context.name);
    }
}

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

    #[test]
    fn test_circuit_breaker_creation() {
        let config = Config::default();
        let circuit = CircuitBreaker::new("test".to_string(), config);

        assert!(circuit.is_closed());
        assert!(!circuit.is_open());
    }

    #[test]
    fn test_circuit_opens_after_threshold() {
        let config = Config {
            failure_threshold: Some(3),
            ..Default::default()
        };

        let mut circuit = CircuitBreaker::new("test".to_string(), config);

        // Trigger failures via call() method
        let _ = circuit.call(|| Err::<(), _>("error 1"));
        let _ = circuit.call(|| Err::<(), _>("error 2"));
        assert!(circuit.is_closed());

        let _ = circuit.call(|| Err::<(), _>("error 3"));
        assert!(circuit.is_open());
    }

    #[test]
    fn test_reset_clears_state() {
        let config = Config {
            failure_threshold: Some(2),
            ..Default::default()
        };

        let mut circuit = CircuitBreaker::new("test".to_string(), config);

        // Trigger failures
        let _ = circuit.call(|| Err::<(), _>("error 1"));
        let _ = circuit.call(|| Err::<(), _>("error 2"));
        assert!(circuit.is_open());

        circuit.reset();
        assert!(circuit.is_closed());
    }

    #[test]
    fn test_state_machine_closed_to_open_transition() {
        let storage = Arc::new(crate::MemoryStorage::new());
        let config = Config {
            failure_threshold: Some(3),
            ..Default::default()
        };

        let ctx = CircuitContext {
            failure_classifier: None,
            bulkhead: None,
            name: "test_circuit".to_string(),
            config,
            storage: storage.clone(),
        };

        let mut circuit = DynamicCircuit::new(ctx.clone());

        // Initially closed - trip should fail guard
        let result = circuit.handle(CircuitEvent::Trip);
        assert!(result.is_err(), "Should fail guard when below threshold");

        // Record failures to exceed threshold
        storage.record_failure("test_circuit", 0.1);
        storage.record_failure("test_circuit", 0.1);
        storage.record_failure("test_circuit", 0.1);

        // Now trip should succeed - guards pass
        circuit
            .handle(CircuitEvent::Trip)
            .expect("Should open after reaching threshold");

        assert_eq!(circuit.current_state(), "Open");
    }

    #[test]
    fn test_state_machine_open_to_half_open_transition() {
        let storage = Arc::new(crate::MemoryStorage::new());
        let config = Config {
            failure_threshold: Some(2),
            half_open_timeout_secs: 0.001, // Very short timeout for testing
            ..Default::default()
        };

        let ctx = CircuitContext {
            failure_classifier: None,
            bulkhead: None,
            name: "test_circuit".to_string(),
            config,
            storage: storage.clone(),
        };

        // Record failures and open circuit
        storage.record_failure("test_circuit", 0.1);
        storage.record_failure("test_circuit", 0.1);

        let mut circuit = DynamicCircuit::new(ctx.clone());
        circuit.handle(CircuitEvent::Trip).expect("Should open");

        // Set opened_at timestamp
        if let Some(data) = circuit.open_data_mut() {
            data.opened_at = storage.monotonic_time();
        }

        // Immediately try to reset - should fail guard (timeout not elapsed)
        let result = circuit.handle(CircuitEvent::AttemptReset);
        assert!(
            result.is_err(),
            "Should fail guard when timeout not elapsed"
        );

        // Wait for timeout
        std::thread::sleep(std::time::Duration::from_millis(5));

        circuit
            .handle(CircuitEvent::AttemptReset)
            .expect("Should reset after timeout");

        // Verify we're in HalfOpen state
        assert_eq!(circuit.current_state(), "HalfOpen");
        let data = circuit.half_open_data().expect("Should have HalfOpen data");
        assert_eq!(data.consecutive_successes, 0);
    }

    #[test]
    fn test_state_machine_half_open_to_closed_guard() {
        let storage = Arc::new(crate::MemoryStorage::new());
        let config = Config {
            failure_threshold: Some(2),
            half_open_timeout_secs: 0.001,
            ..Default::default()
        };

        let ctx = CircuitContext {
            failure_classifier: None,
            bulkhead: None,
            name: "test_circuit".to_string(),
            config,
            storage: storage.clone(),
        };

        // Get to HalfOpen state
        storage.record_failure("test_circuit", 0.1);
        storage.record_failure("test_circuit", 0.1);

        let mut circuit = DynamicCircuit::new(ctx.clone());
        circuit.handle(CircuitEvent::Trip).expect("Should open");

        // Set opened_at and wait for timeout
        if let Some(data) = circuit.open_data_mut() {
            data.opened_at = storage.monotonic_time();
        }
        std::thread::sleep(std::time::Duration::from_millis(5));

        circuit
            .handle(CircuitEvent::AttemptReset)
            .expect("Should reset");

        // Try to close - should fail guard (not enough successes)
        let result = circuit.handle(CircuitEvent::Close);
        assert!(result.is_err(), "Should fail guard without successes");
    }

    #[test]
    fn test_jitter_disabled() {
        let storage = Arc::new(crate::MemoryStorage::new());
        let config = Config {
            failure_threshold: Some(1),
            half_open_timeout_secs: 1.0, // 1 second timeout
            jitter_factor: 0.0,          // No jitter
            ..Default::default()
        };

        let ctx = CircuitContext {
            failure_classifier: None,
            bulkhead: None,
            name: "test_circuit".to_string(),
            config,
            storage: storage.clone(),
        };

        // Open circuit
        storage.record_failure("test_circuit", 0.1);
        let mut circuit = DynamicCircuit::new(ctx.clone());
        circuit.handle(CircuitEvent::Trip).expect("Should open");

        // Set opened_at
        if let Some(data) = circuit.open_data_mut() {
            data.opened_at = storage.monotonic_time();
        }

        // Wait exactly 1 second
        std::thread::sleep(std::time::Duration::from_secs(1));

        // Should transition to HalfOpen (no jitter = exact timeout)
        circuit
            .handle(CircuitEvent::AttemptReset)
            .expect("Should reset after exact timeout");
        assert_eq!(circuit.current_state(), "HalfOpen");
    }

    #[test]
    fn test_jitter_enabled() {
        let storage = Arc::new(crate::MemoryStorage::new());
        let config = Config {
            failure_threshold: Some(1),
            half_open_timeout_secs: 1.0,
            jitter_factor: 0.1, // 10% jitter = 90-100% of timeout
            ..Default::default()
        };

        let ctx = CircuitContext {
            failure_classifier: None,
            bulkhead: None,
            name: "test_circuit".to_string(),
            config,
            storage: storage.clone(),
        };

        // Test multiple times to verify jitter reduces timeout
        let mut found_early_reset = false;
        for _ in 0..10 {
            // Open circuit
            storage.record_failure("test_circuit", 0.1);
            let mut circuit = DynamicCircuit::new(ctx.clone());
            circuit.handle(CircuitEvent::Trip).expect("Should open");

            if let Some(data) = circuit.open_data_mut() {
                data.opened_at = storage.monotonic_time();
            }

            // With 10% jitter, timeout should be 900-1000ms
            // Try at 950ms - should sometimes succeed (jitter applied)
            std::thread::sleep(std::time::Duration::from_millis(950));

            if circuit.handle(CircuitEvent::AttemptReset).is_ok() {
                found_early_reset = true;
                break;
            }

            storage.clear("test_circuit");
        }

        assert!(
            found_early_reset,
            "Jitter should occasionally allow reset before full timeout"
        );
    }

    #[test]
    fn test_builder_with_jitter() {
        let mut circuit = CircuitBreaker::builder("test")
            .failure_threshold(2)
            .half_open_timeout_secs(1.0)
            .jitter_factor(0.5) // 50% jitter
            .build();

        // Trigger failures
        let _ = circuit.call(|| Err::<(), _>("error 1"));
        let _ = circuit.call(|| Err::<(), _>("error 2"));
        assert!(circuit.is_open());

        // Verify jitter_factor was set
        assert_eq!(circuit.context.config.jitter_factor, 0.5);
    }

    #[test]
    fn test_fallback_when_open() {
        let mut circuit = CircuitBreaker::builder("test").failure_threshold(2).build();

        // Trigger failures to open circuit
        let _ = circuit.call(|| Err::<(), _>("error 1"));
        let _ = circuit.call(|| Err::<(), _>("error 2"));
        assert!(circuit.is_open());

        // Call with fallback should return fallback result
        let result = circuit.call((
            || Err::<String, _>("should not execute"),
            CallOptions::new().with_fallback(|ctx| {
                assert_eq!(ctx.circuit_name, "test");
                assert_eq!(ctx.state, "Open");
                Ok("fallback response".to_string())
            }),
        ));

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "fallback response");
    }

    #[test]
    fn test_fallback_error_propagation() {
        let mut circuit = CircuitBreaker::builder("test").failure_threshold(1).build();

        // Trigger failure to open circuit
        let _ = circuit.call(|| Err::<(), _>("error"));
        assert!(circuit.is_open());

        // Fallback can also return errors
        let result = circuit.call((
            || Ok::<String, _>("should not execute".to_string()),
            CallOptions::new().with_fallback(|_ctx| Err::<String, _>("fallback error")),
        ));

        assert!(result.is_err());
        match result {
            Err(CircuitError::Execution(e)) => assert_eq!(e, "fallback error"),
            _ => panic!("Expected CircuitError::Execution"),
        }
    }

    #[test]
    fn test_rate_based_threshold() {
        let mut circuit = CircuitBreaker::builder("test")
            .disable_failure_threshold() // Only use rate-based
            .failure_rate(0.5) // 50% failure rate
            .minimum_calls(10)
            .build();

        // First 9 calls - below minimum, circuit stays closed
        for i in 0..9 {
            let _result = if i % 2 == 0 {
                circuit.call(|| Ok::<(), _>(()))
            } else {
                circuit.call(|| Err::<(), _>("error"))
            };
            // Even with failures, circuit should stay closed (below minimum calls)
            assert!(circuit.is_closed(), "Circuit opened before minimum calls");
        }

        // 10th call - now at minimum, with 5 failures out of 10 = 50% rate
        // This should trip the circuit
        let _ = circuit.call(|| Err::<(), _>("error"));

        // Circuit should now be open (failure rate reached threshold)
        assert!(circuit.is_open(), "Circuit did not open at rate threshold");
    }

    #[test]
    fn test_rate_and_absolute_threshold_both_active() {
        let mut circuit = CircuitBreaker::builder("test")
            .failure_threshold(3) // Absolute: 3 failures
            .failure_rate(0.8) // Rate: 80%
            .minimum_calls(10)
            .build();

        // Trigger 3 failures quickly - should open via absolute threshold
        let _ = circuit.call(|| Err::<(), _>("error 1"));
        let _ = circuit.call(|| Err::<(), _>("error 2"));
        assert!(circuit.is_closed());

        let _ = circuit.call(|| Err::<(), _>("error 3"));
        assert!(
            circuit.is_open(),
            "Circuit did not open at absolute threshold"
        );
    }

    #[test]
    fn test_minimum_calls_prevents_premature_trip() {
        let mut circuit = CircuitBreaker::builder("test")
            .disable_failure_threshold()
            .failure_rate(0.5)
            .minimum_calls(20)
            .build();

        // Record 10 failures out of 10 calls = 100% failure rate
        for _ in 0..10 {
            let _ = circuit.call(|| Err::<(), _>("error"));
        }

        // Circuit should still be closed (below minimum_calls)
        assert!(
            circuit.is_closed(),
            "Circuit opened before reaching minimum_calls"
        );
    }

    #[test]
    fn test_failure_classifier_filters_errors() {
        use crate::classifier::PredicateClassifier;

        // Classifier that only trips on "server" errors, not "client" errors
        let classifier = Arc::new(PredicateClassifier::new(|ctx| {
            ctx.error
                .downcast_ref::<&str>()
                .map(|e| e.contains("server"))
                .unwrap_or(true)
        }));

        let mut circuit = CircuitBreaker::builder("test")
            .failure_threshold(2)
            .failure_classifier(classifier)
            .build();

        // Client errors should not trip circuit
        for _ in 0..5 {
            let _ = circuit.call(|| Err::<(), _>("client_error"));
        }
        assert!(
            circuit.is_closed(),
            "Circuit should not trip on filtered errors"
        );

        // Server errors should trip circuit
        let _ = circuit.call(|| Err::<(), _>("server_error_1"));
        let _ = circuit.call(|| Err::<(), _>("server_error_2"));
        assert!(circuit.is_open(), "Circuit should trip on server errors");
    }

    #[test]
    fn test_failure_classifier_with_slow_errors() {
        use crate::classifier::PredicateClassifier;

        // Only trip on errors that take > 0.5s
        let classifier = Arc::new(PredicateClassifier::new(|ctx| ctx.duration > 0.5));

        let mut circuit = CircuitBreaker::builder("test")
            .failure_threshold(2)
            .failure_classifier(classifier)
            .build();

        // Fast errors don't trip (duration will be near zero in tests)
        for _ in 0..10 {
            let _ = circuit.call(|| Err::<(), _>("fast error"));
        }
        assert!(
            circuit.is_closed(),
            "Circuit should not trip on fast errors"
        );
    }

    #[test]
    fn test_no_classifier_default_behavior() {
        // Without classifier, all errors should trip circuit (backward compatible)
        let mut circuit = CircuitBreaker::builder("test").failure_threshold(3).build();

        let _ = circuit.call(|| Err::<(), _>("error 1"));
        let _ = circuit.call(|| Err::<(), _>("error 2"));
        assert!(circuit.is_closed());

        let _ = circuit.call(|| Err::<(), _>("error 3"));
        assert!(
            circuit.is_open(),
            "All errors should trip circuit without classifier"
        );
    }

    #[test]
    fn test_classifier_with_custom_error_type() {
        use crate::classifier::PredicateClassifier;

        #[derive(Debug)]
        enum ApiError {
            ClientError(u16),
            ServerError(u16),
        }

        // Only trip on server errors (5xx), not client errors (4xx)
        let classifier = Arc::new(PredicateClassifier::new(|ctx| {
            ctx.error
                .downcast_ref::<ApiError>()
                .map(|e| match e {
                    ApiError::ServerError(code) => *code >= 500,
                    ApiError::ClientError(code) => *code >= 500, // Should never happen, but validates field
                })
                .unwrap_or(true)
        }));

        let mut circuit = CircuitBreaker::builder("test")
            .failure_threshold(2)
            .failure_classifier(classifier)
            .build();

        // Client errors (4xx) should not trip
        for _ in 0..10 {
            let _ = circuit.call(|| Err::<(), _>(ApiError::ClientError(404)));
        }
        assert!(circuit.is_closed(), "Client errors should not trip circuit");

        // Server errors (5xx) should trip
        let _ = circuit.call(|| Err::<(), _>(ApiError::ServerError(500)));
        let _ = circuit.call(|| Err::<(), _>(ApiError::ServerError(503)));
        assert!(circuit.is_open(), "Server errors should trip circuit");
    }

    #[test]
    fn test_bulkhead_rejects_at_capacity() {
        let mut circuit = CircuitBreaker::builder("test").max_concurrency(2).build();

        // First two calls should succeed (we're not actually holding them)
        let result1 = circuit.call(|| Ok::<_, String>("success 1"));
        let result2 = circuit.call(|| Ok::<_, String>("success 2"));

        assert!(result1.is_ok());
        assert!(result2.is_ok());
    }

    #[test]
    fn test_bulkhead_releases_on_success() {
        use std::sync::{Arc, Mutex};

        let circuit = Arc::new(Mutex::new(
            CircuitBreaker::builder("test").max_concurrency(1).build(),
        ));

        // First call acquires permit
        let result1 = circuit.lock().unwrap().call(|| Ok::<_, String>("success"));
        assert!(result1.is_ok());

        // Permit is released, second call should succeed
        let result2 = circuit.lock().unwrap().call(|| Ok::<_, String>("success"));
        assert!(result2.is_ok());
    }

    #[test]
    fn test_bulkhead_releases_on_failure() {
        use std::sync::{Arc, Mutex};

        let circuit = Arc::new(Mutex::new(
            CircuitBreaker::builder("test")
                .max_concurrency(1)
                .failure_threshold(10) // High threshold so circuit doesn't open
                .build(),
        ));

        // First call fails but releases permit
        let result1 = circuit.lock().unwrap().call(|| Err::<(), _>("error"));
        assert!(result1.is_err());

        // Permit is released, second call should succeed
        let result2 = circuit.lock().unwrap().call(|| Ok::<_, String>("success"));
        assert!(result2.is_ok());
    }

    #[test]
    fn test_bulkhead_without_limit() {
        let mut circuit = CircuitBreaker::builder("test").build();

        // Without bulkhead, all calls should go through
        for _ in 0..100 {
            let result = circuit.call(|| Ok::<_, String>("success"));
            assert!(result.is_ok());
        }
    }

    #[test]
    fn test_bulkhead_error_contains_limit() {
        // Test that bulkhead full error contains circuit name and limit
        // We use the underlying semaphore to simulate capacity exhaustion
        use std::sync::Arc;

        let bulkhead = Arc::new(BulkheadSemaphore::new(2));

        let mut circuit = CircuitBreaker::builder("test").build();

        // Manually inject bulkhead into circuit context
        circuit.context.bulkhead = Some(bulkhead.clone());

        // Acquire all permits directly from semaphore
        let _guard1 = bulkhead.try_acquire().unwrap();
        let _guard2 = bulkhead.try_acquire().unwrap();

        // Now circuit call should fail with BulkheadFull
        let result = circuit.call(|| Ok::<_, String>("should fail"));

        match result {
            Err(CircuitError::BulkheadFull {
                circuit: name,
                limit,
            }) => {
                assert_eq!(name, "test");
                assert_eq!(limit, 2);
            }
            _ => panic!("Expected BulkheadFull error, got: {:?}", result),
        }

        // Drop guards to release permits
        drop(_guard1);
        drop(_guard2);

        // Now call should succeed
        let result = circuit.call(|| Ok::<_, String>("success"));
        assert!(result.is_ok());
    }

    #[test]
    fn test_bulkhead_with_circuit_breaker() {
        let mut circuit = CircuitBreaker::builder("test")
            .max_concurrency(5)
            .failure_threshold(3)
            .build();

        // Circuit is closed, bulkhead allows calls
        let result = circuit.call(|| Ok::<_, String>("success"));
        assert!(result.is_ok());

        // Open the circuit with failures
        for _ in 0..3 {
            let _ = circuit.call(|| Err::<(), _>("error"));
        }
        assert!(circuit.is_open());

        // Even with bulkhead capacity, open circuit rejects calls
        let result = circuit.call(|| Ok::<_, String>("should fail"));
        assert!(matches!(result, Err(CircuitError::Open { .. })));
    }

    #[test]
    fn test_check_and_trip_sets_opened_at_and_callback() {
        use std::sync::atomic::{AtomicBool, Ordering};

        let opened = Arc::new(AtomicBool::new(false));
        let opened_clone = opened.clone();

        let mut circuit = CircuitBreaker::builder("test")
            .failure_threshold(1)
            .on_open(move |_name| {
                opened_clone.store(true, Ordering::SeqCst);
            })
            .build();

        circuit.record_failure(0.1);
        let tripped = circuit.check_and_trip();

        assert!(tripped, "Trip should succeed");
        assert!(circuit.is_open(), "Circuit should be open after trip");

        let opened_at = circuit
            .machine
            .open_data()
            .expect("Open data should be present")
            .opened_at;

        assert!(opened_at > 0.0, "opened_at should be set");
        assert!(
            opened.load(Ordering::SeqCst),
            "on_open callback should fire"
        );
    }

    #[test]
    fn test_half_open_failure_resets_consecutive_successes() {
        let mut circuit = CircuitBreaker::builder("test")
            .failure_threshold(2)
            .half_open_timeout_secs(0.001)
            .success_threshold(2)
            .build();

        // Open the circuit
        let _ = circuit.call(|| Err::<(), _>("error 1"));
        let _ = circuit.call(|| Err::<(), _>("error 2"));
        assert!(circuit.is_open());

        // Move to HalfOpen
        if let Some(data) = circuit.machine.open_data_mut() {
            data.opened_at = circuit.context.storage.monotonic_time();
        }
        std::thread::sleep(std::time::Duration::from_millis(2));
        circuit
            .machine
            .handle(CircuitEvent::AttemptReset)
            .expect("Should transition to HalfOpen");
        assert_eq!(circuit.machine.current_state(), "HalfOpen");

        // Clear counts to simulate expired failure window
        circuit.context.storage.clear("test");

        // First success increments consecutive count
        let _ = circuit.call(|| Ok::<_, String>("ok"));
        assert_eq!(
            circuit
                .machine
                .half_open_data()
                .expect("HalfOpen data")
                .consecutive_successes,
            1
        );

        // Failure below threshold should not reopen circuit but should reset counter
        let _ = circuit.call(|| Err::<(), _>("fail"));
        assert_eq!(circuit.machine.current_state(), "HalfOpen");
        assert_eq!(
            circuit
                .machine
                .half_open_data()
                .expect("HalfOpen data")
                .consecutive_successes,
            0
        );

        // Next success starts count from 1 again
        let _ = circuit.call(|| Ok::<_, String>("ok2"));
        assert_eq!(
            circuit
                .machine
                .half_open_data()
                .expect("HalfOpen data")
                .consecutive_successes,
            1
        );
    }

    #[test]
    fn test_jitter_distribution_within_bounds() {
        // Test that jitter produces values within expected bounds
        // With 25% jitter on 1000ms base, expect 750-1000ms range
        let storage = Arc::new(crate::MemoryStorage::new());
        let base_timeout = 1.0; // 1 second
        let jitter_factor = 0.25;

        let config = Config {
            failure_threshold: Some(1),
            half_open_timeout_secs: base_timeout,
            jitter_factor,
            ..Default::default()
        };

        let ctx = CircuitContext {
            failure_classifier: None,
            bulkhead: None,
            name: "jitter_test".to_string(),
            config,
            storage: storage.clone(),
        };

        // Run 50 iterations and collect timeout values
        let mut min_seen = f64::MAX;
        let mut max_seen = f64::MIN;

        for _ in 0..50 {
            storage.record_failure("jitter_test", 0.1);
            let mut circuit = DynamicCircuit::new(ctx.clone());
            circuit.handle(CircuitEvent::Trip).expect("Should open");

            if let Some(data) = circuit.open_data_mut() {
                data.opened_at = storage.monotonic_time();
            }

            // Calculate what the jittered timeout would be
            let policy = chrono_machines::Policy {
                max_attempts: 1,
                base_delay_ms: (base_timeout * 1000.0) as u64,
                multiplier: 1.0,
                max_delay_ms: (base_timeout * 1000.0) as u64,
            };
            let timeout_ms = policy.calculate_delay(1, jitter_factor);
            let timeout_secs = (timeout_ms as f64) / 1000.0;

            min_seen = min_seen.min(timeout_secs);
            max_seen = max_seen.max(timeout_secs);

            storage.clear("jitter_test");
        }

        // With 25% jitter, minimum should be ~0.75s (75% of base)
        // Maximum should be ~1.0s (100% of base)
        let min_expected = base_timeout * (1.0 - jitter_factor);
        let max_expected = base_timeout;

        assert!(
            min_seen >= min_expected - 0.01,
            "Minimum jittered timeout {} should be >= {}",
            min_seen,
            min_expected
        );
        assert!(
            max_seen <= max_expected + 0.01,
            "Maximum jittered timeout {} should be <= {}",
            max_seen,
            max_expected
        );
    }

    #[test]
    fn test_jitter_produces_variance() {
        // Test that jitter actually produces different values (not all same)
        let storage = Arc::new(crate::MemoryStorage::new());

        let config = Config {
            failure_threshold: Some(1),
            half_open_timeout_secs: 1.0,
            jitter_factor: 0.5, // 50% jitter for more variance
            ..Default::default()
        };

        let _ctx = CircuitContext {
            failure_classifier: None,
            bulkhead: None,
            name: "jitter_variance".to_string(),
            config,
            storage: storage.clone(),
        };

        let mut values = std::collections::HashSet::new();

        for _ in 0..20 {
            let policy = chrono_machines::Policy {
                max_attempts: 1,
                base_delay_ms: 1000,
                multiplier: 1.0,
                max_delay_ms: 1000,
            };
            let timeout_ms = policy.calculate_delay(1, 0.5);
            values.insert(timeout_ms);
        }

        // With 50% jitter over 20 iterations, we should see at least 2 different values
        // (statistically, seeing all same values is extremely unlikely)
        assert!(
            values.len() >= 2,
            "Jitter should produce variance, got {} unique values",
            values.len()
        );
    }

    #[test]
    fn test_zero_jitter_produces_constant_timeout() {
        // Test that 0% jitter always produces the same timeout
        let policy = chrono_machines::Policy {
            max_attempts: 1,
            base_delay_ms: 1000,
            multiplier: 1.0,
            max_delay_ms: 1000,
        };

        let mut values = std::collections::HashSet::new();

        for _ in 0..10 {
            let timeout_ms = policy.calculate_delay(1, 0.0);
            values.insert(timeout_ms);
        }

        assert_eq!(
            values.len(),
            1,
            "Zero jitter should produce constant timeout"
        );
        assert!(values.contains(&1000), "Timeout should be exactly 1000ms");
    }
}