relentless 0.11.1

Composable retry policies for fallible operations and polling.
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
//! Tests for the Stop trait and built-in stop strategies.
//!
//! Covers threshold/boundary behavior of each strategy, the non-generic (T/E-independent)
//! nature of Stop, composition via `|`/`&` (StopAny/StopAll), and the no-short-circuit
//! guarantee for composite strategies.

use core::cell::Cell;
use core::time::Duration;
use relentless::Stop;
use relentless::stop;

const MAX_ATTEMPTS: u32 = 5;
const DEADLINE: Duration = Duration::from_secs(30);

fn make_state(attempt: u32) -> relentless::RetryState {
    relentless::RetryState::new(attempt, None)
}

fn make_state_with_elapsed(attempt: u32, elapsed: Duration) -> relentless::RetryState {
    relentless::RetryState::new(attempt, Some(elapsed))
}

/// Minimal Stop implementation used to verify the trait contract.
/// Stops when `state.attempt >= self.max`.
struct StopAfter {
    max: u32,
}

impl Stop for StopAfter {
    fn should_stop(&self, state: &relentless::RetryState) -> bool {
        state.attempt >= self.max
    }
}

#[test]
fn stop_should_stop_takes_ref_self_and_retry_state() {
    let stop = StopAfter { max: 3 };

    let state = make_state(1);
    assert!(
        !stop.should_stop(&state),
        "attempt 1 < max, should not stop"
    );

    let state = make_state(3);
    assert!(stop.should_stop(&state), "attempt == max, should stop");
}

#[test]
fn stop_and_wait_are_not_generic_over_result_type() {
    let stop = StopAfter { max: 3 };
    let state = make_state(1);

    // Stop takes &RetryState, not Result<T, E> — a single strategy instance works across
    // operations with different outcome types. This is a compile-time check.
    assert!(!stop.should_stop(&state));
}

#[test]
fn attempts_does_not_fire_below_threshold() {
    let s = stop::attempts(MAX_ATTEMPTS);
    for attempt in 1..MAX_ATTEMPTS {
        let state = make_state(attempt);
        assert!(
            !s.should_stop(&state),
            "should not stop at attempt {attempt}"
        );
    }
}

#[test]
fn attempts_fires_at_threshold() {
    let s = stop::attempts(MAX_ATTEMPTS);
    let state = make_state(MAX_ATTEMPTS);
    assert!(s.should_stop(&state));
}

#[test]
fn attempts_fires_above_threshold() {
    let s = stop::attempts(MAX_ATTEMPTS);
    let state = make_state(MAX_ATTEMPTS + 1);
    assert!(s.should_stop(&state));
}

#[test]
fn attempts_with_one_stops_immediately() {
    let s = stop::attempts(1);
    let state = make_state(1);
    assert!(s.should_stop(&state));
}

#[test]
fn attempts_with_zero_panics() {
    let panic_result = std::panic::catch_unwind(|| {
        let _ = stop::attempts(0);
    });
    let panic_message = panic_result
        .as_ref()
        .err()
        .and_then(|payload| payload.downcast_ref::<&str>().copied())
        .or_else(|| {
            panic_result
                .as_ref()
                .err()
                .and_then(|payload| payload.downcast_ref::<String>().map(String::as_str))
        })
        .unwrap_or("<non-string panic payload>");
    assert!(
        panic_result.is_err(),
        "stop::attempts(0) should panic with invalid configuration"
    );
    assert!(
        panic_message.contains("stop::attempts requires max >= 1"),
        "panic message should explain invalid attempts count, got: {panic_message}"
    );
}

#[test]
fn elapsed_does_not_fire_below_deadline() {
    let s = stop::elapsed(DEADLINE);
    let state = make_state_with_elapsed(1, DEADLINE.checked_sub(Duration::from_millis(1)).unwrap());
    assert!(!s.should_stop(&state));
}

#[test]
fn elapsed_fires_at_deadline() {
    let s = stop::elapsed(DEADLINE);
    let state = make_state_with_elapsed(1, DEADLINE);
    assert!(s.should_stop(&state));
}

#[test]
fn elapsed_fires_above_deadline() {
    let s = stop::elapsed(DEADLINE);
    let state = make_state_with_elapsed(1, DEADLINE + Duration::from_secs(1));
    assert!(s.should_stop(&state));
}

#[test]
fn elapsed_never_fires_when_no_clock() {
    let s = stop::elapsed(DEADLINE);
    // make_state passes None for elapsed — simulates no clock available
    let state = make_state(1);
    assert!(
        !s.should_stop(&state),
        "elapsed should never fire when clock is unavailable"
    );
}

#[test]
fn never_always_returns_false() {
    let s = stop::never();
    for &attempt in &[1, 100, u32::MAX] {
        let state = make_state(attempt);
        assert!(
            !s.should_stop(&state),
            "never() should not stop at attempt {attempt}"
        );
    }
}

