Skip to content
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

test_runner: add bail out #56490

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

pmarchini
Copy link
Member

Catching up with the last attempt (#48919), this is another try at introducing the bailout feature.
I'm opening this PR as a draft to discuss the implementation and because refactoring may be needed if this approach is well-received by the community.

Note: In some tests, I had to enforce a concurrency=1 setting because testing the bailout feature across multiple files concurrently proved to be extremely flaky.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Jan 6, 2025
@pmarchini pmarchini force-pushed the test_runner/bail-out branch from 89f64db to 55874e8 Compare January 6, 2025 19:18
Comment on lines 230 to 233
this.root.harness.testsProcesses.forEach((child) => {
child.kill();
});
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace with an abort signal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @atlowChemi, sure!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have an existing signal passed to the spawned process, but that is a signal at the Test level IIRC. perhaps we can add a new controller on root.harness, and spawn with a AbortSignal.any

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!
I moved the AbortSignal.any directly into createTestTree for consistency

@cjihrig
Copy link
Contributor

cjihrig commented Jan 6, 2025

In some tests, I had to enforce a concurrency=1 setting because testing the bailout feature across multiple files concurrently proved to be extremely flaky.

Can you explain more about what was flaky? I'm guessing you mean the tests were at different points of execution when they received the bail out signal. I think the best way to work around this is to use test fixtures that never finish.

@pmarchini
Copy link
Member Author

Can you explain more about what was flaky? I'm guessing you mean the tests were at different points of execution when they received the bail out signal. I think the best way to work around this is to use test fixtures that never finish.

Hey @cjihrig, you're guessing right!

Your proposed solution sounds good.

I think we should add a test as follows:

  • First file test: A test that fails after a long timeout (maybe 5-10 seconds) to allow other file test processes to be spawned correctly.
  • Second file test: A test with an infinite loop.

WDYT?

lib/internal/test_runner/runner.js Outdated Show resolved Hide resolved
doc/api/test.md Outdated Show resolved Hide resolved
doc/api/test.md Outdated
By enabling this flag, the test runner will exit the test suite early
when it encounters the first failing test, preventing
the execution of subsequent tests.
Already running tests will be canceled, and no further tests will be started.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we need to get into this small of a detail, but technically there will be a window of time where new tests may be started before being cancelled. Maybe we can say something like "The test runner will cancel all remaining tests."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

doc/api/test.md Outdated Show resolved Hide resolved
doc/api/test.md Outdated
@@ -1483,6 +1512,11 @@ changes:
does not have a name.
* `options` {Object} Configuration options for the test. The following
properties are supported:
* `bail` {boolean}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be an option to test(). Should it be part of run() instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my bad, the bail option it's part of run. I'll update the doc ASAP !

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

test/fixtures/test-runner/output/bail-spec.js Outdated Show resolved Hide resolved
test/parallel/test-runner-run.mjs Outdated Show resolved Hide resolved
it('should only allow a boolean in options.bail', async () => {
[Symbol(), {}, [], () => { }, 0, 1, 0n, 1n, '', '1', Promise.resolve([])]
.forEach((bail) => assert.throws(() => run({ bail }), {
code: 'ERR_INVALID_ARG_TYPE'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, are we sure that these errors actually correspond to the use of bail? For example, it's possible that ERR_INVALID_ARG_TYPE or ERR_INVALID_ARG_VALUE are thrown by run() completely unrelated to the bail option.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've written the test following a red-green cycle.
I'll take another look and I'll add a comment here 😁

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to update the assertion to check the code and the message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, we should probably do the same for the other "validation" tests as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

test/parallel/test-runner-run.mjs Outdated Show resolved Hide resolved

// TODO(pmarchini): Bailout is not supported in watch mode yet but it should be.
// We should enable this test once it is supported.
it.todo('should handle the bail option with watch mode');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be documented?

@cjihrig
Copy link
Contributor

cjihrig commented Jan 6, 2025

I think we should add a test as follows:

  • First file test: A test that fails after a long timeout (maybe 5-10 seconds) to allow other file test processes to be spawned correctly.
  • Second file test: A test with an infinite loop.

There are all sorts of annoying edge cases to account for here, and it might be worth doing a survey of how the tap, mocha, and vitest runners handle bailing out when things are running in parallel. It's much more straightforward when only one thing is running. But, for example, if the very first test in the first process fails, do we bother spawning the other child processes at all? Or, in a bail out situation, how important is it to have an accurate summary at the end of the test run with correct counts for total tests, cancelled tests, etc.

@pmarchini pmarchini force-pushed the test_runner/bail-out branch from 55874e8 to f1c269c Compare January 10, 2025 17:36
@pmarchini
Copy link
Member Author

@cjihrig I'm still checking different runners and it seems that mostly the behaviour is "non-standard".
Vitest returns, even after the bail out, the list of all the skipped test files.

Mocha stops the execution returning a partial result without reporting the full list of cancelled tests.

      const result = await runMochaAsync('options/parallel/test-*', [
        '--parallel',
        '--bail'
      ]);
      // we don't know _exactly_ how many tests will be skipped here
      // due to the --bail, but the number of tests completed should be
      // less than the total, which is 5.
      return expect(
        result.passing + result.pending + result.failing,
        'to be less than',
        5
      );

Checking tests like this one I have the impression that the testing of the feature itself follows a "best effort" approach in more than one tool.

While I'm still checking other examples I think that the most common use case for the bailout is to stop as soon as possible the execution in a CI/automation env.
In this scenario, IMHO, all that matters is the "fail fast" and "fail exit".
I'm not even sure that it would make any sense at all to provide a report after a bail and, if a report is being provided, then I prefer the idea of having an output that gives an exact trace of the actual run (so partial).
WDYT?

Regarding the tests I was thinking about:

  1. sequential with single file
  2. sequential isolation none
  3. parallel with 2 test files ( 1 with some "loading-time" and 1 with infinite waiting loop )
  4. parallel with 2+x test files with concurrency fixed to 2 -> this in order to cover the behaviour of one test file that should not even start its run
  5. order of the hooks while in bail out -> to ensure the hooks are being run as supposed.

Do you have any suggestions / other behaviours you think we should ensure?

@pmarchini pmarchini marked this pull request as ready for review January 12, 2025 21:51
Copy link

codecov bot commented Jan 12, 2025

Codecov Report

Attention: Patch coverage is 94.11765% with 6 lines in your changes missing coverage. Please review.

Project coverage is 89.19%. Comparing base (062ae6f) to head (39a21ba).
Report is 43 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/test_runner/runner.js 86.36% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #56490      +/-   ##
==========================================
+ Coverage   89.12%   89.19%   +0.07%     
==========================================
  Files         662      662              
  Lines      191555   191854     +299     
  Branches    36859    36935      +76     
==========================================
+ Hits       170722   171133     +411     
+ Misses      13694    13565     -129     
- Partials     7139     7156      +17     
Files with missing lines Coverage Δ
lib/internal/test_runner/harness.js 92.83% <100.00%> (+0.16%) ⬆️
lib/internal/test_runner/reporter/spec.js 96.29% <100.00%> (+0.10%) ⬆️
lib/internal/test_runner/reporter/tap.js 95.43% <100.00%> (+0.06%) ⬆️
lib/internal/test_runner/reporter/utils.js 96.84% <100.00%> (+0.13%) ⬆️
lib/internal/test_runner/test.js 96.98% <100.00%> (+0.05%) ⬆️
lib/internal/test_runner/tests_stream.js 92.13% <100.00%> (+0.41%) ⬆️
lib/internal/test_runner/utils.js 58.79% <100.00%> (+2.58%) ⬆️
src/node_options.cc 87.94% <100.00%> (+0.01%) ⬆️
src/node_options.h 98.32% <100.00%> (+<0.01%) ⬆️
lib/internal/test_runner/runner.js 89.88% <86.36%> (+0.37%) ⬆️

... and 55 files with indirect coverage changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants