Merge pull request #7419 from yuhaoth/test/random-time-test-fail
Workaround random `test_suite_platform` fail in time test
diff --git a/tests/suites/test_suite_platform.data b/tests/suites/test_suite_platform.data
index 557b586..39b423f 100644
--- a/tests/suites/test_suite_platform.data
+++ b/tests/suites/test_suite_platform.data
@@ -7,6 +7,3 @@
Time: delay milliseconds
time_delay_milliseconds:1000
-
-Time: delay seconds
-time_delay_seconds:1
diff --git a/tests/suites/test_suite_platform.function b/tests/suites/test_suite_platform.function
index 54ddd42..7453c32 100644
--- a/tests/suites/test_suite_platform.function
+++ b/tests/suites/test_suite_platform.function
@@ -38,8 +38,6 @@
/* END_DEPENDENCIES */
-
-
/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
void time_get_milliseconds()
{
@@ -76,6 +74,14 @@
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
+
+/*
+ * WARNING: DO NOT ENABLE THIS TEST. We keep the code here to document the
+ * reason.
+ *
+ * The test often failed on the CI. See #1517. CI failures cannot be
+ * completely avoided due to out-of-sync clock sources.
+ */
void time_delay_seconds(int delay_secs)
{
mbedtls_time_t current = mbedtls_time(NULL);
@@ -84,7 +90,23 @@
sleep_ms(delay_secs * 1000);
elapsed_secs = mbedtls_time(NULL) - current;
- TEST_ASSERT(elapsed_secs >= delay_secs && elapsed_secs < 4 + delay_secs);
+
+ /*
+ * `mbedtls_time()` was defined as c99 function `time()`, returns the number
+ * of seconds since the Epoch. And it is affected by discontinuous changes
+ * from automatic drift adjustment or time setting system call. The POSIX.1
+ * specification for clock_settime says that discontinuous changes in
+ * CLOCK_REALTIME should not affect `nanosleep()`.
+ *
+ * If discontinuous changes occur during `nanosleep()`, we will get
+ * `elapsed_secs < delay_secs` for backward or `elapsed_secs > delay_secs`
+ * for forward.
+ *
+ * The following tolerance windows cannot be guaranteed.
+ * PLEASE DO NOT ENABLE IT IN CI TEST.
+ */
+ TEST_ASSERT(elapsed_secs - delay_secs >= -1 &&
+ elapsed_secs - delay_secs < 4);
/* This goto is added to avoid warnings from the generated code. */
goto exit;
}