#[test]
fn never_returns_false_even_with_elapsed() {
    let s = stop::never();
    let state = make_state_with_elapsed(u32::MAX, Duration::MAX);
    assert!(!s.should_stop(&state));
}

#[test]
fn stop_any_fires_when_left_fires() {
    // attempts(1) fires immediately; elapsed(DEADLINE) does not.
    let s = stop::attempts(1) | stop::elapsed(DEADLINE);
    let state = make_state(1);
    assert!(s.should_stop(&state));
}

#[test]
fn stop_any_fires_when_right_fires() {
    // attempts(MAX) does not fire; elapsed fires.
    let s = stop::attempts(u32::MAX) | stop::elapsed(DEADLINE);
    let state = make_state_with_elapsed(1, DEADLINE);
    assert!(s.should_stop(&state));
}

#[test]
fn stop_any_does_not_fire_when_neither_fires() {
    let s = stop::attempts(MAX_ATTEMPTS) | stop::elapsed(DEADLINE);
    let state = make_state(1); // attempt 1 < 5 and no elapsed — neither condition is met
    assert!(!s.should_stop(&state));
}

#[test]
fn stop_all_fires_when_both_fire() {
    let s = stop::attempts(MAX_ATTEMPTS) & stop::elapsed(DEADLINE);
    let state = make_state_with_elapsed(MAX_ATTEMPTS, DEADLINE);
    assert!(s.should_stop(&state));
}

#[test]
fn stop_all_does_not_fire_when_only_left_fires() {
    let s = stop::attempts(MAX_ATTEMPTS) & stop::elapsed(DEADLINE);
    let state = make_state(MAX_ATTEMPTS); // elapsed is None, so elapsed condition is unmet
    assert!(!s.should_stop(&state));
}

#[test]
fn stop_all_does_not_fire_when_only_right_fires() {
    let s = stop::attempts(MAX_ATTEMPTS) & stop::elapsed(DEADLINE);
    let state = make_state_with_elapsed(1, DEADLINE); // attempts condition unmet (1 < 5)
    assert!(!s.should_stop(&state));
}

#[test]
fn three_way_or_composition() {
    let s = stop::attempts(MAX_ATTEMPTS) | stop::elapsed(DEADLINE) | stop::never();

    let state = make_state(MAX_ATTEMPTS);
    assert!(s.should_stop(&state));

    let state = make_state_with_elapsed(1, DEADLINE);
    assert!(s.should_stop(&state));

    // never() never fires, and neither of the other conditions is met here
    let state = make_state(1);
    assert!(!s.should_stop(&state));
}

#[test]
fn mixed_and_or_composition() {
    // (attempts(5) & elapsed(30s)) | never()
    let s = (stop::attempts(MAX_ATTEMPTS) & stop::elapsed(DEADLINE)) | stop::never();

    // Both inner conditions met — the & fires, so the outer | fires too.
    let state = make_state_with_elapsed(MAX_ATTEMPTS, DEADLINE);
    assert!(s.should_stop(&state));

    // Only attempts condition met — the & doesn't fire, and never() never fires.
    let state = make_state(MAX_ATTEMPTS);
    assert!(!s.should_stop(&state));
}

#[test]
fn attempts_is_clone_and_debug() {
    let s = stop::attempts(MAX_ATTEMPTS);
    fn assert_clone<T: Clone>(_value: &T) {}
    assert_clone(&s);
    let debug = format!("{s:?}");
    assert!(debug.contains("StopAfterAttempts"));
}

#[test]
fn elapsed_is_clone_and_debug() {
    let s = stop::elapsed(DEADLINE);
    fn assert_clone<T: Clone>(_value: &T) {}
    assert_clone(&s);
    let debug = format!("{s:?}");
    assert!(debug.contains("StopAfterElapsed"));
}

#[test]
fn never_is_clone_and_debug() {
    let s = stop::never();
    fn assert_clone<T: Clone>(_value: &T) {}
    assert_clone(&s);
    let debug = format!("{s:?}");
    assert!(debug.contains("StopNever"));
}

#[test]
fn stop_any_is_clone_and_debug() {
    let s = stop::attempts(MAX_ATTEMPTS) | stop::elapsed(DEADLINE);
    let s2 = s.clone();
    let debug = format!("{s2:?}");
    assert!(debug.contains("StopAny"));
}

#[test]
fn stop_all_is_clone_and_debug() {
    let s = stop::attempts(MAX_ATTEMPTS) & stop::elapsed(DEADLINE);
    let s2 = s.clone();
    let debug = format!("{s2:?}");
    assert!(debug.contains("StopAll"));
}

