Skip to content

Commit 24041f9

Browse files
committed
itest: add next_frame() for deferred checks
1 parent 6f1fbed commit 24041f9

File tree

1 file changed

+22
-2
lines changed
  • itest/rust/src/framework

1 file changed

+22
-2
lines changed

itest/rust/src/framework/mod.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8-
use godot::classes::{Engine, Node, Os};
8+
use godot::classes::{Engine, Node, Os, SceneTree};
99
use godot::obj::Gd;
1010
use godot::sys;
1111
use std::collections::HashSet;
@@ -254,6 +254,27 @@ pub fn expect_debug_panic_or_release_ok(_context: &str, code: impl FnOnce()) {
254254
code()
255255
}
256256

257+
/// Run code asynchronously, immediately before the next _process_ frame.
258+
///
259+
/// Useful for assertions that run expect a `call_deferred()` or similar operation, and still want to check the result.
260+
#[cfg(since_api = "4.2")]
261+
#[must_use]
262+
#[allow(dead_code)] // not yet used.
263+
pub fn next_frame<F>(code: F) -> godot::task::TaskHandle
264+
where
265+
F: FnOnce() + 'static,
266+
{
267+
let tree = Engine::singleton()
268+
.get_main_loop()
269+
.unwrap()
270+
.cast::<SceneTree>();
271+
272+
godot::task::spawn(async move {
273+
let _: () = tree.signals().process_frame().to_future().await;
274+
code();
275+
})
276+
}
277+
257278
/// Synchronously run a thread and return result. Panics are propagated to caller thread.
258279
#[track_caller]
259280
pub fn quick_thread<R, F>(f: F) -> R
@@ -262,7 +283,6 @@ where
262283
R: Send + 'static,
263284
{
264285
let handle = std::thread::spawn(f);
265-
266286
match handle.join() {
267287
Ok(result) => result,
268288
Err(panic_payload) => {

0 commit comments

Comments
 (0)