-
Notifications
You must be signed in to change notification settings - Fork 6
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
unittest2.nim: ensure the testTeardownIMPL is performed at the end #35
unittest2.nim: ensure the testTeardownIMPL is performed at the end #35
Conversation
This is important to prevent failing tests to fail in an uncontrolled way and generating a SIGSEGV (segmentation fault) Particularly, when the test body raises an exception, e.g. "assert false", what happened was that the "testTeardownIMPL" was invoked before the exception handlging and that caused errors like: Traceback (most recent call last, using override) /home/ivansete/workspace/status/nwaku/vendor/nim-unittest2/unittest2.nim(1154) unittest2 /home/ivansete/workspace/status/nwaku/vendor/nim-unittest2/unittest2.nim(1086) runDirect /home/ivansete/workspace/status/nwaku/vendor/nim-unittest2/unittest2.nim(1111) runTestX60gensym398 /home/ivansete/workspace/status/nwaku/vendor/nimbus-build-system/vendor/Nim/lib/system/excpt.nim(631) signalHandler SIGSEGV: Illegal storage access. (Attempt to read from nil?) Segmentation fault (core dumped)
fc80b2d
to
226c663
Compare
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.
Not going to approve as I don't know the extent of implications this might have in the codebase, but that's one headache less!
Thanks!
unittest2.nim
Outdated
when declared(testTeardownIMPLFlag): | ||
testTeardownIMPL() | ||
except CatchableError: | ||
checkpoint("Exception when calling testTeardownIMPL: " & getCurrentExceptionMsg()) |
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.
Using testTeardownIMPL
(in the log message) feels slightly odd to me, but I might be overanalysing things. Everyone who'll read this will (likely) be an engineer, so I don't see any danger 😆
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.
indeed, this should be teardown
, which is what the library user sees
Add test please. |
Very good morning @jangko . We've encountered the Particularly, we are now facing it in the following PR: waku-org/nwaku#2269 And the following comment indicates the precise test that is causing it: waku-org/nwaku#2269 (comment) Let me know if any info or detail is missing. |
unittest2.nim
Outdated
try: | ||
when declared(testTeardownIMPLFlag): | ||
testTeardownIMPL() | ||
except CatchableError, Defect, Exception: |
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.
Thanks @lchenut for this recommendation! I think now we will be more protected 🥳
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.
this except
line is redundant (Exception is the superclass of all exceptions) - also, we typically don't use getCurrentException*
as they have semantic problems - it's better to name the exception in the except
line
Thank you for the comments @AlejandroCabeza @arnetheduck ! I think now it is better. @jangko - I'm afraid I cannot implement a proper test that validates the desired behavior. Let me elaborate on it a bit more:
As you can see, I would need to explicitly throw an exception and make the test fail in order to force the desired flow. Something that seems incompatible with a successful test. On the other hand, the following is the undesired flow:
We had a very tricky issue from I hope that makes it clearer. |
can't we misuse |
Apologies but I can't quite understand the question. The ℹ️
On the other hand, from
|
I will merge that PR. |
What I mean is we can use the suite "a test suite":
setup:
debugEcho "SETUP"
test "something":
debugEcho "TEST"
doAssert(false)
teardown:
debugEcho "TEARDOWN"
check testStatusIMPL == TestStatus.FAILED
testStatusIMPL = TestStatus.OK |
With #35, it is no longer possible in `teardown` to refer to variables introduced in `setup`. To avoid having to rewrite tests, address the issue in #35 by wrapping `body` with an additional exception catching layer. This then allows to re-use the previous `teardown` semantics where `teardown` only ran if `setup` fully completed and also has access to the variables introduced in `setup`.
* only run `teardown` if `setup` completed With #35, it is no longer possible in `teardown` to refer to variables introduced in `setup`. To avoid having to rewrite tests, address the issue in #35 by wrapping `body` with an additional exception catching layer. This then allows to re-use the previous `teardown` semantics where `teardown` only ran if `setup` fully completed and also has access to the variables introduced in `setup`. * workaround shadowing bug * Add test --------- Co-authored-by: jangko <jangko128@gmail.com>
This is important to prevent "failing tests" from failing in an uncontrolled way and generating a SIGSEGV (segmentation fault)
Particularly, when the test body raises an exception, e.g. "assert false", what happened was that the "testTeardownIMPL" was invoked before the exception handling, and that caused errors like: