Skip to content

Instantly share code, notes, and snippets.

@kornysietsma
Created October 24, 2014 11:41
Show Gist options
  • Save kornysietsma/df45bbea3196adb5821b to your computer and use it in GitHub Desktop.
Save kornysietsma/df45bbea3196adb5821b to your computer and use it in GitHub Desktop.
wait until in clojure test
(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