Lines
99.05 %
Functions
100 %
Branches
use super::*;
use crate::tabs::nms_eval::ConsoleEval;
use rpc::{ScriptCtx, ScriptLimits};
use sqlx::types::Uuid;
use std::time::Duration;
use tokio::time::sleep;
const FOREVER: &str = "(do ((i 0 (+ i 1))) ((>= i 1073741823) i))";
async fn real_app() -> App {
let ctx = ScriptCtx::new(Uuid::nil()).with_limits(ScriptLimits {
fuel: u64::MAX,
..ScriptLimits::default()
});
let eval = ConsoleEval::connect(&tokio::runtime::Handle::current(), ctx, rpc::Session::new)
.expect("open the console");
let mut app = crate::test_support::app();
app.attach_console(eval);
assert!(app.await_ui_config_read().await, "the theme read hung");
app
}
fn new_console(app: &mut App) -> WindowId {
app.open_new_window(View::Console(ConsoleState::new()))
fn scrollback(app: &App, console: WindowId) -> Vec<String> {
app.win::<ConsoleState>(console)
.map(|c| c.scrollback.clone())
.unwrap_or_default()
/// Drains until `done` holds; false after two seconds of 20 ms rounds.
async fn until(app: &mut App, done: impl Fn(&App) -> bool) -> bool {
for _ in 0..100 {
app.drain_eval();
if done(app) {
return true;
sleep(Duration::from_millis(20)).await;
false
fn shows(app: &App, console: WindowId, text: &str) -> bool {
scrollback(app, console).iter().any(|l| l.contains(text))
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn consoles_do_not_see_each_others_definitions() {
let mut app = real_app().await;
let a = new_console(&mut app);
let b = new_console(&mut app);
app.submit_console_form(a, "(defvar x 1)".to_string());
assert!(until(&mut app, |app| scrollback(app, a).len() > 1).await);
app.submit_console_form(b, "x".to_string());
assert!(until(&mut app, |app| shows(app, b, "[error]")).await);
app.submit_console_form(b, "(+ 1 2)".to_string());
assert!(until(&mut app, |app| shows(app, b, "3")).await);
assert!(!shows(&app, a, "[error]"));
async fn interrupting_one_console_leaves_the_other_running() {
app.submit_console_form(a, FOREVER.to_string());
app.submit_console_form(b, "(+ 20 22)".to_string());
// Gives a misordered B time to answer, so the guard below cannot pass vacuously.
sleep(Duration::from_millis(100)).await;
assert!(!shows(&app, b, "42"), "B ran before A held the permit");
app.raise_window(a);
app.interrupt_console();
assert!(until(&mut app, |app| shows(app, a, "interrupted")).await);
assert!(until(&mut app, |app| shows(app, b, "42")).await);
assert!(!shows(&app, b, "interrupted"));
async fn closing_a_console_with_a_queued_form_leaves_no_route() {
app.submit_console_form(a, "(+ 1 2)".to_string());
app.submit_console_form(b, "(+ 4 5)".to_string());
app.close_window(a);
assert!(until(&mut app, |app| app.pending_routes.is_empty()).await);
assert_eq!(scrollback(&app, b), ["> (+ 4 5)", "9"]);
async fn the_console_past_the_stream_cap_is_refused_until_one_closes() {
let mut open = Vec::new();
for _ in 0..7 {
let console = new_console(&mut app);
app.submit_console_form(console, "(+ 1 2)".to_string());
assert!(until(&mut app, |app| shows(app, console, "3")).await);
open.push(console);
let refused = new_console(&mut app);
app.submit_console_form(refused, "(+ 1 2)".to_string());
assert!(until(&mut app, |app| shows(app, refused, "too-many-streams")).await);
assert!(shows(&app, refused, "close a console window"));
app.close_window(open[0]);
let fresh = new_console(&mut app);
app.submit_console_form(fresh, "(+ 1 2)".to_string());
assert!(until(&mut app, |app| shows(app, fresh, "3")).await);
#[tokio::test]
async fn app_requests_stay_on_stream_zero_beside_console_streams() {
let sent = app.attach_echo();
app.submit_console_form(a, "(a)".to_string());
app.submit_console_form(b, "(b)".to_string());
assert!(
app.dispatch_app_eval(RouteCtx::None, "(app)".to_string())
.is_some()
);
assert_eq!(
sent.frames(),
[
"(:id 1 :stream 1 :form (a))",
"(:id 2 :stream 2 :form (b))",
"(:id 3 :form (app))"
]