diff mbox series

[1/4] kunit: Introduce kunit_is_running()

Message ID 20240821144305.1958-2-michal.wajdeczko@intel.com (mailing list archive)
State New
Delegated to: Brendan Higgins
Headers show
Series kunit: Add macros to help write more complex tests | expand

Commit Message

Michal Wajdeczko Aug. 21, 2024, 2:43 p.m. UTC
Wrap uses of the static key 'kunit_running' into a helper macro
to allow future checks to be placed in the code residing outside
of the CONFIG_KUNIT. We will start using this in upcoming patch.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
---
Cc: Daniel Latypov <dlatypov@google.com>
Cc: David Gow <davidgow@google.com>
Cc: Lucas De Marchi <lucas.demarchi@intel.com>
---
 include/kunit/test-bug.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

Comments

Rae Moar Aug. 21, 2024, 9:21 p.m. UTC | #1
On Wed, Aug 21, 2024 at 10:43 AM Michal Wajdeczko
<michal.wajdeczko@intel.com> wrote:
>
> Wrap uses of the static key 'kunit_running' into a helper macro
> to allow future checks to be placed in the code residing outside
> of the CONFIG_KUNIT. We will start using this in upcoming patch.
>
> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>

Hello!

I am good with this. It is definitely a prettier way to access kunit_running.

Reviewed-by: Rae Moar <rmoar@google.com>

Thanks!

-Rae

