From d0d8434d500bfdc0f3b318fd5aa3638283eaee8f Mon Sep 17 00:00:00 2001 From: Jett Rink Date: Mon, 14 Oct 2019 10:45:27 -0600 Subject: test: don't rely on system time for tests Instead of relying on the host's clock, we need to monotonically increase a timestamp. This gives tests predictability. BRANCH=none BUG=chromium:1013701 TEST=ran usb_prl over 100 times without failure Change-Id: Id4a1e9a8cbd9dd44509747916c9073444d71af5c Signed-off-by: Jett Rink Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1860474 Tested-by: Edward Hill Reviewed-by: Denis Brockus Reviewed-by: Edward Hill --- core/host/timer.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/host/timer.c b/core/host/timer.c index e76e4b3e3b..f7067dce08 100644 --- a/core/host/timer.c +++ b/core/host/timer.c @@ -52,12 +52,16 @@ void usleep(unsigned us) timestamp_t _get_time(void) { - struct timespec ts; - timestamp_t ret; - clock_gettime(CLOCK_MONOTONIC, &ts); - ret.val = (1000000000 * (uint64_t)ts.tv_sec + ts.tv_nsec) * - TEST_TIME_SCALE / 1000 / TEST_TIME_SLOW_DOWN; - return ret; + static timestamp_t time; + + /* + * We just monotonically increase the microsecond every time we check + * the time. Do not depend on host system time as this introduces + * flakyness in tests. The time is periodically fast forwarded with + * force_time() during the host's task scheduler implementation. + */ + ++time.val; + return time; } test_mockable timestamp_t get_time(void) -- cgit v1.2.1