1
use super::*;
2
use crate::command::CmdError;
3
use crate::tag::lock::ATTEMPTS;
4

            
5
#[test]
6
1
fn retries_are_exhausted_after_the_attempt_budget() {
7
1
    let mut tries = Tries::default();
8
1
    for _ in 1..ATTEMPTS {
9
2
        let step = tries.step::<(), ServerError>(Err(Attempt::Retry));
10
2
        assert!(matches!(step, Ok(None)));
11
    }
12
1
    let last = tries.step::<(), ServerError>(Err(Attempt::Retry));
13
1
    assert!(matches!(last, Err(ServerError::DB(DBError::WriteConflict))));
14
1
}
15

            
16
#[test]
17
1
fn exhausted_command_tries_wrap_the_same_conflict() {
18
1
    let mut tries = Tries::default();
19
1
    let last = (0..ATTEMPTS)
20
3
        .map(|_| tries.step::<(), CmdError>(Err(Attempt::Retry)))
21
        .last();
22
1
    assert!(matches!(
23
1
        last,
24
        Some(Err(CmdError::Server(ServerError::DB(
25
            DBError::WriteConflict
26
        ))))
27
    ));
28
1
}
29

            
30
#[test]
31
1
fn a_commit_and_a_failure_end_the_loop() {
32
1
    assert!(matches!(
33
1
        Tries::default().step::<_, CmdError>(Ok(7)),
34
        Ok(Some(7))
35
    ));
36
1
    let failed = Tries::default()
37
1
        .step::<(), CmdError>(Err(Attempt::Failed(CmdError::Args("x".to_string()))));
38
1
    assert!(matches!(failed, Err(CmdError::Args(_))));
39
1
}
40

            
41
#[test]
42
1
fn a_changed_key_and_a_retryable_error_run_again() {
43
1
    let changed = Attempt::<ServerError>::of::<(), ServerError>(
44
1
        Ok::<_, ServerError>(Err(KeyChanged)),
45
        |_| false,
46
        |e| e,
47
    );
48
1
    assert!(matches!(changed, Err(Attempt::Retry)));
49
    let retryable =
50
1
        Attempt::<ServerError>::of::<(), ServerError>(Err(ServerError::Creation), |_| true, |e| e);
51
1
    assert!(matches!(retryable, Err(Attempt::Retry)));
52
1
}
53

            
54
#[test]
55
1
fn a_commit_and_another_error_end_the_attempt() {
56
1
    let done = Attempt::<ServerError>::of::<(), ServerError>(
57
1
        Ok::<_, ServerError>(Ok(())),
58
        |_| true,
59
        |e| e,
60
    );
61
1
    assert!(matches!(done, Ok(())));
62
    let failed =
63
1
        Attempt::<ServerError>::of::<(), ServerError>(Err(ServerError::Creation), |_| false, |e| e);
64
1
    assert!(matches!(
65
1
        failed,
66
        Err(Attempt::Failed(ServerError::Creation))
67
    ));
68
1
}