1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-25 07:50:27 +00:00

Make clock tests less fragile for CI.

This commit is contained in:
Calvin Rose 2023-05-31 22:21:12 -05:00
parent 644ac8caf8
commit 14e33c295f

View File

@ -333,29 +333,12 @@
(assert (pos? (length (gensym))) "gensym not empty, regression #753")
(assert-no-error (os/clock :realtime) "realtime clock")
(assert-no-error (os/clock :cputime) "cputime clock")
(assert-no-error (os/clock :monotonic) "monotonic clock")
# os/clock. These tests might prove fragile under CI because they
# rely on measured time. We'll see.
(defmacro measure-time [clocks & body]
(def [t1 t2] [(gensym) (gensym)])
~(do
(def ,t1 (map |(os/clock $) ,clocks))
,;body
(def ,t2 (map |(os/clock $) ,clocks))
(zipcoll ,clocks (map |(- ;$) (map tuple ,t2 ,t1))))
)
# Spin for 0.1 seconds
(def dt (measure-time [:realtime :monotonic :cputime]
(def t1 (os/clock :monotonic))
(while (< (- (os/clock :monotonic) t1) 0.1) true)))
(assert (> (dt :monotonic) 0.10))
(assert (> (dt :cputime) 0.05))
# Sleep for 0.1 seconds
(def dt (measure-time [:realtime :monotonic :cputime] (os/sleep 0.1)))
(assert (> (dt :monotonic) 0.10))
(assert (< (dt :cputime) 0.05))
(def before (os/clock :monotonic))
(def after (os/clock :monotonic))
(assert (>= after before) "monotonic clock is monotonic")
(end-suite)