Skip to content

Commit

Permalink
macos: Use dispatch to spawn serial tests tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Oct 14, 2023
1 parent b7b16a4 commit 3fb2632
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ jobs:
with:
command: clippy
args: --features v4_12,xml_validation --manifest-path ./gtk4/Cargo.toml
# FIXME: renable once https://github.com/gtk-rs/gtk4-rs/issues/1235 is fixed
#- name: Tests
# uses: actions-rs/cargo@v1
# with:
# command: test
# args: --features v4_8,xml_validation
- name: Tests
uses: actions-rs/cargo@v1
with:
command: test
args: --features v4_12,xml_validation --manifest-path ./gtk4/Cargo.toml
3 changes: 3 additions & 0 deletions gtk4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ gtk4-macros = {path = "../gtk4-macros", version = "0.8"}
libc = "0.2"
pango = {git = "https://github.com/gtk-rs/gtk-rs-core", version = "0.19", features = ["v1_46"]}

[target.'cfg(target_os = "macos")'.dependencies]
dispatch = "0.2.0"

[dev-dependencies]
gir-format-check = "^0.1"
35 changes: 26 additions & 9 deletions gtk4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,38 @@ where
{
skip_assert_initialized!();

use std::panic;
use std::sync::mpsc;

let (tx, rx) = mpsc::sync_channel(1);
TEST_THREAD_WORKER
.push(move || {
tx.send(panic::catch_unwind(function))
let (tx, rx) = std::sync::mpsc::sync_channel(1);
#[cfg(not(target_os = "macos"))]
{
TEST_THREAD_WORKER
.push(move || {
tx.send(std::panic::catch_unwind(function))
.unwrap_or_else(|_| panic!("Failed to return result from thread pool"));
})
.expect("Failed to schedule a test call");
}
#[cfg(target_os = "macos")]
{
QUEUE.exec_async(move || {
tx.send(std::panic::catch_unwind(function))
.unwrap_or_else(|_| panic!("Failed to return result from thread pool"));
})
.expect("Failed to schedule a test call");
});
}
rx.recv()
.expect("Failed to receive result from thread pool")
.unwrap_or_else(|e| std::panic::resume_unwind(e))
}
#[cfg(target_os = "macos")]
static QUEUE: glib::once_cell::sync::Lazy<dispatch::Queue> =
glib::once_cell::sync::Lazy::new(|| {
let queue = dispatch::Queue::main();
queue.exec_sync(move || {
crate::init().expect("Failed to initialize GTK");
});
queue
});

#[cfg(not(target_os = "macos"))]
static TEST_THREAD_WORKER: glib::once_cell::sync::Lazy<glib::ThreadPool> =
glib::once_cell::sync::Lazy::new(|| {
let pool = glib::ThreadPool::exclusive(1).unwrap();
Expand Down

0 comments on commit 3fb2632

Please sign in to comment.