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

Add JS function to abort test #2093

Merged
merged 8 commits into from
Dec 17, 2021
Merged

Add JS function to abort test #2093

merged 8 commits into from
Dec 17, 2021

Conversation

imiric
Copy link
Contributor

@imiric imiric commented Jul 13, 2021

These are the changes from #1920, squashed and rebased on master, while changing the function from k6.abortTest() to the k6/execution module as test.abort(). Additionally a new ExecutionStatusInterrupted was added, a separate exit code (108), and a few other fixes and minor changes. See the commits for details.

Closes #1001

@codecov-commenter
Copy link

codecov-commenter commented Jul 13, 2021

Codecov Report

Merging #2093 (87b85d6) into master (04c8b1f) will decrease coverage by 0.09%.
The diff coverage is 55.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2093      +/-   ##
==========================================
- Coverage   72.11%   72.01%   -0.10%     
==========================================
  Files         180      181       +1     
  Lines       14249    14319      +70     
==========================================
+ Hits        10275    10312      +37     
- Misses       3348     3375      +27     
- Partials      626      632       +6     
Flag Coverage Δ
ubuntu 71.94% <55.42%> (-0.10%) ⬇️
windows 71.62% <55.42%> (-0.10%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
cmd/run.go 12.39% <0.00%> (-0.65%) ⬇️
js/modules/modules.go 81.81% <ø> (ø)
lib/executor/helpers.go 86.40% <38.88%> (-10.07%) ⬇️
js/common/interrupt_error.go 40.00% <40.00%> (ø)
core/local/local.go 77.57% <50.00%> (-0.63%) ⬇️
js/runner.go 82.15% <50.00%> (+0.28%) ⬆️
js/initcontext.go 86.79% <71.42%> (-1.09%) ⬇️
js/modules/k6/metrics/metrics.go 86.53% <93.10%> (+4.72%) ⬆️
core/engine.go 85.28% <0.00%> (-0.87%) ⬇️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 04c8b1f...87b85d6. Read the comment docs.

cmd/run.go Outdated Show resolved Hide resolved
@imiric
Copy link
Contributor Author

imiric commented Jul 13, 2021

Lint errors are from #2073, so ignore :)

And there are currently no tests for this, since they were all at the JS level and were moved to the extension (mostly). Should we add some lower level tests here?

Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

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

I didn't understand why you need to use my WIP PRs to test it?

Apart from that and the error comment it seems okay to me

@imiric
Copy link
Contributor Author

imiric commented Jul 22, 2021

I didn't understand why you need to use my WIP PRs to test it?

Because exec.test.abort() is based on grafana/xk6-execution#2, which depends on #2073, so I needed both your changes and these core changes to test the extension. Your commits just don't need to be merged here, so I'll remove them once this is approved, and then you can rebase on master and I'll point the extension PR to your branch instead.

@imiric imiric force-pushed the feat/1001-abort-test branch from 87b85d6 to 913c0bf Compare July 23, 2021 10:39
@mstoykov mstoykov mentioned this pull request Aug 5, 2021
@imiric imiric force-pushed the feat/1001-abort-test branch 2 times, most recently from b899f9e to 314e25c Compare August 6, 2021 15:05
@imiric imiric requested a review from mstoykov August 12, 2021 08:42
@imiric imiric changed the base branch from master to feat/common.BindReplacement August 12, 2021 08:56
mstoykov
mstoykov previously approved these changes Aug 12, 2021
Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

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

LGTM apart from the possible change to showing the summary

cmd/run.go Outdated Show resolved Hide resolved
na--
na-- previously approved these changes Aug 12, 2021
Copy link
Member

@na-- na-- left a comment

Choose a reason for hiding this comment

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

LGTM in principle, but I didn't review this very deeply, mostly relied on my memories from reviewing the old PR 😅

imiric pushed a commit that referenced this pull request Aug 13, 2021
@imiric imiric requested review from mstoykov and na-- August 13, 2021 10:10
cmd/run.go Outdated
Comment on lines 328 to 332
if runErr != nil {
if common.IsInterruptError(runErr) {
return errext.WithExitCodeIfNone(runErr, exitcodes.ScriptException)
}
return errext.WithExitCodeIfNone(runErr, exitcodes.GenericEngine)
Copy link
Contributor

Choose a reason for hiding this comment

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

this also changes that we will show the summary on non InterruptError which probably is not what we want

Copy link
Contributor Author

@imiric imiric Aug 13, 2021

Choose a reason for hiding this comment

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

No? I did that intentionally as I don't see why we shouldn't show the summary in all cases.

I'll leave this as is, but you guys feel free to change it before merging.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see some merit to showing the summary in more cases but I think this is kind of a bigger and different change so I would prefer to have it outside of this PR

cc @na--

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this in the latest force push, so now only interrupt errors should show the summary.

Base automatically changed from feat/common.BindReplacement to master August 25, 2021 14:47
@mstoykov mstoykov dismissed stale reviews from na-- and themself via 2381b70 August 25, 2021 14:47
imiric pushed a commit that referenced this pull request Sep 7, 2021
@imiric imiric force-pushed the feat/1001-abort-test branch from 2381b70 to 06cc52f Compare September 7, 2021 14:10
imiric pushed a commit that referenced this pull request Sep 17, 2021
@imiric imiric force-pushed the feat/1001-abort-test branch from 06cc52f to 710cfed Compare September 17, 2021 09:49
@imiric imiric changed the title Core changes for aborting test from script Add JS function to abort test Sep 17, 2021
@imiric
Copy link
Contributor Author

imiric commented Dec 17, 2021

@codebien I wanted to keep the original commit mostly intact, as it was authored by gernest. The changes on top of it are mostly atomic new features and shouldn't be squashed, but I can rebase it on current master and clean it up a bit. I'll update the PR description as well.

gernest and others added 8 commits December 17, 2021 10:47
Co-authored-by: Ivan Mirić <[email protected]>

Closes #1001
This adds abortTest() helper function to the k6 module. This function when
called inside a script it will

- stop the whole test run and k6 will exit with 107 status code
- stops immediately the VU that called it  and no more iterations are started
- make sure the teardown() is called
- the engine run status is 7 (RunStatusAbortedScriptError)

`(*goja.Runtime).Interrupt` is used for halting script execution and capturing
stack traces for better error message of what is happening with the script.

We introduce InterruptError which is used with `(*goja.Runtime).Interrupt` to
identify interrupts emitted by abortTest(). This way we use special handling of
this type.

Example script is

```js
import { abortTest, sleep } from 'k6';

export default function () {
    // We abort the test on second iteration
    if (__ITER == 1) {
        abortTest();
    }
    sleep(1);
}

export function teardown() {
    console.log('This function will be called even when we abort script');
}
```

abortTest() can be called in both default and setup functions, however you can't use it
in the init context

The following script will fail with the error
```
ERRO[0000] Using abortTest() in the init context is not supported at (...path to the script )init.js:13:43(34)
```

```js
import {
    abortTest
} from 'k6';

abortTest();

export function setup() {
}

export default function () {
    // ... some test logic ...
    console.log('mayday, mayday');
}
```

You can customize the reason for abortTest() by passing values to the function

```js
abortTest("Exceeded expectations");
```

Will emit `"Exceeded expectations"` on the logs
This is needed to be able to read the test run status via the API, e.g.
in the Cloud.
This ensures we exit with code 108 (ScriptAborted) in all places where
it might be called from.
Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

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

Should we add for this?

@na-- na-- added the documentation-needed A PR which will need a separate PR for documentation label Dec 17, 2021
@imiric
Copy link
Contributor Author

imiric commented Dec 17, 2021

5 months later and almost 9 months since #1920, we're finally merging this! 😅 It's a Christmas miracle! 😄🎄

Thanks for the reviews everyone! I'll try to add some documentation for this next week.

Actually, spoke too soon... The CLA not passing is blocking it 🤦‍♂️
2021-12-17-141125_1269x275_scrot

Anyone know how to retrigger it? Or should we just merge this manually?

Ah, it won't pass because gernest hasn't signed the new CLA. Since he did sign the previous one and the new one just has branding changes, should we manually merge this or ping him here?

@na--
Copy link
Member

na-- commented Dec 17, 2021

He signed the old CLA in #1920 (comment), which is basically identical to the current one for k6 (the new one only expands to cover other xk6 projects), so it's fine to merge as it is.

@imiric imiric merged commit edd149b into master Dec 17, 2021
@imiric imiric deleted the feat/1001-abort-test branch December 17, 2021 13:40
oleiade pushed a commit that referenced this pull request Jan 11, 2022
oleiade pushed a commit that referenced this pull request Jan 11, 2022
@jackgoodacre-crypto
Copy link

jackgoodacre-crypto commented Jan 13, 2022

Hey all,

Have found my way onto this PR as a result of looking for the ability to abort k6 execution with non-zero exit code. I appreciate the efforts required to develop these changes, so thank you for building on top of suggestions/requests coming from users.

I was just wondering, although this functionality is merged to master I don't believe it is available in an official version yet right? If not, are there currently any estimations as to when this may occur?

Thanks again

UPDATE 20220128: I see this is now released under v0.36.0. Tyvm

harrytwigg pushed a commit to APITeamLimited/globe-test that referenced this pull request Jan 11, 2023
harrytwigg pushed a commit to APITeamLimited/globe-test that referenced this pull request Jan 11, 2023
harrytwigg pushed a commit to APITeamLimited/globe-test that referenced this pull request Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation-needed A PR which will need a separate PR for documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide an API to exit a test
9 participants