From: Bagas Sanjaya <bagasdotme@gmail.com>
To: David Gow <davidgow@google.com>,
Matti Vaittinen <mazziesaccount@gmail.com>,
Daniel Latypov <dlatypov@google.com>,
Maxime Ripard <maxime@cerno.tech>,
Benjamin Berg <benjamin@sipsolutions.net>,
Brendan Higgins <brendan.higgins@linux.dev>,
Stephen Boyd <sboyd@kernel.org>,
Shuah Khan <skhan@linuxfoundation.org>,
Rae Moar <rmoar@google.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J . Wysocki" <rafael@kernel.org>,
Heikki Krogerus <heikki.krogerus@linux.intel.com>,
Jonathan Cameron <jic23@kernel.org>,
Sadiya Kazi <sadiyakazi@google.com>,
Jonathan Corbet <corbet@lwn.net>,
linux-doc@vger.kernel.org, kunit-dev@googlegroups.com,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 4/4] Documentation: kunit: Add usage notes for kunit_add_action()
Date: Fri, 19 May 2023 15:30:47 +0700 [thread overview]
Message-ID: <ZGcztwtEvf/c0kGg@debian.me> (raw)
In-Reply-To: <20230518083849.2631178-4-davidgow@google.com>
[-- Attachment #1: Type: text/plain, Size: 2092 bytes --]
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
next prev parent reply other threads:[~2023-05-19 8:30 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-18 8:38 [PATCH v2 1/4] kunit: Add kunit_add_action() to defer a call until test exit David Gow
2023-05-18 8:38 ` [PATCH v2 2/4] kunit: executor_test: Use kunit_add_action() David Gow
2023-05-18 17:02 ` kernel test robot
2023-05-18 8:38 ` [PATCH v2 3/4] kunit: kmalloc_array: " David Gow
2023-05-18 16:24 ` kernel test robot
2023-05-18 8:38 ` [PATCH v2 4/4] Documentation: kunit: Add usage notes for kunit_add_action() David Gow
2023-05-19 8:30 ` Bagas Sanjaya [this message]
2023-05-24 21:22 ` Rae Moar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZGcztwtEvf/c0kGg@debian.me \
--to=bagasdotme@gmail.com \
--cc=benjamin@sipsolutions.net \
--cc=brendan.higgins@linux.dev \
--cc=corbet@lwn.net \
--cc=davidgow@google.com \
--cc=dlatypov@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=heikki.krogerus@linux.intel.com \
--cc=jic23@kernel.org \
--cc=kunit-dev@googlegroups.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=maxime@cerno.tech \
--cc=mazziesaccount@gmail.com \
--cc=rafael@kernel.org \
--cc=rmoar@google.com \
--cc=sadiyakazi@google.com \
--cc=sboyd@kernel.org \
--cc=skhan@linuxfoundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox