From 44801c4ef63bd10137c236faff0ed12bb7c2d640 Mon Sep 17 00:00:00 2001
From: smlng <s@mlng.net>
Date: Fri, 10 Mar 2017 17:43:34 +0100
Subject: [PATCH] tests: enhance xtimer_msg_receive_timeout

    - correct test period from 100ms to 1000ms, to match description
    - moved description to separate README.md
    - changed timer message type from 44 to 42, for nerdiness
    - minor code enhancements
---
 tests/xtimer_msg_receive_timeout/README.md    |  9 ++++++
 tests/xtimer_msg_receive_timeout/main.c       | 28 ++++++++-----------
 .../tests/01-run.py                           |  4 ++-
 3 files changed, 24 insertions(+), 17 deletions(-)
 create mode 100644 tests/xtimer_msg_receive_timeout/README.md

diff --git a/tests/xtimer_msg_receive_timeout/README.md b/tests/xtimer_msg_receive_timeout/README.md
new file mode 100644
index 0000000000..064f137c0c
--- /dev/null
+++ b/tests/xtimer_msg_receive_timeout/README.md
@@ -0,0 +1,9 @@
+# test application for xtimer_msg_receive_timeout()
+
+This test will sequentially start TEST_COUNT xtimers to send a IPC msg,
+alternating with an interval of TEST_PERIOD +/- 10%. Every time a timer
+was set, it will wait for a message for at most TEST_PERIOD microseconds.
+This should succeed with a message or fail with timeout in an alternating
+manner.
+
+Default values are TEST_COUNT = 10, and TEST_PERIOD = 100ms = 100000us.
diff --git a/tests/xtimer_msg_receive_timeout/main.c b/tests/xtimer_msg_receive_timeout/main.c
index c7794c3fe7..d697b6f1b8 100644
--- a/tests/xtimer_msg_receive_timeout/main.c
+++ b/tests/xtimer_msg_receive_timeout/main.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2015 INRIA
+ *               2017 HAW Hamburg
  *
  * This file is subject to the terms and conditions of the GNU Lesser
  * General Public License v2.1. See the file LICENSE in the top level
@@ -13,13 +14,8 @@
  * @file
  * @brief       test application for xtimer_msg_receive_timeout()
  *
- *              This test will start sequentially start 10 xtimers to send a
- *              IPC msg, alternating with an interval of 900ms and 1100ms
- *              respectively. Everytime a timer was set, it will wait for a
- *              message for at most 1000ms. This should succeed and fail in an
- *              alternating manner.
- *
  * @author      Oliver Hahm <oliver.hahm@inria.fr>
+ * @author      Sebastian Meiling <s@mlng.net>
  *
  * @}
  */
@@ -30,27 +26,27 @@
 #include "xtimer.h"
 #include "timex.h"
 
-#define TEST_PERIOD (100000LU)
+#define TEST_PERIOD     (100LU * US_PER_MS) /* 100ms in US */
+#define TEST_COUNT      (10LU)
 
 int main(void)
 {
     msg_t m, tmsg;
     xtimer_t t;
-    int64_t offset = -1000;
-    tmsg.type = 44;
-    t.target = 0;
-    t.long_target = 0;
-
-    for (int i = 0; i < 10; i++) {
+    int64_t offset = -(TEST_PERIOD/10);
+    tmsg.type = 42;
+    puts("[START]");
+    for (unsigned i = 0; i < TEST_COUNT; i++) {
         xtimer_set_msg(&t, TEST_PERIOD + offset, &tmsg, sched_active_pid);
         if (xtimer_msg_receive_timeout(&m, TEST_PERIOD) < 0) {
             puts("Timeout!");
         }
         else {
-            printf("Message received: %" PRIu16 "\n", m.type);
+            printf("Message: %" PRIu16 "\n", m.type);
         }
-        offset = (offset < 0) ? 1000 : -1000;
-        xtimer_remove(&t);
+        /* flip sign */
+        offset *= (-1);
     }
+    puts("[SUCCESS]");
     return 0;
 }
diff --git a/tests/xtimer_msg_receive_timeout/tests/01-run.py b/tests/xtimer_msg_receive_timeout/tests/01-run.py
index 0fbaf595a8..37ac3704d8 100755
--- a/tests/xtimer_msg_receive_timeout/tests/01-run.py
+++ b/tests/xtimer_msg_receive_timeout/tests/01-run.py
@@ -13,9 +13,11 @@ sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
 import testrunner
 
 def testfunc(child):
+    child.expect("[START]")
     for i in range(5):
-        child.expect("Message received: 44")
+        child.expect("Message: 42")
         child.expect("Timeout!")
+    child.expect("[SUCCESS]")
 
 if __name__ == "__main__":
     sys.exit(testrunner.run(testfunc))
-- 
GitLab