test: cover file task delivery, replacement, cancellation and provider failures
Verify Tessera GUI / verify (push) Failing after 9m15s
Verify Tessera GUI / verify (push) Failing after 9m15s
This commit is contained in:
@@ -2031,6 +2031,167 @@ page.render(block)
|
||||
);
|
||||
}
|
||||
|
||||
fn with_file_task_input(contents: &str, run: impl FnOnce(&std::path::Path)) {
|
||||
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
||||
let id = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
let path =
|
||||
std::env::temp_dir().join(format!("tessera-text-task-{}-{id}.txt", std::process::id()));
|
||||
struct Cleanup(std::path::PathBuf);
|
||||
impl Drop for Cleanup {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_file(&self.0);
|
||||
}
|
||||
}
|
||||
let _cleanup = Cleanup(path.clone());
|
||||
std::fs::write(&path, contents).unwrap();
|
||||
run(&path);
|
||||
}
|
||||
fn file_task_source(path: &std::path::Path, tail: &str) -> String {
|
||||
let path = serde_json::to_string(&path.to_string_lossy()).unwrap();
|
||||
format!(
|
||||
"class Loader {{\n target: TextBlock\n task: Object\n function apply(self: Loader): Unit {{ self.target.text = self.task.result() }}\n}}\ntarget = TextBlock(\"waiting\")\nloader = Loader(target,ui.state(0))\npage = Page()\npage.render(TextBlock(\"target stays hidden\"))\nloader.task = ui.readText(loader,\"read\",{path},loader.apply)\n{tail}"
|
||||
)
|
||||
}
|
||||
fn file_task_state(session: &mut ReplSession) -> String {
|
||||
let globals = session.candidate.globals.unwrap();
|
||||
let Value::Object(loader) = session
|
||||
.candidate
|
||||
.vm
|
||||
.dict_get(globals, &Value::Text("loader".into()))
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
else {
|
||||
panic!("loader");
|
||||
};
|
||||
let Value::Object(task) = session.candidate.vm.get_attr(loader, "task").unwrap() else {
|
||||
panic!("Task");
|
||||
};
|
||||
let definition = session
|
||||
.candidate
|
||||
.vm
|
||||
.object_store()
|
||||
.type_of(task)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let method = session
|
||||
.candidate
|
||||
.vm
|
||||
.object_store()
|
||||
.lookup_method(definition, "status")
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let Value::Text(status) = session
|
||||
.candidate
|
||||
.vm
|
||||
.call(method, &[Value::Object(task)])
|
||||
.unwrap()
|
||||
else {
|
||||
panic!("status");
|
||||
};
|
||||
status
|
||||
}
|
||||
fn complete_file_task(session: &mut ReplSession) {
|
||||
for _ in 0..1000 {
|
||||
session.poll_tasks().unwrap();
|
||||
if file_task_state(session) != "pending" {
|
||||
return;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
}
|
||||
panic!("file task did not complete");
|
||||
}
|
||||
#[test]
|
||||
fn file_task_updates_hidden_object_and_replacement_drops_old_delivery() {
|
||||
with_file_task_input("old", |old| {
|
||||
with_file_task_input("new UTF-8 文本", |new| {
|
||||
let path = serde_json::to_string(&new.to_string_lossy()).unwrap();
|
||||
let tail = format!(
|
||||
"old = loader.task\nloader.task = ui.readText(loader,\"read\",{path},loader.apply)"
|
||||
);
|
||||
let (mut session, _) =
|
||||
ReplSession::load(&file_task_source(old, &tail), "app.main", 282, 1).unwrap();
|
||||
complete_file_task(&mut session);
|
||||
assert_eq!(file_task_state(&mut session), "completed");
|
||||
assert_eq!(
|
||||
session.execute("target.text", "app.main").unwrap().value,
|
||||
"\"new UTF-8 文本\""
|
||||
);
|
||||
assert_eq!(
|
||||
session.execute("old.status()", "app.main").unwrap().value,
|
||||
"\"cancelled\""
|
||||
);
|
||||
assert_eq!(
|
||||
session
|
||||
.execute("target.mounts().count", "app.main")
|
||||
.unwrap()
|
||||
.value,
|
||||
"0"
|
||||
);
|
||||
})
|
||||
});
|
||||
}
|
||||
#[test]
|
||||
fn cancelled_file_task_never_runs_callback_and_missing_file_keeps_vm_live() {
|
||||
with_file_task_input("ignored", |path| {
|
||||
let (mut cancelled, _) = ReplSession::load(
|
||||
&file_task_source(path, "loader.task.cancel()"),
|
||||
"app.main",
|
||||
283,
|
||||
1,
|
||||
)
|
||||
.unwrap();
|
||||
complete_file_task(&mut cancelled);
|
||||
assert_eq!(file_task_state(&mut cancelled), "cancelled");
|
||||
assert_eq!(
|
||||
cancelled.execute("target.text", "app.main").unwrap().value,
|
||||
"\"waiting\""
|
||||
);
|
||||
let missing = path.with_extension("missing");
|
||||
let (mut failed, _) =
|
||||
ReplSession::load(&file_task_source(&missing, ""), "app.main", 284, 1).unwrap();
|
||||
complete_file_task(&mut failed);
|
||||
assert_eq!(file_task_state(&mut failed), "failed");
|
||||
assert!(!failed.is_faulted());
|
||||
assert_ne!(
|
||||
failed
|
||||
.execute("loader.task.error()", "app.main")
|
||||
.unwrap()
|
||||
.value,
|
||||
"\"\""
|
||||
);
|
||||
assert_eq!(
|
||||
failed.execute("target.text", "app.main").unwrap().value,
|
||||
"\"waiting\""
|
||||
);
|
||||
});
|
||||
}
|
||||
#[test]
|
||||
fn callback_free_file_task_retains_result_after_completion() {
|
||||
with_file_task_input("callback free", |path| {
|
||||
let path = serde_json::to_string(&path.to_string_lossy()).unwrap();
|
||||
let source = format!(
|
||||
"class Holder {{ task: Object }}\nloader = Holder(ui.state(0))\npage = Page()\npage.render(TextBlock(\"file\"))\nloader.task = ui.readText(ui,\"read\",{path})"
|
||||
);
|
||||
let (mut session, _) = ReplSession::load(&source, "app.main", 285, 1).unwrap();
|
||||
complete_file_task(&mut session);
|
||||
assert_eq!(file_task_state(&mut session), "completed");
|
||||
assert_eq!(
|
||||
session
|
||||
.execute("loader.task.result()", "app.main")
|
||||
.unwrap()
|
||||
.value,
|
||||
"\"callback free\""
|
||||
);
|
||||
assert_eq!(
|
||||
session
|
||||
.execute("loader.task.error()", "app.main")
|
||||
.unwrap()
|
||||
.value,
|
||||
"\"\""
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn owned_tasks_replace_cancel_and_update_unmounted_objects() {
|
||||
let source = r#"class Action {
|
||||
|
||||
Reference in New Issue
Block a user