Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to avoid using an atom #1

Merged
merged 3 commits into from Dec 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 29 additions & 26 deletions src/kaocha_retry/plugin.clj
Expand Up @@ -7,51 +7,54 @@

(def default-max-retries 3)
(def default-wait-time 100)
(def current-retries (atom {}))
(def to-report (atom nil))

(def current-retries (atom 0))

(defn- with-capture-report [t]
(with-redefs [te/report (fn [& args]
(reset! to-report args))]
(t)))

(defn run-with-retry [max-retries wait-time t test-id]
(defn run-with-retry [max-retries wait-time t]
(fn []
(loop [passed? (with-capture-report t)]
(let [attempts (get @current-retries test-id)
report #(apply te/report @to-report)]
(loop [passed? (with-capture-report t)
attempts 0]
(reset! current-retries attempts)
(let [report #(do
(apply te/report @to-report)
%)]
(if passed?
(do (report) true)
(report passed?)
(if (= attempts max-retries)
(do (report) false)
(report passed?)
(do
(Thread/sleep wait-time)
(swap! current-retries
assoc
test-id
(inc attempts))

(recur (with-capture-report t)))))))))
(recur (with-capture-report t) (inc attempts)))))))))

(defplugin kaocha-retry.plugin/retry
(pre-run [test-plan]
(reset! current-retries {})
;; propagate the retry? true if needed
;; in all the tests?
(assoc test-plan ::retries {})
test-plan)

(post-test [testable test-plan]
(if (h/leaf? testable)
(assoc-in testable
[::retries (:kaocha.testable/id testable)]
@current-retries)
testable))

(pre-test [testable test-plan]
(reset! current-retries 0)
(let [max-retries (::retry-max-tries test-plan 3)
wait-time (::retry-wait-time test-plan default-wait-time)
test-id (:kaocha.testable/id testable)]
wait-time (::retry-wait-time test-plan default-wait-time)]

(if (h/leaf? testable)
(do
(swap! current-retries assoc test-id 0)
(-> (update testable
:kaocha.testable/wrap
conj
(fn [t]
(run-with-retry max-retries
wait-time
t
test-id)))))
(-> (update testable
:kaocha.testable/wrap
conj
(fn [t]
(run-with-retry max-retries wait-time t))))
testable))))