DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH v2 3/3] test/alarm: rewrite the alarm test
Date: Fri,  9 Aug 2024 08:24:29 -0700	[thread overview]
Message-ID: <20240809152540.9568-4-stephen@networkplumber.org> (raw)
In-Reply-To: <20240809152540.9568-1-stephen@networkplumber.org>

The alarm test had several issues:
   - only negative tests were done, no positive test to make sure
     API worked
   - the set/cancel test had too short an interval and the alarm
     might expire
   - the test should have used the standard TEST macros.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_alarm.c | 78 ++++++++++++++++++++++++++++---------------
 1 file changed, 51 insertions(+), 27 deletions(-)

diff --git a/app/test/test_alarm.c b/app/test/test_alarm.c
index 0cdfad1374..9ed8c6f72c 100644
--- a/app/test/test_alarm.c
+++ b/app/test/test_alarm.c
@@ -3,53 +3,77 @@
  */
 
 #include <stdio.h>
+#include <stdbool.h>
 #include <stdint.h>
 
-#include <rte_common.h>
 #include <rte_alarm.h>
+#include <rte_atomic.h>
+#include <rte_common.h>
+#include <rte_cycles.h>
+#include <rte_errno.h>
 
 #include "test.h"
 
-static volatile int flag;
+#ifndef US_PER_SEC
+#define US_PER_SEC	1000000u
+#endif
 
 static void
 test_alarm_callback(void *cb_arg)
 {
-	flag = 1;
-	printf("Callback setting flag - OK. [cb_arg = %p]\n", cb_arg);
+	RTE_ATOMIC(bool) *triggered = cb_arg;
+
+	rte_atomic_store_explicit(triggered, 1, rte_memory_order_release);
 }
 
+
 static int
 test_alarm(void)
 {
-	/* check if it will fail to set alarm with wrong us value */
-	printf("check if it will fail to set alarm with wrong ms values\n");
-	if (rte_eal_alarm_set(0, test_alarm_callback,
-						NULL) >= 0) {
-		printf("should not be successful with 0 us value\n");
-		return -1;
-	}
-	if (rte_eal_alarm_set(UINT64_MAX - 1, test_alarm_callback,
-						NULL) >= 0) {
-		printf("should not be successful with (UINT64_MAX-1) us value\n");
-		return -1;
-	}
+	RTE_ATOMIC(bool) triggered = false;
+	RTE_ATOMIC(bool) later = false;
+	int ret;
+
+	/* check if it will fail to set alarm with wrong us values */
+	TEST_ASSERT_FAIL(rte_eal_alarm_set(0, test_alarm_callback, NULL),
+			 "Expected rte_eal_alarm_set to fail with 0 us value");
+
+	/* check it if will fail with a very large timeout value */
+	TEST_ASSERT_FAIL(rte_eal_alarm_set(UINT64_MAX - 1, test_alarm_callback, NULL),
+			 "Expected rte_eal_alarm_set to fail with (UINT64_MAX-1) us value");
 
 	/* check if it will fail to set alarm with null callback parameter */
-	printf("check if it will fail to set alarm with null callback parameter\n");
-	if (rte_eal_alarm_set(10 /* ms */, NULL, NULL) >= 0) {
-		printf("should not be successful to set alarm with null callback parameter\n");
-		return -1;
-	}
+	TEST_ASSERT_FAIL(rte_eal_alarm_set(US_PER_SEC, NULL, NULL),
+			 "Expected rte_eal_alarm_set to fail with null callback parameter");
 
 	/* check if it will fail to remove alarm with null callback parameter */
-	printf("check if it will fail to remove alarm with null callback parameter\n");
-	if (rte_eal_alarm_cancel(NULL, NULL) == 0) {
-		printf("should not be successful to remove alarm with null callback parameter");
-		return -1;
-	}
+	TEST_ASSERT_FAIL(rte_eal_alarm_cancel(NULL, NULL),
+			 "Expected rte_eal_alarm_cancel to fail with null callback parameter");
+
+	/* check if can set a alarm for one second */
+	TEST_ASSERT_SUCCESS(rte_eal_alarm_set(US_PER_SEC, test_alarm_callback, &triggered),
+			    "Setting one second alarm failed");
+
+	/* set a longer alarm that will be canceled. */
+	TEST_ASSERT_SUCCESS(rte_eal_alarm_set(10 * US_PER_SEC, test_alarm_callback, &later),
+			    "Setting ten second alarm failed");
+
+	/* wait for alarm to happen */
+	while (rte_atomic_load_explicit(&triggered, rte_memory_order_acquire) == false)
+		rte_delay_us_sleep(US_PER_SEC / 10);
+
+	TEST_ASSERT(!rte_atomic_load_explicit(&later, rte_memory_order_acquire),
+		    "Only one alarm should have fired.");
+
+	ret = rte_eal_alarm_cancel(test_alarm_callback, &triggered);
+	TEST_ASSERT(ret == 0 && rte_errno == ENOENT,
+		    "Canceling alarm after run ret %d: %s", ret, rte_strerror(rte_errno));
+
+	ret = rte_eal_alarm_cancel(test_alarm_callback, &later);
+	TEST_ASSERT(ret == 1, "Canceling ten second alarm failed %d: %s",
+		    ret, rte_strerror(rte_errno));
 
 	return 0;
 }
 
-REGISTER_TEST_COMMAND(alarm_autotest, test_alarm);
+REGISTER_FAST_TEST(alarm_autotest, true, true, test_alarm);
-- 
2.43.0


      parent reply	other threads:[~2024-08-09 15:26 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-08 19:46 [PATCH 0/5] alarm related patches Stephen Hemminger
2024-08-08 19:46 ` [PATCH 1/5] eal: add missing parameter check to rte_eal_alarm_set on Windows Stephen Hemminger
2024-08-08 19:46 ` [PATCH 2/5] Revert "test/alarm: disable bad time cases on Windows" Stephen Hemminger
2024-08-09  7:23   ` Dmitry Kozlyuk
2024-08-09  7:33     ` Dmitry Kozlyuk
2024-08-09 14:59     ` Stephen Hemminger
2024-08-08 19:46 ` [PATCH 3/5] test: support alarm test on FreeBSD Stephen Hemminger
2024-08-08 19:47 ` [PATCH 4/5] test/alarm: rewrite the alarm test Stephen Hemminger
2024-08-08 19:47 ` [PATCH 5/5] eal: simplify eal alarm cancel by using LIST_FOREACH_SAFE Stephen Hemminger
2024-08-09  8:33   ` Morten Brørup
2024-08-09 15:00     ` Stephen Hemminger
2024-08-09 15:24 ` [PATCH v2 0/3] alarm test fixes Stephen Hemminger
2024-08-09 15:24   ` [PATCH v2 1/3] eal: add missing parameter check to rte_eal_alarm_set on Windows Stephen Hemminger
2024-08-09 15:24   ` [PATCH v2 2/3] test: support alarm test on FreeBSD Stephen Hemminger
2024-08-09 15:24   ` Stephen Hemminger [this message]

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=20240809152540.9568-4-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.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;
as well as URLs for NNTP newsgroup(s).