Fork me on GitHub
#testing
<
2022-10-18
>
aaron5112:10:01

Trying to write defmacro will-be , like clojure.test/is, that times out after multiple attempts, and produces nice failures like clojure.test/is (comparing actual & expected). I have the below attempt, but it’s not a macro and doesn’t produce nice errors. Any ideas, or does this function/macro already exist?

(defn will-be
  [f timeout-ms sleep-ms]
  (let [start (System/currentTimeMillis)]
    (while (not (f))
           (when (< (+ start timeout-ms) (System/currentTimeMillis))
                 (throw (ex-info "will-be timed out" {})))
           (Thread/sleep sleep-ms))))

;; for example, (will-be #(= "hello world" @server-response) 1000 100)

mauricio.szabo02:10:08

I have one drafted on my library Check: https://gitlab.com/mauricioszabo/check/-/blob/await-for-things/src/check/async.clj#L208-239 I'm using Promesa but if instead you throw an exception, maybe it can work for you?

mauricio.szabo02:10:49

Nice failures are basically by making the await-for a macro and trying to get what condition was wrong