I have a set function that calls an API of another Service.
I implemented a feature where
- I retry the set() upto 3 times if the set() fails.
- If retry() fails for all the 3 times, cut a ticket (generate an email saying there is an error in set() ).
I want to test the above feature.
Mock the set() such that I can verify retry works successfully.
Mock set() and retry such that I can verify the creation of ticket.
How can I mock the above scenarios in clojure?
My function looks like this:
(defn set *
"Set"
[param1 param2 param3]
(try
(utils/retry 2
(do (set param1 param2 param3)))
(catch Exception e
(cut-ticket))))
(defmacro retry
"Evaluates expr up to cnt + 1 times, retrying if an exception
is thrown. If an exception is thrown on the final attempt, it
is allowed to bubble up."
[cnt expr]
(letfn [(go [cnt]
(if (zero? cnt)
expr
`(try ~expr
(catch Exception e#
(retry ~(dec cnt) ~expr)))))]
(go cnt)))
Ref: retrying something 3 times before throwing an exception - in clojure