2023-05-18 08:59:10

by David Gow

[permalink] [raw]
Subject: [PATCH v2 4/4] Documentation: kunit: Add usage notes for kunit_add_action()

Add some basic documentation for kunit_add_action() and related
deferred action functions.

Signed-off-by: David Gow <[email protected]>
---

This patch is new in v2.

---
Documentation/dev-tools/kunit/usage.rst | 51 +++++++++++++++++++++++++
1 file changed, 51 insertions(+)

diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst
index 46957d1cbcbb..c2f0ed648385 100644
--- a/Documentation/dev-tools/kunit/usage.rst
+++ b/Documentation/dev-tools/kunit/usage.rst
@@ -615,6 +615,57 @@ For example:
KUNIT_ASSERT_STREQ(test, buffer, "");
}

+Registering Cleanup Actions
+---------------------------
+
+If you need to perform some cleanup beyond simple use of ``kunit_kzalloc``,
+you can register a cusom "deferred action", which is a cleanup function
+run when the test exits (whether cleanly, or via a failed assertion).
+
+Actions are simple functions with no return value, and a single ``void*``
+context argument, and forfil the same role as "cleanup" functions in Python
+and Go tests, "defer" statements in languages which support them, and
+(in some cases) destructors in RAII languages.
+
+These are very useful for unregistering things from global lists, closing
+files or other resources, or freeing resources.
+
+For example:
+
+.. code-block:: C
+
+ static void cleanup_device(void *ctx)
+ {
+ struct device *dev = (struct device *)ctx;
+
+ device_unregister(dev);
+ }
+
+ void example_device_test(struct kunit *test)
+ {
+ struct my_device dev;
+
+ device_register(&dev);
+
+ kunit_add_action(test, &cleanup_device, &dev);
+ }
+
+Note that, for functions like device_unregister which only accept a single
+pointer-sized argument, it's possible to directly cast that function to
+a ``kunit_action_t`` rather than writing a wrapper function, for example:
+
+.. code-block:: C
+
+ kunit_add_action(test, (kunit_action_t *)&device_unregister, &dev);
+
+``kunit_add_action`` can fail if, for example, the system is out of memory.
+You can use ``kunit_add_action_or_reset`` instead which runs the action
+immediately if it cannot be deferred.
+
+If you need more control over when the cleanup function is called, you
+can trigger it early using ``kunit_release_action``, or cancel it entirely
+with ``kunit_remove_action``.
+

Testing Static Functions
------------------------
--
2.40.1.698.g37aff9b760-goog



2023-05-19 08:51:21

by Bagas Sanjaya

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] Documentation: kunit: Add usage notes for kunit_add_action()

On Thu, May 18, 2023 at 04:38:46PM +0800, David Gow wrote:
> +Registering Cleanup Actions
> +---------------------------
> +
> +If you need to perform some cleanup beyond simple use of ``kunit_kzalloc``,
> +you can register a cusom "deferred action", which is a cleanup function
> +run when the test exits (whether cleanly, or via a failed assertion).
> +
> +Actions are simple functions with no return value, and a single ``void*``
> +context argument, and forfil the same role as "cleanup" functions in Python
"... fulfill the same role ..."?

> +and Go tests, "defer" statements in languages which support them, and
> +(in some cases) destructors in RAII languages.
> +
> +These are very useful for unregistering things from global lists, closing
> +files or other resources, or freeing resources.
> +
> +For example:
> +
> +.. code-block:: C
> +
> + static void cleanup_device(void *ctx)
> + {
> + struct device *dev = (struct device *)ctx;
> +
> + device_unregister(dev);
> + }
> +
> + void example_device_test(struct kunit *test)
> + {
> + struct my_device dev;
> +
> + device_register(&dev);
> +
> + kunit_add_action(test, &cleanup_device, &dev);
> + }
> +
> +Note that, for functions like device_unregister which only accept a single
> +pointer-sized argument, it's possible to directly cast that function to
> +a ``kunit_action_t`` rather than writing a wrapper function, for example:
> +
> +.. code-block:: C
> +
> + kunit_add_action(test, (kunit_action_t *)&device_unregister, &dev);
> +
> +``kunit_add_action`` can fail if, for example, the system is out of memory.
> +You can use ``kunit_add_action_or_reset`` instead which runs the action
> +immediately if it cannot be deferred.
> +
> +If you need more control over when the cleanup function is called, you
> +can trigger it early using ``kunit_release_action``, or cancel it entirely
> +with ``kunit_remove_action``.
> +
>
> Testing Static Functions
> ------------------------

The rest is LGTM.

--
An old man doll... just what I always wanted! - Clara


Attachments:
(No filename) (2.04 kB)
signature.asc (235.00 B)
Download all attachments

2023-05-24 21:25:27

by Rae Moar

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] Documentation: kunit: Add usage notes for kunit_add_action()

On Thu, May 18, 2023 at 4:39 AM David Gow <[email protected]> wrote:
>
> Add some basic documentation for kunit_add_action() and related
> deferred action functions.

Hi David!

This looks good to me. Just two typos below. Thanks!

Reviewed-by: Rae Moar <[email protected]>

>
> Signed-off-by: David Gow <[email protected]>
> ---
>
> This patch is new in v2.
>
> ---
> Documentation/dev-tools/kunit/usage.rst | 51 +++++++++++++++++++++++++
> 1 file changed, 51 insertions(+)
>
> diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst
> index 46957d1cbcbb..c2f0ed648385 100644
> --- a/Documentation/dev-tools/kunit/usage.rst
> +++ b/Documentation/dev-tools/kunit/usage.rst
> @@ -615,6 +615,57 @@ For example:
> KUNIT_ASSERT_STREQ(test, buffer, "");
> }
>
> +Registering Cleanup Actions
> +---------------------------
> +
> +If you need to perform some cleanup beyond simple use of ``kunit_kzalloc``,
> +you can register a cusom "deferred action", which is a cleanup function

Looks like a typo here: "custom"

> +run when the test exits (whether cleanly, or via a failed assertion).
> +
> +Actions are simple functions with no return value, and a single ``void*``
> +context argument, and forfil the same role as "cleanup" functions in Python

Another small typo here as Bagas noted.

> +and Go tests, "defer" statements in languages which support them, and
> +(in some cases) destructors in RAII languages.
> +
> +These are very useful for unregistering things from global lists, closing
> +files or other resources, or freeing resources.
> +
> +For example:
> +
> +.. code-block:: C
> +
> + static void cleanup_device(void *ctx)
> + {
> + struct device *dev = (struct device *)ctx;
> +
> + device_unregister(dev);
> + }
> +
> + void example_device_test(struct kunit *test)
> + {
> + struct my_device dev;
> +
> + device_register(&dev);
> +
> + kunit_add_action(test, &cleanup_device, &dev);
> + }
> +
> +Note that, for functions like device_unregister which only accept a single
> +pointer-sized argument, it's possible to directly cast that function to
> +a ``kunit_action_t`` rather than writing a wrapper function, for example:
> +
> +.. code-block:: C
> +
> + kunit_add_action(test, (kunit_action_t *)&device_unregister, &dev);
> +
> +``kunit_add_action`` can fail if, for example, the system is out of memory.
> +You can use ``kunit_add_action_or_reset`` instead which runs the action
> +immediately if it cannot be deferred.
> +
> +If you need more control over when the cleanup function is called, you
> +can trigger it early using ``kunit_release_action``, or cancel it entirely
> +with ``kunit_remove_action``.
> +
>
> Testing Static Functions
> ------------------------
> --
> 2.40.1.698.g37aff9b760-goog
>