kunit: Deferred action helpers
cover kunit: Deferred action helpers
This is v1 of the KUnit deferred actions API, which implements an equivalent of devm_add_action[1] on top of KUnit managed resources. This provides a simple way of scheduling a function to run when the test terminates (whether successfully, or with an error). It's therefore very useful for freeing resources, or otherwise cleaning up.
The notable changes since RFCv2[2] are:
- Got rid of the 'cancellation token' concept. It was overcomplicated, and we can add it back if we need to.
- kunit_add_action() therefore now returns 0 on success, and an error otherwise (like devm_add_action()). Though you may wish to use:
- Added kunit_add_action_or_reset(), which will call the deferred function if an error occurs. (See devm_add_action_or_reset()). This also returns an error on failure, which can be asserted safely.
- Got rid of the function pointer typedef. Personally, I liked it, but it's more typedef-y than most kernel code.
- Got rid of the 'internal_gfp' argument: all internal state is now allocated with GFP_KERNEL. The main KUnit resource API can be used instead if this doesn't work for your use-case.
I'd love to hear any further thoughts!
Cheers,
-- David
[1]: https://docs.kernel.org/driver-api/basics.html#c.devm_add_action
[2]: https://patchwork.kernel.org/project/linux-kselftest/list/?series=735720
David Gow (3):
kunit: Add kunit_add_action() to defer a call until test exit
kunit: executor_test: Use kunit_add_action()
kunit: kmalloc_array: Use kunit_add_action()
include/kunit/resource.h | 76 +++++++++++++++++++++++++++++++ include/kunit/test.h | 10 ++++- lib/kunit/executor_test.c | 11 ++--- lib/kunit/kunit-test.c | 88 +++++++++++++++++++++++++++++++++++- lib/kunit/resource.c | 95 +++++++++++++++++++++++++++++++++++++++ lib/kunit/test.c | 48 ++++---------------- 6 files changed, 279 insertions(+), 49 deletions(-)
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
PATCH 1/3 kunit: Add kunit_add_action() to defer a call until test exit
Many uses of the KUnit resource system are intended to simply defer calling a function until the test exits (be it due to success or failure). The existing kunit_alloc_resource() function is often used for this, but was awkward to use (requiring passing NULL init functions, etc), and returned a resource without incrementing its reference count, which -- while okay for this use-case -- could cause problems in others.
Instead, introduce a simple kunit_add_action() API: a simple function (returning nothing, accepting a single void* argument) can be scheduled to be called when the test exits. Deferred actions are called in the opposite order to that which they were registered.
This mimics the devres API, devm_add_action(), and also provides kunit_remove_action(), to cancel a deferred action, and kunit_release_action() to trigger one early.
This is implemented as a resource under the hood, so the ordering between resource cleanup and deferred functions is maintained.
---
Note: there is a checkpatch warning:
"function definition argument 'void *' should also have an identifier name"
I think this is a false-positive, as we're defining a function pointer struct member, not a specific function.
Changes since RFC v2:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Got rid of internal_gfp
- everything uses GFP_KERNEL now
- This includes kunit_kzalloc() and friends, which still allocate the returned memory with the provided GFP, but use GFP_KERNEL for internal bookkeeping data.
- Thanks Maxime & Benjamin!
- Got rid of cancellation tokens.
- kunit_add_action() now returns 0 on success, otherwise an error
- Note that this can quite easily lead to a memory leak, so look at kunit_add_action_or_reset()
- Thanks Maxime & Benjamin!
- Added kunit_add_action_or_reset
- Matches devm_add_action_or_reset()
- Returns 0 on success.
- Thanks Maxime & Benjamin!
- Got rid of the kunit_defer_func_t typedef.
- I liked it, but it is probably pushing the boundaries of kernel style.
- Use (void (*)(void *)) instead.
Changes since RFC v1:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Rename functions to better match the devm_* APIs. (Thanks Maxime)
- Embed the kunit_resource in struct kunit_action_ctx to avoid an extra allocation (Thanks Benjamin)
- Use 'struct kunit_action_ctx' as the type for cancellation tokens (Thanks Benjamin)
- Add tests.
include/kunit/resource.h | 76 ++++++++++++++++++++++++++++++++ lib/kunit/kunit-test.c | 88 ++++++++++++++++++++++++++++++++++++- lib/kunit/resource.c | 95 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 258 insertions(+), 1 deletion(-)
include/kunit/resource.h +76 -0
@@ -387,4 +387,80 @@ static inline int kunit_destroy_named_resource(struct kunit *test,*/ void kunit_remove_resource (struct kunit *test,struct kunit_resource *res); + +/** +* kunit_add_action() - Defer an 'action' (function call) until the test ends. +* @test: Test case to associate the action with. +* @func: The function to run on test exit +* @ctx: Data passed into @func +* +* Defer the execution of a function until the test exits, either normally or +* due to a failure. @ctx is passed as additional context. All functions +* registered with kunit_add_action() will execute in the opposite order to that +* they were registered in. +* +* This is useful for cleaning up allocated memory and resources. +* +* Returns: +* 0 on success, an error if the action could not be deferred. +*/ +int kunit_add_action (struct kunit *test,void (*action) (void *),void *ctx); + +/** +* kunit_add_action_or_reset() - Defer an 'action' (function call) until the test ends. +* @test: Test case to associate the action with. +* @func: The function to run on test exit +* @ctx: Data passed into @func +* +* Defer the execution of a function until the test exits, either normally or +* due to a failure. @ctx is passed as additional context. All functions +* registered with kunit_add_action() will execute in the opposite order to that +* they were registered in. +* +* This is useful for cleaning up allocated memory and resources. +* +* If the action cannot be created (e.g., due to the system being out of memory), +* then action(ctx) will be called immediately, and an error will be returned. +* +* Returns: +* 0 on success, an error if the action could not be deferred. +*/ +int kunit_add_action_or_reset (struct kunit *test,void (*action)(void *), +void *ctx); + +/** +* kunit_remove_action() - Cancel a matching deferred action. +* @test: Test case the action is associated with. +* @func: The deferred function to cancel. +* @ctx: The context passed to the deferred function to trigger. +* +* Prevent an action deferred via kunit_add_action() from executing when the +* test terminates.. +* If the function/context pair was deferred multiple times, only the most +* recent one will be cancelled. +*/ +void kunit_remove_action (struct kunit *test, +void (*action)(void *), +void *ctx); + +/** +* kunit_release_action() - Run a matching action call immediately. +* @test: Test case the action is associated with. +* @func: The deferred function to trigger. +* @ctx: The context passed to the deferred function to trigger. +* +* Execute a function deferred via kunit_add_action()) immediately, rather than +* when the test ends. +* If the function/context pair was deferred multiple times, it will only be +* executed once here. The most recent deferral will no longer execute when +* the test ends. +* +* kunit_release_action(test, func, ctx); +* is equivalent to +* func(ctx); +* kunit_remove_action(test, func, ctx); +*/ +void kunit_release_action (struct kunit *test, +void (*action)(void *), +void *ctx);#endif /* _KUNIT_RESOURCE_H */
lib/kunit/kunit-test.c +87 -1
106 lines not on this page · raw message
lib/kunit/resource.c +95 -0
98 lines not on this page · raw message
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
PATCH 2/3 kunit: executor_test: Use kunit_add_action()
In reply to David GowNow we have the kunit_add_action() function, we can use it to implement kfree_at_end() and free_subsuite_at_end() without the need for extra helper functions.
---
Changes since RFCv2:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Don't use the no-longer-extant kunit_defer_func_t typedef.
- Don't pass a GFP pointer in.
--- lib/kunit/executor_test.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
lib/kunit/executor_test.c +4 -7
@@ -125,11 +125,6 @@ kunit_test_suites(&executor_test_suite);/* Test helpers */ -static void kfree_res_free (struct kunit_resource *res) -{ -kfree (res->data); -} -/* Use the resource API to register a call to kfree(to_free). * Since we never actually use the resource, it's safe to use on const data. */
@@ -138,8 +133,10 @@ static void kfree_at_end(struct kunit *test, const void *to_free)/* kfree() handles NULL already, but avoid allocating a no-op cleanup. */ if (IS_ERR_OR_NULL (to_free))return ; -kunit_alloc_resource (test,NULL , kfree_res_free,GFP_KERNEL , - (void *)to_free); + +kunit_add_action (test, + (void (*)(void *))kfree, + (void *)to_free); } static struct kunit_suite *alloc_fake_suite(struct kunit *test,
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
PATCH 3/3 kunit: kmalloc_array: Use kunit_add_action()
In reply to David GowThe kunit_add_action() function is much simpler and cleaner to use that the full KUnit resource API for simple things like the kunit_kmalloc_array() functionality.
Replacing it allows us to get rid of a number of helper functions, and leaves us with no uses of kunit_alloc_resource(), which has some usability problems and is going to have its behaviour modified in an upcoming patch.
Note that we need to use kunit_release_action() to implement kunit_kfree().
---
Changes since RFCv2:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Update to match changes in the the action API.
- Always allocate the action context with GFP_KERNEL.
- Update documentation to note that this will cause GFP_KERNEL allocations, regardless of the gfp argument passed in.
--- include/kunit/test.h | 10 +++++++-- lib/kunit/test.c | 48 +++++++++----------------------------------- 2 files changed, 17 insertions(+), 41 deletions(-)
include/kunit/test.h +8 -2
@@ -321,8 +321,11 @@ enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);* @gfp: flags passed to underlying kmalloc(). * * Just like `kmalloc_array(...)`, except the allocation is managed by the test case -* and is automatically cleaned up after the test case concludes. See &struct -* kunit_resource for more information. +* and is automatically cleaned up after the test case concludes. See kunit_add_action() +* for more information. +* +* Note that some internal context data is also allocated with GFP_KERNEL, +* regardless of the gfp passed in. */ void *kunit_kmalloc_array (struct kunit *test,size_t n,size_t size,gfp_t gfp);
@@ -333,6 +336,9 @@ void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);* @gfp: flags passed to underlying kmalloc(). * * See kmalloc() and kunit_kmalloc_array() for more information. +* +* Note that some internal context data is also allocated with GFP_KERNEL, +* regardless of the gfp passed in. */ static inline void *kunit_kmalloc (struct kunit *test,size_t size,gfp_t gfp) {
lib/kunit/test.c +9 -39
67 lines not on this page · raw message
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
Quoted 20 lines · David Gow' via KUnit Development
This is v1 of the KUnit deferred actions API, which implements an equivalent of devm_add_action[1] on top of KUnit managed resources. This provides a simple way of scheduling a function to run when the test terminates (whether successfully, or with an error). It's therefore very useful for freeing resources, or otherwise cleaning up.
The notable changes since RFCv2[2] are:
- Got rid of the 'cancellation token' concept. It was overcomplicated, and we can add it back if we need to.
-kunit_add_action()therefore now returns 0 on success, and an error otherwise (likedevm_add_action()). Though you may wish to use:
- Addedkunit_add_action_or_reset(), which will call the deferred function if an error occurs. (Seedevm_add_action_or_reset()). This also returns an error on failure, which can be asserted safely.
- Got rid of the function pointer typedef. Personally, I liked it, but it's more typedef-y than most kernel code.
- Got rid of the 'internal_gfp' argument: all internal state is now allocated withGFP_KERNEL. The main KUnit resource API can be used instead if this doesn't work for your use-case.
Quoted 1 line
I'd love to hear any further thoughts!
I am happy with it as-is.
Quoted 24 lines
Cheers,
-- David[1]: https://docs.kernel.org/driver-api/basics.html#c.devm_add_action
[2]: https://patchwork.kernel.org/project/linux-kselftest/list/?series=735720David Gow (3):
kunit: Addkunit_add_action()to defer a call until test exit
kunit:executor_test: Usekunit_add_action()
kunit:kmalloc_array: Usekunit_add_action()include/kunit/resource.h | 76 +++++++++++++++++++++++++++++++ include/kunit/test.h | 10 ++++-
lib/kunit/executor_test.c| 11 ++---
lib/kunit/kunit-test.c | 88 +++++++++++++++++++++++++++++++++++- lib/kunit/resource.c | 95 +++++++++++++++++++++++++++++++++++++++ lib/kunit/test.c | 48 ++++----------------6 files changed, 279 insertions(+), 49 deletions(-)Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
Hi David,
Quoted 21 lines · Benjamin Berg
Quoted 20 lines · David Gow' via KUnit Development
This is v1 of the KUnit deferred actions API, which implements an equivalent of devm_add_action[1] on top of KUnit managed resources. This provides a simple way of scheduling a function to run when the test terminates (whether successfully, or with an error). It's therefore very useful for freeing resources, or otherwise cleaning up.
The notable changes since RFCv2[2] are:
- Got rid of the 'cancellation token' concept. It was overcomplicated, and we can add it back if we need to.
-kunit_add_action()therefore now returns 0 on success, and an error otherwise (likedevm_add_action()). Though you may wish to use:
- Addedkunit_add_action_or_reset(), which will call the deferred function if an error occurs. (Seedevm_add_action_or_reset()). This also returns an error on failure, which can be asserted safely.
- Got rid of the function pointer typedef. Personally, I liked it, but it's more typedef-y than most kernel code.
- Got rid of the 'internal_gfp' argument: all internal state is now allocated withGFP_KERNEL. The main KUnit resource API can be used instead if this doesn't work for your use-case.
Quoted 3 lines
Quoted 1 line
I'd love to hear any further thoughts!
I am happy with it as-is.
Oh, wait. Nothing big, but I just noticed that the new API functions seem to not yet be exported using EXPORT_SYMBOL_GPL.
Benjamin
Quoted 31 lines
Reviewed-by Benjamin Berg <[email protected]>Quoted 28 lines
Cheers,
-- David[1]:
https://docs.kernel.org/driver-api/basics.html#c.devm_add_action
[2]:
https://patchwork.kernel.org/project/linux-kselftest/list/?series=735720David Gow (3):
kunit: Addkunit_add_action()to defer a call until test exit
kunit:executor_test: Usekunit_add_action()
kunit:kmalloc_array: Usekunit_add_action()include/kunit/resource.h | 76 +++++++++++++++++++++++++++++++ include/kunit/test.h | 10 ++++-
lib/kunit/executor_test.c| 11 ++--- lib/kunit/kunit-test.c | 88
+++++++++++++++++++++++++++++++++++-
lib/kunit/resource.c | 95
+++++++++++++++++++++++++++++++++++++++
lib/kunit/test.c | 48 ++++----------------6 files changed, 279 insertions(+), 49 deletions(-)Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
Hi,
Quoted 20 lines · David Gow
This is v1 of the KUnit deferred actions API, which implements an equivalent of devm_add_action[1] on top of KUnit managed resources. This provides a simple way of scheduling a function to run when the test terminates (whether successfully, or with an error). It's therefore very useful for freeing resources, or otherwise cleaning up.
The notable changes since RFCv2[2] are:
- Got rid of the 'cancellation token' concept. It was overcomplicated, and we can add it back if we need to.
-kunit_add_action()therefore now returns 0 on success, and an error otherwise (likedevm_add_action()). Though you may wish to use:
- Addedkunit_add_action_or_reset(), which will call the deferred function if an error occurs. (Seedevm_add_action_or_reset()). This also returns an error on failure, which can be asserted safely.
- Got rid of the function pointer typedef. Personally, I liked it, but it's more typedef-y than most kernel code.
- Got rid of the 'internal_gfp' argument: all internal state is now allocated withGFP_KERNEL. The main KUnit resource API can be used instead if this doesn't work for your use-case.
Quoted 1 line
I'd love to hear any further thoughts!
I've converted the KMS kunit tests to use that API when relevant, and it works like a charm and is super usable, thanks so much.
One improvement we could do as a second step is to provide a kunit_action_t type or something to make casting kfree-like functions easier, but it's already great overall.
Thanks again,
Maxime
Re: [PATCH v1 1/3] kunit: Add kunit_add_action() to defer a call until test exit
In reply to David GowQuoted 8 lines · David Gow
Many uses of the KUnit resource system are intended to simply defer calling a function until the test exits (be it due to success or failure). The existing
kunit_alloc_resource()function is often used for this, but was awkward to use (requiring passing NULL init functions, etc), and returned a resource without incrementing its reference count, which -- while okay for this use-case -- could cause problems in others.
Quoted 8 lines
Instead, introduce a simple
kunit_add_action()API: a simple function (returning nothing, accepting a single void* argument) can be scheduled to be called when the test exits. Deferred actions are called in the opposite order to that which they were registered.This mimics the devres API,
devm_add_action(), and also provideskunit_remove_action(), to cancel a deferred action, andkunit_release_action()to trigger one early.
Apologies for the delayed bikeshedding.
I think mimicking the devres API is a better idea than kunit_defer() and friends.
But I can't help but think this still isn't the best name. I personally would have no idea what kunit_release_action() does without looking it up.
I feel like kunit_add_cleanup() probably works better for a unit test framework.
I think kunit_remove_cleanup() is fine and kunit_release_cleanup() is questionably ok.
Instead of release, maybe it should be kunit_trigger_cleanup() or more verbosely, something like kunit_early_trigger_cleanup().
I tried to look for equivalents in other languages/frameworks:
* Rust and C++ rely on RAII, don't think they have equivalents in testing libs
* Python has self.addCleanup(),
https://docs.python.org/3/library/unittest.html#unittest.TestCase.addCleanup
* Go has t.Cleanup(), https://pkg.go.dev/testing#T.Cleanup
* Looking at Zig since it also has a defer, I guess they just use that, I don't see anything in
https://ziglang.org/documentation/master/std/#A;std:testing
* I know nothing about JUnit, but a quick search seems like they rely on @After and @AfterClass annotations,
https://junit.org/junit4/javadoc/4.12/org/junit/After.html
* I know even less about HUnit, but it looks like it relies on wrapping things via the IO monad,
https://hackage.haskell.org/package/HUnit-1.6.2.0/docs/Test-HUnit-Base.html#t:AssertionPredicate
* Since we were inspired by TAP, I tried to look at Perl, but didn't immediately see anything that looked equivalent,
https://metacpan.org/pod/Test::Most
Quoted 6 lines
This is implemented as a resource under the hood, so the ordering between resource cleanup and deferred functions is maintained.
Signed-off-by David Gow <[email protected]>---
<snip>
Quoted 11 lines
include/kunit/resource.h +3
-0@@ -387,4 +387,80 @@ static inline int kunit_destroy_named_resource(struct kunit *test,*/ void kunit_remove_resource (struct kunit *test,struct kunit_resource *res); + +/** +* kunit_add_action() - Defer an 'action' (function call) until the test ends.
include/kunit/resource.h static inline int kunit_destroy_named_resource(struct kunit *test, · line 402
Quoted 10 lines
include/kunit/resource.h +10
-0+* @test: Test case to associate the action with. +* @func: The function to run on test exit +* @ctx: Data passed into @func +* +* Defer the execution of a function until the test exits, either normally or +* due to a failure. @ctx is passed as additional context. All functions +* registered with kunit_add_action() will execute in the opposite order to that +* they were registered in. +* +* This is useful for cleaning up allocated memory and resources.
Re renaming to kunit_add_cleanup(), I think this makes writing the comment easier.
E.g.
- kunit_add_action() - Defer an 'action' (function call) until the test ends.
+ kunit_add_cleanup() - Call a function when the test ends.
+ ...
+ This is useful for cleaning up allocated memory and resources.
Daniel
Quoted 25 lines · Benjamin Berg
Hi David,
Quoted 21 lines · Benjamin Berg
Quoted 20 lines · David Gow' via KUnit Development
This is v1 of the KUnit deferred actions API, which implements an equivalent of devm_add_action[1] on top of KUnit managed resources. This provides a simple way of scheduling a function to run when the test terminates (whether successfully, or with an error). It's therefore very useful for freeing resources, or otherwise cleaning up.
The notable changes since RFCv2[2] are:
- Got rid of the 'cancellation token' concept. It was overcomplicated, and we can add it back if we need to.
-kunit_add_action()therefore now returns 0 on success, and an error otherwise (likedevm_add_action()). Though you may wish to use:
- Addedkunit_add_action_or_reset(), which will call the deferred function if an error occurs. (Seedevm_add_action_or_reset()). This also returns an error on failure, which can be asserted safely.
- Got rid of the function pointer typedef. Personally, I liked it, but it's more typedef-y than most kernel code.
- Got rid of the 'internal_gfp' argument: all internal state is now allocated withGFP_KERNEL. The main KUnit resource API can be used instead if this doesn't work for your use-case.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
I'd love to hear any further thoughts!
I am happy with it as-is.
Oh, wait. Nothing big, but I just noticed that the new API functions seem to not yet be exported using
EXPORT_SYMBOL_GPL.
Ah, nice catch! I'll add those to the next version.
Cheers,
-- David
Quoted 24 lines · Maxime Ripard
Hi,
Quoted 20 lines · David Gow
This is v1 of the KUnit deferred actions API, which implements an equivalent of devm_add_action[1] on top of KUnit managed resources. This provides a simple way of scheduling a function to run when the test terminates (whether successfully, or with an error). It's therefore very useful for freeing resources, or otherwise cleaning up.
The notable changes since RFCv2[2] are:
- Got rid of the 'cancellation token' concept. It was overcomplicated, and we can add it back if we need to.
-kunit_add_action()therefore now returns 0 on success, and an error otherwise (likedevm_add_action()). Though you may wish to use:
- Addedkunit_add_action_or_reset(), which will call the deferred function if an error occurs. (Seedevm_add_action_or_reset()). This also returns an error on failure, which can be asserted safely.
- Got rid of the function pointer typedef. Personally, I liked it, but it's more typedef-y than most kernel code.
- Got rid of the 'internal_gfp' argument: all internal state is now allocated withGFP_KERNEL. The main KUnit resource API can be used instead if this doesn't work for your use-case.
Quoted 4 lines
Quoted 1 line
I'd love to hear any further thoughts!
I've converted the KMS kunit tests to use that API when relevant, and it works like a charm and is super usable, thanks so much.
Nice! I'm glad it's working well.
Quoted 4 lines
One improvement we could do as a second step is to provide a
kunit_action_ttype or something to make casting kfree-like functions easier, but it's already great overall.
I had that in an earlier version and got rid of it to better match what devm_* was doing, but I personally agree that it's nice to have. I'll add it back in the next version.
Quoted 2 lines
Reviewed-by Maxime Ripard <[email protected]>Tested-by Maxime Ripard <[email protected]>
Cheers,
-- David
Re: [PATCH v1 1/3] kunit: Add kunit_add_action() to defer a call until test exit
In reply to Daniel LatypovQuoted 27 lines · Daniel Latypov
Quoted 8 lines · David Gow
Many uses of the KUnit resource system are intended to simply defer calling a function until the test exits (be it due to success or failure). The existing
kunit_alloc_resource()function is often used for this, but was awkward to use (requiring passing NULL init functions, etc), and returned a resource without incrementing its reference count, which -- while okay for this use-case -- could cause problems in others.Quoted 8 lines
Instead, introduce a simple
kunit_add_action()API: a simple function (returning nothing, accepting a single void* argument) can be scheduled to be called when the test exits. Deferred actions are called in the opposite order to that which they were registered.This mimics the devres API,
devm_add_action(), and also provideskunit_remove_action(), to cancel a deferred action, andkunit_release_action()to trigger one early.Apologies for the delayed bikeshedding.
I think mimicking the devres API is a better idea than
kunit_defer()and friends.
But I can't help but think this still isn't the best name. I personally would have no idea whatkunit_release_action()does without looking it up.
Quoted 6 lines
I feel like
kunit_add_cleanup()probably works better for a unit test framework.
I thinkkunit_remove_cleanup()is fine andkunit_release_cleanup()is questionably ok.
Instead ofrelease, maybe it should bekunit_trigger_cleanup()or more verbosely, something likekunit_early_trigger_cleanup().
Hmm... While personally I prefer 'defer' or 'cleanup' to 'action' in isolation, I think the benefits of matching the devm_ API probably exceed the benefits of a slightly better name here.
I'm less convinced by the _release_action() and _remove_action()
names: I definitely think 'trigger' is more obvious here. I hope that, with some extra documentation, we can nevertheless make this consistent with devm_*, but it's definitely suboptimal.
Quoted 10 lines
I tried to look for equivalents in other languages/frameworks:
* Rust and C++ rely on RAII, don't think they have equivalents in testing libs
* Python hasself.addCleanup(),
https://docs.python.org/3/library/unittest.html#unittest.TestCase.addCleanup
* Go hast.Cleanup(), https://pkg.go.dev/testing#T.Cleanup
* Looking at Zig since it also has adefer, I guess they just use that, I don't see anything in
https://ziglang.org/documentation/master/std/#A;std:testing
* I know nothing about JUnit, but a quick search seems like they rely
Quoted 9 lines
on @After and @AfterClass annotations,
https://junit.org/junit4/javadoc/4.12/org/junit/After.html
* I know even less about HUnit, but it looks like it relies on wrapping things via the IO monad,
https://hackage.haskell.org/package/HUnit-1.6.2.0/docs/Test-HUnit-Base.html#t:AssertionPredicate
* Since we were inspired by TAP, I tried to look at Perl, but didn't immediately see anything that looked equivalent,
https://metacpan.org/pod/Test::Most
Thanks for putting that together. It looks like cleanup is the winner here, followed maybe by defer. I'd been using 'cleanup' to refer to the sum total of all deferred functions, resource free functions, and the test 'exit()' function (i.e., everything which runs after a failed assertion), so I don't want to totally confuse the issue.
Regardless, it's probably worth at least having a mention in the documentation that these are referred to as a cleanup in Python/Go/etc, and are vaguely equivalent to 'defer' in Go and Zig.
Quoted 34 lines
Quoted 6 lines
This is implemented as a resource under the hood, so the ordering between resource cleanup and deferred functions is maintained.
Signed-off-by David Gow <[email protected]>---
<snip>
Quoted 11 lines
include/kunit/resource.h +3
-0@@ -387,4 +387,80 @@ static inline int kunit_destroy_named_resource(struct kunit *test,*/ void kunit_remove_resource (struct kunit *test,struct kunit_resource *res); + +/** +* kunit_add_action() - Defer an 'action' (function call) until the test ends. include/kunit/resource.h static inline int kunit_destroy_named_resource(struct kunit *test, · line 402
Quoted 10 lines
include/kunit/resource.h +10
-0+* @test: Test case to associate the action with. +* @func: The function to run on test exit +* @ctx: Data passed into @func +* +* Defer the execution of a function until the test exits, either normally or +* due to a failure. @ctx is passed as additional context. All functions +* registered with kunit_add_action() will execute in the opposite order to that +* they were registered in. +* +* This is useful for cleaning up allocated memory and resources. Re renaming to
kunit_add_cleanup(), I think this makes writing the comment easier.
Quoted 6 lines
E.g.
-kunit_add_action()- Defer an 'action' (function call) until the test ends.
+kunit_add_cleanup()- Call a function when the test ends.
+ ...
+ This is useful for cleaning up allocated memory and resources.
Good point. I think we can probably use the better description here even if we don't rename the function.
Cheers,
-- David
Quoted 1 line
Daniel