summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurens Valk <laurens@pybricks.com>2022-11-24 08:58:10 +0100
committerLaurens Valk <laurens@pybricks.com>2022-11-25 08:14:54 +0100
commit3c1a2a942a3264da7861891eb9852cdd2a889666 (patch)
treeaef9a13436693e06e097338d4bbb279ceac277c3
parent5588647ad28ea51682fafab50b95ae9f161d18fe (diff)
tests/misc/cexample_class: Fix timing sensitivity.
This test could occasionally fail because some operations take longer than expected. This relaxes the timing constraints and defers printing until the very end. Signed-off-by: Laurens Valk <laurens@pybricks.com>
-rw-r--r--tests/misc/cexample_class.py16
-rw-r--r--tests/misc/cexample_class.py.exp1
2 files changed, 11 insertions, 6 deletions
diff --git a/tests/misc/cexample_class.py b/tests/misc/cexample_class.py
index bdeb9a8cb..6b8718ad8 100644
--- a/tests/misc/cexample_class.py
+++ b/tests/misc/cexample_class.py
@@ -7,14 +7,18 @@ except ImportError:
print("SKIP")
raise SystemExit
-t = cexample.Timer()
-print(t)
-print(t.time() <= 1)
+SLEEP_MS = 100
+TOLERANCE_MS = 20
+
+timer = cexample.Timer()
+
+t_start = timer.time()
time.sleep_ms(100)
-elapsed = t.time()
+t_end = timer.time()
-if not (99 <= elapsed <= 110):
- print("Elapsed time should be approx. 100ms but it is", elapsed)
+print(timer)
+print(0 <= t_start <= TOLERANCE_MS)
+print(SLEEP_MS - TOLERANCE_MS <= t_end <= SLEEP_MS + TOLERANCE_MS)
diff --git a/tests/misc/cexample_class.py.exp b/tests/misc/cexample_class.py.exp
index 67d98761b..b9a06602a 100644
--- a/tests/misc/cexample_class.py.exp
+++ b/tests/misc/cexample_class.py.exp
@@ -1,2 +1,3 @@
<Timer>
True
+True