#[test]
fn cloned_strategy_is_independent() {
    let original = stop::attempts(MAX_ATTEMPTS);
    let cloned = original;

    let state = make_state(MAX_ATTEMPTS);
    assert!(original.should_stop(&state));
    assert!(cloned.should_stop(&state));

    let state = make_state(1);
    assert!(!original.should_stop(&state));
    assert!(!cloned.should_stop(&state));
}

/// A stateful stop strategy that fires after being consulted a fixed number of times.
/// Used with interior mutability to count evaluations and verify `StopAny`'s
/// no-short-circuit guarantee.
struct StopAfterConsultations {
    threshold: u32,
    count: Cell<u32>,
}

impl StopAfterConsultations {
    const fn new(threshold: u32) -> Self {
        Self {
            threshold,
            count: Cell::new(0),
        }
    }
}

impl Stop for StopAfterConsultations {
    fn should_stop(&self, _state: &relentless::RetryState) -> bool {
        let n = self.count.get() + 1;
        self.count.set(n);
        n >= self.threshold
    }
}

const CONSULTATION_THRESHOLD: u32 = 3;

#[test]
fn no_short_circuit_in_stop_any() {
    // StopAny must evaluate both sides on every call — it does not short-circuit when
    // the left side fires, because stateful strategies on the right rely on being called.
    let composite = stop::attempts(1) | StopAfterConsultations::new(CONSULTATION_THRESHOLD);
    let state = make_state(1);

    assert!(composite.should_stop(&state)); // left fires; right count -> 1
    assert!(composite.should_stop(&state)); // left fires; right count -> 2
    assert!(composite.should_stop(&state)); // left fires; right count -> 3 (threshold reached)
}

#[test]
fn no_short_circuit_in_stop_all() {
    // StopAll must evaluate both sides on every call — it does not short-circuit when
    // the left side returns false, because stateful strategies on the right rely on being called.
    let composite = stop::never() & StopAfterConsultations::new(CONSULTATION_THRESHOLD);
    let state = make_state(1);

    assert!(!composite.should_stop(&state)); // left=false; right count -> 1
    assert!(!composite.should_stop(&state)); // left=false; right count -> 2
    assert!(!composite.should_stop(&state)); // left=false; right count -> 3 (threshold reached, but left never fires)
    // Right has hit threshold but left (never()) never fires, so StopAll never fires either.
    // Crucially, right was evaluated all 3 times despite left always returning false.
    assert!(
        !composite.should_stop(&state),
        "StopAll fires only when both fire; left=never keeps it from firing"
    );
}

#[test]
fn stop_named_combinators_match_operator_forms() {
    let named_or = stop::attempts(3).or(stop::elapsed(Duration::from_secs(2)));
    let op_or = stop::attempts(3) | stop::elapsed(Duration::from_secs(2));

    let early = make_state_with_elapsed(1, Duration::from_secs(1));
    let elapsed_hit = make_state_with_elapsed(1, Duration::from_secs(2));
    let attempt_hit = make_state(3);
    assert_eq!(named_or.should_stop(&early), op_or.should_stop(&early));
    assert_eq!(
        named_or.should_stop(&elapsed_hit),
        op_or.should_stop(&elapsed_hit)
    );
    assert_eq!(
        named_or.should_stop(&attempt_hit),
        op_or.should_stop(&attempt_hit)
    );

    let named_and = stop::attempts(3).and(stop::elapsed(Duration::from_secs(2)));
    let op_and = stop::attempts(3) & stop::elapsed(Duration::from_secs(2));

    let both_hit = make_state_with_elapsed(3, Duration::from_secs(2));
    assert_eq!(named_and.should_stop(&early), op_and.should_stop(&early));
    assert_eq!(
        named_and.should_stop(&elapsed_hit),
        op_and.should_stop(&elapsed_hit)
    );
    assert_eq!(
        named_and.should_stop(&both_hit),
        op_and.should_stop(&both_hit)
    );
}

#[test]
fn stop_any_bitand_produces_stop_all() {
    // Tests the BitAnd impl on StopAny: (a | b) & c.
    let s = (stop::attempts(MAX_ATTEMPTS) | stop::elapsed(DEADLINE)) & stop::attempts(MAX_ATTEMPTS);

    // Only attempts met — the | fires, and the right & fires too.
    let state = make_state(MAX_ATTEMPTS);
    assert!(s.should_stop(&state));

    // Only elapsed met — the | fires, but the right & doesn't.
    let state = make_state_with_elapsed(1, DEADLINE);
    assert!(!s.should_stop(&state));
}

/// A boxed strategy delegates `should_stop` to the boxed impl.
#[cfg(feature = "alloc")]
#[test]
fn boxed_stop_delegates_should_stop() {
    let boxed: Box<dyn Stop> = Box::new(stop::attempts(MAX_ATTEMPTS));
    assert!(!boxed.should_stop(&make_state(MAX_ATTEMPTS - 1)));
    assert!(boxed.should_stop(&make_state(MAX_ATTEMPTS)));
}