1
//! Interrupt and close while an eval is running: only the epoch trap can stop it.
2

            
3
use super::*;
4
use crate::ctx::{EpochBumper, ScriptLimits};
5

            
6
// Nested, so it outlasts any test timeout: only the epoch trap ends it.
7
const ENDLESS: &str = "(do ((j 0 (+ j 1))) ((>= j 1073741823) j) \
8
                         (do ((i 0 (+ i 1))) ((>= i 1073741823) i)))";
9

            
10
4
fn unbounded_fuel() -> ScriptCtx {
11
4
    ScriptCtx::new(uuid::Uuid::nil()).with_limits(ScriptLimits {
12
4
        fuel: u64::MAX,
13
4
        ..ScriptLimits::default()
14
4
    })
15
4
}
16

            
17
/// Stream 1 runs a loop that neither finishes nor runs out of fuel; stream 2 waits behind it.
18
3
async fn running_rig() -> (Rig, Vec<EpochBumper>) {
19
3
    let mut rig = rig_with(unbounded_fuel());
20
3
    rig.frame(&format!("(:id 1 :stream 1 :form {ENDLESS})"));
21
3
    rig.frame("(:id 2 :stream 2 :form (+ 1 2))");
22
4
    while rig.mux.permits.available_permits() != 0 {
23
1
        tokio::time::sleep(Duration::from_millis(5)).await;
24
    }
25
3
    tokio::time::sleep(Duration::from_secs(1)).await;
26
3
    let bumpers = rig
27
3
        .mux
28
3
        .streams
29
3
        .iter()
30
9
        .map(|(_, s)| s.bumper.clone())
31
        .collect();
32
3
    (rig, bumpers)
33
3
}
34

            
35
/// On timeout the loop is trapped by hand first, so a regression fails instead of hanging the runtime.
36
6
async fn next_or_trap(rig: &mut Rig, bumpers: &[EpochBumper]) -> String {
37
6
    let reply = tokio::time::timeout(Duration::from_secs(30), rig.replies.recv()).await;
38
6
    if let Ok(Some(reply)) = reply {
39
6
        return reply;
40
    }
41
    for bumper in bumpers {
42
        bumper.bump();
43
    }
44
    panic!("no reply: the interrupt never reached the running eval");
45
6
}
46

            
47
2
async fn assert_trapped_then_neighbour_answers(rig: &mut Rig, bumpers: &[EpochBumper]) {
48
2
    let trapped = next_or_trap(rig, bumpers).await;
49
2
    assert!(
50
2
        trapped.starts_with("(:id 1 :stream 1 :error (:code interrupted"),
51
        "{trapped}"
52
    );
53
2
    assert!(
54
2
        trapped.contains(":detail"),
55
        "not the pre-start abort: {trapped}"
56
    );
57
2
    assert_eq!(
58
2
        next_or_trap(rig, bumpers).await,
59
2
        "(:id 2 :stream 2 :value 3)"
60
    );
61
2
}
62

            
63
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
64
1
async fn interrupt_traps_the_running_eval_only_on_its_stream() {
65
1
    let (mut rig, bumpers) = running_rig().await;
66
1
    rig.frame("(:interrupt 1)");
67
1
    assert_trapped_then_neighbour_answers(&mut rig, &bumpers).await;
68
1
}
69

            
70
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
71
1
async fn close_traps_the_running_eval_only_on_its_stream() {
72
1
    let (mut rig, bumpers) = running_rig().await;
73
1
    rig.frame("(:close 1)");
74
1
    assert_trapped_then_neighbour_answers(&mut rig, &bumpers).await;
75
1
}
76

            
77
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
78
1
async fn abort_drops_the_running_eval_and_answers_the_queue_interrupted() {
79
1
    let (mut rig, bumpers) = running_rig().await;
80
1
    rig.mux.abort_handle().abort();
81
    let mut replies = [
82
1
        next_or_trap(&mut rig, &bumpers).await,
83
1
        next_or_trap(&mut rig, &bumpers).await,
84
    ];
85
1
    replies.sort();
86
1
    for (reply, stream) in replies.iter().zip([1, 2]) {
87
2
        assert!(reply.contains(":code interrupted"), "{reply}");
88
2
        assert!(reply.contains(&format!(":stream {stream}")), "{reply}");
89
1
    }
90
1
}
91

            
92
// One thread: only an eval that yields lets the sleep, or a reader, run while it works.
93
#[tokio::test]
94
1
async fn a_running_eval_yields_to_the_other_tasks() {
95
1
    let mut rig = rig_with(unbounded_fuel());
96
1
    rig.frame("(:id 1 :form (do ((i 0 (+ i 1))) ((>= i 1073741823) i)))");
97
1
    tokio::time::sleep(Duration::from_millis(100)).await;
98
1
    assert_eq!(
99
1
        rig.mux.permits.available_permits(),
100
1
        0,
101
        "the eval is running"
102
    );
103
1
    rig.frame("(:interrupt 0)");
104
1
    let reply = rig.next().await;
105
1
    assert!(reply.contains(":code interrupted"), "{reply}");
106
1
}