Created
October 24, 2014 11:41
-
-
Save kornysietsma/df45bbea3196adb5821b to your computer and use it in GitHub Desktop.
wait until in clojure test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def default-wait-death (time/seconds 5)) | |
(def default-wait-delay-ms 10) | |
(defn wait-until* | |
"wait until a function has become true" | |
([name fn] (wait-until* name fn default-wait-death)) | |
([name fn wait-death] | |
(let [die (time/plus (time/now) wait-death)] | |
(loop [] | |
(if-let [result (fn)] | |
result | |
(do | |
(Thread/sleep default-wait-delay-ms) | |
(if (time/after? (time/now) die) | |
(throw (Exception. (str "timed out waiting for: " name))) | |
(recur)))))))) | |
(defmacro wait-until | |
[expr] | |
`(wait-until* ~(pr-str expr) (fn [] ~expr))) | |
; note you could also use (:line &form) to get & report on the line number - but midje macros swallow this info so it's not useful there. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment