Skip to content

Commit

Permalink
test added to test rate-limits of less than 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene Kim committed Feb 15, 2024
1 parent 36adaba commit 9be2e64
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion test/diehard/rate_limiter_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,23 @@
(.shutdown pool)
(t/is (< (Math/abs (- @counter (* rate time-secs)))
;; error tolerance 3%
(* 0.03 (* rate time-secs)))))))
(* 0.03 (* rate time-secs))))))

(t/testing "rate limits less than 1.0"
(let [rate 0.5
threads 1
pool (Executors/newFixedThreadPool threads)
rl (r/rate-limiter {:rate rate})
counter (atom 0)
time-secs 5]
(doseq [_ (range threads)]
(.submit pool (cast Runnable (fn []
(while true
(r/acquire! rl)
(swap! counter inc))))))
(Thread/sleep (* time-secs 1000))
(.shutdown pool)
(let [variance (Math/abs (- @counter (* rate time-secs)))]
(t/is (<= variance
;; error tolerance: 0.5 -- larger, because we are dealing with slower ticks
0.5))))))

0 comments on commit 9be2e64

Please sign in to comment.