> ---
> Cc: Daniel Latypov <dlatypov@google.com>
> Cc: David Gow <davidgow@google.com>
> Cc: Lucas De Marchi <lucas.demarchi@intel.com>
> ---
>  include/kunit/test-bug.h | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/include/kunit/test-bug.h b/include/kunit/test-bug.h
> index 47aa8f21ccce..e8ea3bab7250 100644
> --- a/include/kunit/test-bug.h
> +++ b/include/kunit/test-bug.h
> @@ -25,6 +25,13 @@ extern struct kunit_hooks_table {
>         void *(*get_static_stub_address)(struct kunit *test, void *real_fn_addr);
>  } kunit_hooks;
>
> +/**
> + * kunit_is_running() - True, if KUnit test is currently running.
> + *
> + * If CONFIG_KUNIT is not enabled, it will compile down to a false.
> + */
> +#define kunit_is_running() static_branch_unlikely(&kunit_running)
> +
>  /**
>   * kunit_get_current_test() - Return a pointer to the currently running
>   *                           KUnit test.
> @@ -40,7 +47,7 @@ extern struct kunit_hooks_table {
>   */
>  static inline struct kunit *kunit_get_current_test(void)
>  {
> -       if (!static_branch_unlikely(&kunit_running))
> +       if (!kunit_is_running())
>                 return NULL;
>
>         return current->kunit_test;
> @@ -53,7 +60,7 @@ static inline struct kunit *kunit_get_current_test(void)
>   * If a KUnit test is running in the current task, mark that test as failed.
>   */
>  #define kunit_fail_current_test(fmt, ...) do {                                 \
> -               if (static_branch_unlikely(&kunit_running)) {                   \
> +               if (kunit_is_running()) {                                       \
>                         /* Guaranteed to be non-NULL when kunit_running true*/  \
>                         kunit_hooks.fail_current_test(__FILE__, __LINE__,       \
>                                                   fmt, ##__VA_ARGS__);          \
> @@ -64,6 +71,7 @@ static inline struct kunit *kunit_get_current_test(void)
>
>  static inline struct kunit *kunit_get_current_test(void) { return NULL; }
>
> +#define kunit_is_running() false
>  #define kunit_fail_current_test(fmt, ...) do {} while (0)
>
>  #endif
> --
> 2.43.0
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20240821144305.1958-2-michal.wajdeczko%40intel.com.
Lucas De Marchi Aug. 21, 2024, 9:24 p.m. UTC | #2
On Wed, Aug 21, 2024 at 04:43:02PM GMT, Michal Wajdeczko wrote:
>Wrap uses of the static key 'kunit_running' into a helper macro
>to allow future checks to be placed in the code residing outside
>of the CONFIG_KUNIT. We will start using this in upcoming patch.
>
>Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>


Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>

Lucas De Marchi

>---
>Cc: Daniel Latypov <dlatypov@google.com>
>Cc: David Gow <davidgow@google.com>
>Cc: Lucas De Marchi <lucas.demarchi@intel.com>
>---
> include/kunit/test-bug.h | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>
>diff --git a/include/kunit/test-bug.h b/include/kunit/test-bug.h
>index 47aa8f21ccce..e8ea3bab7250 100644
>--- a/include/kunit/test-bug.h
>+++ b/include/kunit/test-bug.h
>@@ -25,6 +25,13 @@ extern struct kunit_hooks_table {
> 	void *(*get_static_stub_address)(struct kunit *test, void *real_fn_addr);
> } kunit_hooks;
>
>+/**
>+ * kunit_is_running() - True, if KUnit test is currently running.
>+ *
>+ * If CONFIG_KUNIT is not enabled, it will compile down to a false.
>+ */
>+#define kunit_is_running() static_branch_unlikely(&kunit_running)
>+
> /**
>  * kunit_get_current_test() - Return a pointer to the currently running
>  *			      KUnit test.
>@@ -40,7 +47,7 @@ extern struct kunit_hooks_table {
>  */
> static inline struct kunit *kunit_get_current_test(void)
> {
>-	if (!static_branch_unlikely(&kunit_running))
>+	if (!kunit_is_running())
> 		return NULL;
>
> 	return current->kunit_test;
>@@ -53,7 +60,7 @@ static inline struct kunit *kunit_get_current_test(void)
>  * If a KUnit test is running in the current task, mark that test as failed.
>  */
> #define kunit_fail_current_test(fmt, ...) do {					\
>-		if (static_branch_unlikely(&kunit_running)) {			\
>+		if (kunit_is_running()) {					\
> 			/* Guaranteed to be non-NULL when kunit_running true*/	\
> 			kunit_hooks.fail_current_test(__FILE__, __LINE__,	\
> 						  fmt, ##__VA_ARGS__);		\
>@@ -64,6 +71,7 @@ static inline struct kunit *kunit_get_current_test(void)
>
> static inline struct kunit *kunit_get_current_test(void) { return NULL; }
>
>+#define kunit_is_running() false
> #define kunit_fail_current_test(fmt, ...) do {} while (0)
>
> #endif
>-- 
>2.43.0
>
David Gow Aug. 22, 2024, 6:13 a.m. UTC | #3
On Wed, 21 Aug 2024 at 22:43, Michal Wajdeczko
<michal.wajdeczko@intel.com> wrote:
>
> Wrap uses of the static key 'kunit_running' into a helper macro
> to allow future checks to be placed in the code residing outside
> of the CONFIG_KUNIT. We will start using this in upcoming patch.
>
> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> ---
> Cc: Daniel Latypov <dlatypov@google.com>
> Cc: David Gow <davidgow@google.com>
> Cc: Lucas De Marchi <lucas.demarchi@intel.com>
> ---

This is a big improvement, thanks!

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David


>  include/kunit/test-bug.h | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/include/kunit/test-bug.h b/include/kunit/test-bug.h
> index 47aa8f21ccce..e8ea3bab7250 100644
> --- a/include/kunit/test-bug.h
> +++ b/include/kunit/test-bug.h
> @@ -25,6 +25,13 @@ extern struct kunit_hooks_table {
>         void *(*get_static_stub_address)(struct kunit *test, void *real_fn_addr);
>  } kunit_hooks;
>
> +/**
> + * kunit_is_running() - True, if KUnit test is currently running.
> + *
> + * If CONFIG_KUNIT is not enabled, it will compile down to a false.
> + */
> +#define kunit_is_running() static_branch_unlikely(&kunit_running)
> +
>  /**
>   * kunit_get_current_test() - Return a pointer to the currently running
>   *                           KUnit test.
> @@ -40,7 +47,7 @@ extern struct kunit_hooks_table {
>   */
>  static inline struct kunit *kunit_get_current_test(void)
>  {
> -       if (!static_branch_unlikely(&kunit_running))
> +       if (!kunit_is_running())
>                 return NULL;
>
>         return current->kunit_test;
> @@ -53,7 +60,7 @@ static inline struct kunit *kunit_get_current_test(void)
>   * If a KUnit test is running in the current task, mark that test as failed.
>   */
>  #define kunit_fail_current_test(fmt, ...) do {                                 \
> -               if (static_branch_unlikely(&kunit_running)) {                   \
> +               if (kunit_is_running()) {                                       \
>                         /* Guaranteed to be non-NULL when kunit_running true*/  \
>                         kunit_hooks.fail_current_test(__FILE__, __LINE__,       \
>                                                   fmt, ##__VA_ARGS__);          \
> @@ -64,6 +71,7 @@ static inline struct kunit *kunit_get_current_test(void)
>
>  static inline struct kunit *kunit_get_current_test(void) { return NULL; }
>
> +#define kunit_is_running() false
>  #define kunit_fail_current_test(fmt, ...) do {} while (0)
>
>  #endif
> --
> 2.43.0
>
diff mbox series

Patch

diff --git a/include/kunit/test-bug.h b/include/kunit/test-bug.h
index 47aa8f21ccce..e8ea3bab7250 100644
--- a/include/kunit/test-bug.h
+++ b/include/kunit/test-bug.h
@@ -25,6 +25,13 @@  extern struct kunit_hooks_table {
 	void *(*get_static_stub_address)(struct kunit *test, void *real_fn_addr);
 } kunit_hooks;
 
+/**
+ * kunit_is_running() - True, if KUnit test is currently running.
+ *
+ * If CONFIG_KUNIT is not enabled, it will compile down to a false.
+ */
+#define kunit_is_running() static_branch_unlikely(&kunit_running)
+
 /**
  * kunit_get_current_test() - Return a pointer to the currently running
  *			      KUnit test.
@@ -40,7 +47,7 @@  extern struct kunit_hooks_table {
  */
 static inline struct kunit *kunit_get_current_test(void)
 {
-	if (!static_branch_unlikely(&kunit_running))
+	if (!kunit_is_running())
 		return NULL;
 
 	return current->kunit_test;
@@ -53,7 +60,7 @@  static inline struct kunit *kunit_get_current_test(void)
  * If a KUnit test is running in the current task, mark that test as failed.
  */
 #define kunit_fail_current_test(fmt, ...) do {					\
-		if (static_branch_unlikely(&kunit_running)) {			\
+		if (kunit_is_running()) {					\
 			/* Guaranteed to be non-NULL when kunit_running true*/	\
 			kunit_hooks.fail_current_test(__FILE__, __LINE__,	\
 						  fmt, ##__VA_ARGS__);		\
@@ -64,6 +71,7 @@  static inline struct kunit *kunit_get_current_test(void)
 
 static inline struct kunit *kunit_get_current_test(void) { return NULL; }
 
+#define kunit_is_running() false
 #define kunit_fail_current_test(fmt, ...) do {} while (0)
 
 #endif