Merged
Conversation
Adds a new T.Cleanup method to register cleanup functions. These will run after each iteration of a Check. The implementation of how cleanup functions are tracked and run is borrowed heavily from testing.T's own implementation. Namely: - [T.Cleanup puts functions in a slice](https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/testing/testing.go;l=1214-1216) - [cleanups are run in reverse order](https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/testing/testing.go;l=1433-1446) popping functions from the slice one by one - [panics are not allowed to interrupt cleanup](https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/testing/testing.go;l=1420-1427) As with `testing.T`, cleanup runs after the context is canceled. Because Rapid's context is lazily initialized, it needs an additional check to avoid providing a valid context during cleanup. Resolves flyingmutant#62
abhinav
commented
Feb 20, 2025
Comment on lines
+254
to
+261
| // ignoreErrorsTB is a TB that ignores all errors posted to it. | ||
| type ignoreErrorsTB struct{ TB } | ||
|
|
||
| func (ignoreErrorsTB) Error(...interface{}) {} | ||
| func (ignoreErrorsTB) Errorf(string, ...interface{}) {} | ||
| func (ignoreErrorsTB) Fatal(...interface{}) {} | ||
| func (ignoreErrorsTB) Fatalf(string, ...interface{}) {} | ||
| func (ignoreErrorsTB) Fail() {} |
Contributor
Author
There was a problem hiding this comment.
needed to ignore test failure from panic
| t.mu.Unlock() | ||
|
|
||
| if recurse { | ||
| t.cleanup() |
Owner
There was a problem hiding this comment.
Is this black magic taken from testing?
Contributor
Author
There was a problem hiding this comment.
Yeah, from here: https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/testing/testing.go;l=1420-1427
(Minus the two recovery modes. We don't need divergent behavior in this case.)
This is will basically still let the panic propagate outwards as a regular panic (we don't recover), but it'll make sure all cleanup functions are at least invoked.
Contributor
Author
|
Took another pass over the documentation for both functions. |
Owner
|
Looks good, merged! |
ftrvxmtrx
pushed a commit
to PingThingsIO/rapid
that referenced
this pull request
Jun 25, 2025
* feat: Add T.Cleanup Adds a new T.Cleanup method to register cleanup functions. These will run after each iteration of a Check. The implementation of how cleanup functions are tracked and run is borrowed heavily from testing.T's own implementation. Namely: - [T.Cleanup puts functions in a slice](https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/testing/testing.go;l=1214-1216) - [cleanups are run in reverse order](https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/testing/testing.go;l=1433-1446) popping functions from the slice one by one - [panics are not allowed to interrupt cleanup](https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/testing/testing.go;l=1420-1427) As with `testing.T`, cleanup runs after the context is canceled. Because Rapid's context is lazily initialized, it needs an additional check to avoid providing a valid context during cleanup. Resolves flyingmutant#62 * doc: try to be clearer
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds a new T.Cleanup method to register cleanup functions.
These will run after each iteration of a Check or custom generator.
The implementation of how cleanup functions are tracked and run
is borrowed heavily from testing.T's own implementation.
Namely:
As with
testing.T, cleanup runs after the context is canceled.Because Rapid's Context is lazily initialized,
it needs an additional check to avoid providing a valid context
during cleanup.
Resolves #62