Skip to content

Commit

Permalink
Fix multiple threadpools being created concurrently
Browse files Browse the repository at this point in the history
run-testables ends up being called within its own body, which
kaocha is parallelizing. We generally want to parallelize at the
namespace level, so we use run-test-serial for other testable types.
This way we only create one threadpool at a time.
  • Loading branch information
john-shaffer committed Aug 20, 2022
1 parent 3c68292 commit ae0863b
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/kaocha/testable.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns kaocha.testable
(:refer-clojure :exclude [load])
(:require [clojure.java.io :as io]
[clojure.pprint :as pprint]
[clojure.spec.alpha :as s]
[clojure.test :as t]
[com.climate.claypoole :as cp]
Expand All @@ -12,10 +11,7 @@
[kaocha.plugin :as plugin]
[kaocha.result :as result]
[kaocha.specs :refer [assert-spec]]
[kaocha.util :as util]
[kaocha.hierarchy :as hierarchy])
(:import [clojure.lang Compiler$CompilerException]
[java.util.concurrent ArrayBlockingQueue BlockingQueue]))
[kaocha.util :as util]))

(def ^:dynamic *fail-fast?*
"Should testing terminate immediately upon failure or error?"
Expand Down Expand Up @@ -258,23 +254,28 @@
(defn run-testables-parallel
"Run a collection of testables, returning a result collection."
[testables test-plan]
(let [num-threads (or (:parallel-threads *config*) (+ 2 (cp/ncpus)))
types (set (:parallel-children-exclude *config*))]
(cp/with-shutdown! [pool (cp/threadpool num-threads :name "kaocha-test-runner")]
(doall
(cp/pmap
pool
#(binding [*config*
(cond-> *config*
(contains? types (:kaocha.testable/type %)) (dissoc :parallel)
true (update :levels (fn [x] (if (nil? x) 1 (inc x)))))]
(run-testable % test-plan))
testables)))))
(let [num-threads (or (:parallel-threads *config*) (* 2 (inc (cp/ncpus))))
pred #(= :kaocha.type/ns (:kaocha.testable/type %))
nses (seq (filter pred testables))
others (seq (remove pred testables))]
(concat
(when others (run-testables-serial others test-plan))
(when nses
(cp/with-shutdown! [pool (cp/threadpool num-threads :name "kaocha-test-runner")]
(doall
(cp/pmap
pool
#(binding [*config*
(-> *config*
(dissoc :parallel)
(update :levels (fn [x] (if (nil? x) 1 (inc x)))))]
(run-testable % test-plan))
nses)))))))

(defn run-testables
[testables test-plan]
(if (:parallel *config*)
(doall (run-testables-parallel testables test-plan))
(run-testables-parallel testables test-plan)
(run-testables-serial testables test-plan)))

(defn test-seq [testable]
Expand Down

0 comments on commit ae0863b

Please sign in to comment.