Skip to content

Commit

Permalink
add polling functions
Browse files Browse the repository at this point in the history
  • Loading branch information
David James committed Jul 22, 2014
1 parent 21bd7ed commit 2a5e916
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[kria.conversions :as conv]
[kria.core :as core]
[kria.index :as index]
[kria.polling :as p]
[kria.object :as object]
[kria.schema :as schema]
[kria.search :as search]
Expand Down
28 changes: 28 additions & 0 deletions test/kria/polling.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns kria.polling)

(set! *warn-on-reflection* true)

(defn poll
"Returns true if function `f` returns value `v` within `max-i`
iterations. Returns false otherwise. Each iteration waits `i-delay`
milliseconds."
[v f max-i i-delay]
(loop [i 0]
(if (>= i max-i)
false
(let [fv (f)]
(if (= v fv)
true
(do
(Thread/sleep i-delay)
(recur (inc i))))))))

(defn poll+
"Returns true if `poll` function returns true and predicate
function remains true after an additional time delay. This is
intended to confirm that the function does not change after
becoming true."
[v f max-i i-delay]
(when (poll v f max-i i-delay)
(Thread/sleep (* 2 i-delay))
(= v (f))))

0 comments on commit 2a5e916

Please sign in to comment.