Created
November 15, 2010 22:11
-
-
Save weissjeffm/701051 to your computer and use it in GitHub Desktop.
Clojure macro to insert timeouts into loops
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
;;Code rewriting macro | |
(defmacro loop-with-timeout [timeout bindings & forms] | |
`(let [starttime# (System/currentTimeMillis)] | |
(loop ~bindings | |
(if (> (- (System/currentTimeMillis) starttime#) ~timeout) | |
(throw (RuntimeException. (str "Hit timeout of " ~timeout "ms."))) | |
(do ~@forms))))) | |
;;example use of macro | |
(loop-with-timeout 60000 [] | |
(if (not (.isTextPresent sel text)) | |
(do (Thread/sleep 15000) | |
(recur)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment