Repository navigation
tests: run the qt widget tests on the process main thread #13374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tilladam
wants to merge
2
commits into
slint-ui:master
Choose a base branch
from
tilladam:fix/qt-widgets-test-hang
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright © SixtyFPS GmbH <info@slint.dev> | ||
| // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 | ||
|
|
||
| //! A test harness that runs every test in its own process, on that process's main thread. | ||
| //! | ||
| //! libtest runs each test on a worker thread, but Qt widgets and AppKit controls only work | ||
| //! on the process main thread. | ||
| //! A test binary with `harness = false` registers its tests with `#[satchel::test]` | ||
| //! and calls [`test_main`] from its `main`. | ||
| //! The parent process forks one child per test with `--exact <name>`, | ||
| //! the convention cargo nextest uses, and the child runs that single test on its main thread. | ||
|
|
||
| // cSpell: ignore nextest | ||
|
|
||
| use libtest_mimic::{Arguments, Failed, Trial}; | ||
| use satchel::TestCase; | ||
| use std::process::{Command, Stdio}; | ||
|
|
||
| /// Runs `tests`, forking a process per test, and exits with the result. | ||
| /// The child calls `init` before its test. | ||
| pub fn test_main(tests: impl Iterator<Item = &'static TestCase>, init: fn()) { | ||
| let args = Arguments::from_args(); | ||
| let tests: Vec<_> = tests.collect(); | ||
|
|
||
| // cargo forwards `--exact <name>` to every test binary of a package, so a name | ||
| // that belongs to another binary falls through and reports zero matching tests. | ||
| if args.exact && !args.list { | ||
| let single = args | ||
| .filter | ||
| .as_deref() | ||
| .and_then(|name| tests.iter().find(|test| qualified_name(test) == name)); | ||
| if let Some(test) = single { | ||
| init(); | ||
| (test.test_fn)(); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| let trials = tests | ||
| .into_iter() | ||
| .map(|test| { | ||
| let name = qualified_name(test); | ||
| Trial::test(name.clone(), move || run_forked(&name)) | ||
| .with_ignored_flag(test.ignore.is_some()) | ||
| }) | ||
| .collect(); | ||
| libtest_mimic::run(&args, trials).exit(); | ||
| } | ||
|
|
||
| fn qualified_name(test: &TestCase) -> String { | ||
| format!("{}::{}", test.module_path, test.name) | ||
| } | ||
|
|
||
| fn run_forked(name: &str) -> Result<(), Failed> { | ||
| println!("### FORKING TEST: {name}"); | ||
| let status = Command::new(std::env::current_exe()?) | ||
| .args(["--exact", name]) | ||
| .stdout(Stdio::inherit()) | ||
| .stderr(Stdio::inherit()) | ||
| .status()?; | ||
| println!("### SUBPROCESS STATUS: {status}"); | ||
|
|
||
| if status.success() { | ||
| Ok(()) | ||
| } else { | ||
| Err(Failed::from(format!("Test {name} failed in subprocess"))) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious: why do we use a closure here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The snippets use
?, so they need a body that returnsResult, as in the#[test]branch below it.satchel registers tests as
fn()(TestFnin satchel 0.3) and doesn't wrap other return types the way libtest does.So the closure gives the snippet a
Result-returning scope, and.unwrap()turns anErrinto the panic satchel counts as a failure.I can make it a named inner
fn body() -> Result<…>plusbody().unwrap()if that reads better. It does the same thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this might no longer be true. Does this apply to the new version with the satchel test collection?
The snippet itself contains no
?and the source itself does not support?, doesn't it?At least it doesn't in the
elsecase.