From cefd9f0a7f740d73ce314ece247b5dd3e1e833d8 Mon Sep 17 00:00:00 2001 From: Andrea Crotti Date: Mon, 7 Dec 2020 15:33:40 +0000 Subject: [PATCH 1/3] try to avoid using an atom --- src/kaocha_retry/plugin.clj | 38 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src/kaocha_retry/plugin.clj b/src/kaocha_retry/plugin.clj index 7739511..8b7e589 100644 --- a/src/kaocha_retry/plugin.clj +++ b/src/kaocha_retry/plugin.clj @@ -15,27 +15,24 @@ (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] + (let [report #(apply te/report @to-report)] (if passed? - (do (report) true) + (do (report) [attempts true]) (if (= attempts max-retries) - (do (report) false) + (do (report) [attempts false]) (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) (pre-test [testable test-plan] @@ -44,14 +41,9 @@ test-id (:kaocha.testable/id testable)] (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)))) From 0e43cc18de6abef4cfbfb0e10f93c2646f6a53bc Mon Sep 17 00:00:00 2001 From: Andrea Crotti Date: Mon, 7 Dec 2020 15:44:01 +0000 Subject: [PATCH 2/3] keep the atom but add to testable as well --- src/kaocha_retry/plugin.clj | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/kaocha_retry/plugin.clj b/src/kaocha_retry/plugin.clj index 8b7e589..a2ac1fa 100644 --- a/src/kaocha_retry/plugin.clj +++ b/src/kaocha_retry/plugin.clj @@ -7,9 +7,10 @@ (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))] @@ -19,11 +20,12 @@ (fn [] (loop [passed? (with-capture-report t) attempts 0] + (reset! current-retries attempts) (let [report #(apply te/report @to-report)] (if passed? - (do (report) [attempts true]) + (do (report) true) (if (= attempts max-retries) - (do (report) [attempts false]) + (do (report) false) (do (Thread/sleep wait-time) (recur (with-capture-report t) (inc attempts))))))))) @@ -35,10 +37,17 @@ (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) (-> (update testable From 90b893566867febab9788d0159b7cb7a0db1385d Mon Sep 17 00:00:00 2001 From: Andrea Crotti Date: Tue, 8 Dec 2020 15:21:05 +0000 Subject: [PATCH 3/3] cosmetics --- src/kaocha_retry/plugin.clj | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/kaocha_retry/plugin.clj b/src/kaocha_retry/plugin.clj index a2ac1fa..2b61b95 100644 --- a/src/kaocha_retry/plugin.clj +++ b/src/kaocha_retry/plugin.clj @@ -21,11 +21,13 @@ (loop [passed? (with-capture-report t) attempts 0] (reset! current-retries attempts) - (let [report #(apply te/report @to-report)] + (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) (recur (with-capture-report t) (inc attempts)))))))))