Skip to content

Commit 9687f3b

Browse files
committed
Async Tests
1 parent 1220fd0 commit 9687f3b

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) godot-rust; Bromeon and contributors.
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
*/
7+
8+
use godot::builtin::godot_task;
9+
use godot::builtin::Signal;
10+
use godot::classes::{Engine, Object, SceneTree};
11+
use godot::global::godot_print;
12+
use godot::obj::NewAlloc;
13+
14+
use crate::framework::itest;
15+
16+
async fn call_async_fn(signal: Signal) -> u8 {
17+
let value = 5;
18+
19+
let _: () = signal.to_future().await;
20+
21+
value + 5
22+
}
23+
24+
#[itest]
25+
fn start_async_task() {
26+
let tree = Engine::singleton()
27+
.get_main_loop()
28+
.unwrap()
29+
.cast::<SceneTree>();
30+
31+
let signal = Signal::from_object_signal(&tree, "process_frame");
32+
33+
godot_print!("starting godot_task...");
34+
godot_task(async move {
35+
godot_print!("running async task...");
36+
37+
godot_print!("starting nested task...");
38+
39+
let inner_signal = signal.clone();
40+
godot_task(async move {
41+
godot_print!("inside nested task...");
42+
43+
let _: () = inner_signal.to_future().await;
44+
45+
godot_print!("nested task after await...");
46+
godot_print!("nested task done!");
47+
});
48+
49+
let result = call_async_fn(signal.clone()).await;
50+
godot_print!("got async result...");
51+
52+
assert_eq!(result, 10);
53+
godot_print!("assertion done, async task complete!");
54+
});
55+
godot_print!("after godot_task...");
56+
}
57+
58+
#[itest]
59+
fn cancel_async_task() {
60+
let tree = Engine::singleton()
61+
.get_main_loop()
62+
.unwrap()
63+
.cast::<SceneTree>();
64+
65+
let signal = Signal::from_object_signal(&tree, "process_frame");
66+
67+
let handle = godot_task(async move {
68+
godot_print!("starting task to be canceled...");
69+
70+
let _: () = signal.to_future().await;
71+
72+
unreachable!();
73+
});
74+
75+
handle.cancel();
76+
}
77+
78+
#[itest]
79+
fn async_task_guaranteed_signal_future() {
80+
let mut obj = Object::new_alloc();
81+
82+
let signal = Signal::from_object_signal(&obj, "script_changed");
83+
84+
godot_task(async move {
85+
godot_print!("starting task with guaranteed signal future...");
86+
87+
let result: Option<()> = signal.to_guaranteed_future().await;
88+
89+
assert!(result.is_none());
90+
91+
godot_print!("task asserted!");
92+
});
93+
94+
obj.call_deferred("free", &[]);
95+
}

itest/rust/src/engine_tests/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8+
#[cfg(since_api = "4.2")]
9+
mod async_test;
810
mod codegen_enums_test;
911
mod codegen_test;
1012
mod engine_enum_test;

0 commit comments

Comments
 (0)