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 87ac584
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 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"
42 changes: 29 additions & 13 deletions gtk4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,37 @@ 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))
.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(not(target_os = "macos"))]
{
let (tx, rx) = std::sync::mpsc::sync_channel(1);
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");
rx.recv()
.expect("Failed to receive result from thread pool")
.unwrap_or_else(|e| std::panic::resume_unwind(e))
}
#[cfg(target_os = "macos")]
{
QUEUE
.exec_sync(move || std::panic::catch_unwind(function))
.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 87ac584

Please sign in to comment.