1
use super::*;
2
use crate::tabs::nms_eval::ConsoleEval;
3
use rpc::{ScriptCtx, ScriptLimits};
4
use sqlx::types::Uuid;
5
use std::time::Duration;
6
use tokio::time::sleep;
7

            
8
const FOREVER: &str = "(do ((i 0 (+ i 1))) ((>= i 1073741823) i))";
9

            
10
4
async fn real_app() -> App {
11
4
    let ctx = ScriptCtx::new(Uuid::nil()).with_limits(ScriptLimits {
12
4
        fuel: u64::MAX,
13
4
        ..ScriptLimits::default()
14
4
    });
15
4
    let eval = ConsoleEval::connect(&tokio::runtime::Handle::current(), ctx, rpc::Session::new)
16
4
        .expect("open the console");
17
4
    let mut app = crate::test_support::app();
18
4
    app.attach_console(eval);
19
4
    assert!(app.await_ui_config_read().await, "the theme read hung");
20
4
    app
21
4
}
22

            
23
17
fn new_console(app: &mut App) -> WindowId {
24
17
    app.open_new_window(View::Console(ConsoleState::new()))
25
17
}
26

            
27
56
fn scrollback(app: &App, console: WindowId) -> Vec<String> {
28
56
    app.win::<ConsoleState>(console)
29
56
        .map(|c| c.scrollback.clone())
30
        .unwrap_or_default()
31
56
}
32

            
33
/// Drains until `done` holds; false after two seconds of 20 ms rounds.
34
15
async fn until(app: &mut App, done: impl Fn(&App) -> bool) -> bool {
35
15
    for _ in 0..100 {
36
61
        app.drain_eval();
37
61
        if done(app) {
38
15
            return true;
39
46
        }
40
46
        sleep(Duration::from_millis(20)).await;
41
    }
42
    false
43
15
}
44

            
45
50
fn shows(app: &App, console: WindowId, text: &str) -> bool {
46
79
    scrollback(app, console).iter().any(|l| l.contains(text))
47
50
}
48

            
49
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
50
1
async fn consoles_do_not_see_each_others_definitions() {
51
1
    let mut app = real_app().await;
52
1
    let a = new_console(&mut app);
53
1
    let b = new_console(&mut app);
54
1
    app.submit_console_form(a, "(defvar x 1)".to_string());
55
5
    assert!(until(&mut app, |app| scrollback(app, a).len() > 1).await);
56
1
    app.submit_console_form(b, "x".to_string());
57
2
    assert!(until(&mut app, |app| shows(app, b, "[error]")).await);
58
1
    app.submit_console_form(b, "(+ 1 2)".to_string());
59
6
    assert!(until(&mut app, |app| shows(app, b, "3")).await);
60
1
    assert!(!shows(&app, a, "[error]"));
61
1
}
62

            
63
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
64
1
async fn interrupting_one_console_leaves_the_other_running() {
65
1
    let mut app = real_app().await;
66
1
    let a = new_console(&mut app);
67
1
    let b = new_console(&mut app);
68
1
    app.submit_console_form(a, FOREVER.to_string());
69
1
    app.submit_console_form(b, "(+ 20 22)".to_string());
70
    // Gives a misordered B time to answer, so the guard below cannot pass vacuously.
71
1
    sleep(Duration::from_millis(100)).await;
72
1
    app.drain_eval();
73
1
    assert!(!shows(&app, b, "42"), "B ran before A held the permit");
74
1
    app.raise_window(a);
75
1
    app.interrupt_console();
76
2
    assert!(until(&mut app, |app| shows(app, a, "interrupted")).await);
77
3
    assert!(until(&mut app, |app| shows(app, b, "42")).await);
78
1
    assert!(!shows(&app, b, "interrupted"));
79
1
}
80

            
81
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
82
1
async fn closing_a_console_with_a_queued_form_leaves_no_route() {
83
1
    let mut app = real_app().await;
84
1
    let a = new_console(&mut app);
85
1
    let b = new_console(&mut app);
86
1
    app.submit_console_form(a, FOREVER.to_string());
87
1
    app.submit_console_form(a, "(+ 1 2)".to_string());
88
1
    app.submit_console_form(b, "(+ 4 5)".to_string());
89
1
    app.close_window(a);
90
10
    assert!(until(&mut app, |app| app.pending_routes.is_empty()).await);
91
1
    assert_eq!(scrollback(&app, b), ["> (+ 4 5)", "9"]);
92
1
}
93

            
94
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
95
1
async fn the_console_past_the_stream_cap_is_refused_until_one_closes() {
96
1
    let mut app = real_app().await;
97
1
    let mut open = Vec::new();
98
1
    for _ in 0..7 {
99
7
        let console = new_console(&mut app);
100
7
        app.submit_console_form(console, "(+ 1 2)".to_string());
101
28
        assert!(until(&mut app, |app| shows(app, console, "3")).await);
102
7
        open.push(console);
103
    }
104
1
    let refused = new_console(&mut app);
105
1
    app.submit_console_form(refused, "(+ 1 2)".to_string());
106
1
    assert!(until(&mut app, |app| shows(app, refused, "too-many-streams")).await);
107
1
    assert!(shows(&app, refused, "close a console window"));
108
1
    app.close_window(open[0]);
109
1
    let fresh = new_console(&mut app);
110
1
    app.submit_console_form(fresh, "(+ 1 2)".to_string());
111
4
    assert!(until(&mut app, |app| shows(app, fresh, "3")).await);
112
1
}
113

            
114
#[tokio::test]
115
1
async fn app_requests_stay_on_stream_zero_beside_console_streams() {
116
1
    let mut app = crate::test_support::app();
117
1
    let sent = app.attach_echo();
118
1
    let a = new_console(&mut app);
119
1
    let b = new_console(&mut app);
120
1
    app.submit_console_form(a, "(a)".to_string());
121
1
    app.submit_console_form(b, "(b)".to_string());
122
1
    assert!(
123
1
        app.dispatch_app_eval(RouteCtx::None, "(app)".to_string())
124
1
            .is_some()
125
    );
126
1
    assert_eq!(
127
1
        sent.frames(),
128
1
        [
129
1
            "(:id 1 :stream 1 :form (a))",
130
1
            "(:id 2 :stream 2 :form (b))",
131
1
            "(:id 3 :form (app))"
132
1
        ]
133
1
    );
134
1
}