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

Start to run setup() and teardown() only if they were defined #2791

Merged
merged 2 commits into from
Dec 2, 2022

Conversation

na--
Copy link
Member

@na-- na-- commented Dec 2, 2022

Previous to this PR, k6 would initialize a transient VU and try to execute setup() and teardown() even if we knew they were not defined and exported by the script. That VU was created and then immediately discarded afterwards.

It was probably done like that because, when setup() and teardown() were implemented (#457) in k6 v0.20.0, we didn't keep track of what JS functions were exported from the script. We only started doing that in k6 v0.27.0, when we added support for multiple scenarios, each with a potentially different exec function. We just didn't go back and improve the setup() and teardown() efficiency afterwards and they continued to use the "try to run it and see if it works" approach.

While it was inefficient, it didn't use to be a big problem before. However, now that we want VU initialization to be interruptible by cancelling a context (#2790), it became a problem for fine-grained unit tests. Because the setupTimeout and teardownTimeout default values are defined in the cmd/ package, tests in sub-packages like js/ and core/ saw their values as 0 😭 So, they created contexts with 0 timeouts, which immediately expired and the transient VUs for setup() and teardown() couldn't even be initialized without an error. Our config mess (#883) strikes again...

I thought about fixing the config somehow, but actually not running the setup() and teardown() functions if they were not defined seems both simpler and cleaner. Any low-level test that wants to use them can just manually define their timeouts, which most do and is usually a good idea anyway (the default 60s value is usually too much for tests).

@na-- na-- added this to the v0.42.0 milestone Dec 2, 2022
@na--
Copy link
Member Author

na-- commented Dec 2, 2022

The flaky test is fixed in #2792, will rebase after that gets merged. ✔️

This makes the tests easier to read and the init checks are important to test the current state, since that behavior will be changed in the next commit.
@na-- na-- force-pushed the run-setup-teardown-only-if-defined branch from 1a4f3fe to 876e817 Compare December 2, 2022 13:59
olegbespalov
olegbespalov previously approved these changes Dec 2, 2022
js/runner.go Outdated Show resolved Hide resolved
Previous to this commit, k6 would initialize a transient VU and try to execute setup() and teardown() even if we knew they were not defined and exported by the script. That VU was created and then immediately discarded afterwards. It was probably done like that because, when setup() and teardown() were implemented in k6 v0.20.0, we didn't keep track of what JS functions were exported from the script. We only started doing that in k6 v0.27.0, when we added support for multiple scenarios, each with a potentially different `exec` function. We just didn't go back and improve the setup() and teardown() efficiency afterwards and they continued to use the "try to run it and see if it works" approach.

While it was inefficient, it didn't use to be a big problem before. However, now that we want VU initialization to be interruptible by cancelling a context, it became a problem for fine-grained unit tests. Because the setupTimeout and teardownTimeout default values are defined in the cmd/ package, tests in sub-packages like js/ and core/ saw their values as 0. So, they created contexts with 0 timeouts, which immediately expired and the transient VUs for setup() and teardown() couldn't even be initialized without an error. The config mess strikes again...

I thought about fixing the config somehow, but actually not running the setup() and teardown() functions if they were not defined seems both simpler and cleaner. Any test low-level that wants to use them can just manually define their timeouts, which is usually a good idea to do anyway.
@na-- na-- merged commit 28c0629 into master Dec 2, 2022
@na-- na-- deleted the run-setup-teardown-only-if-defined branch December 2, 2022 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants