|
17 | 17 |
|
18 | 18 | (def ^:private config-mapping |
19 | 19 | [["clojure.test.generative.threads" |
20 | | - [:threads] |
| 20 | + [:nthreads] |
21 | 21 | read-string |
22 | 22 | (max 1 (dec (.availableProcessors (Runtime/getRuntime))))] |
23 | 23 | ["clojure.test.generative.msec" |
|
33 | 33 | (if (seq val) |
34 | 34 | (assoc-in m path (coerce val)) |
35 | 35 | (assoc-in m path default)))) |
36 | | - {} |
37 | 36 | config-mapping)) |
38 | 37 |
|
39 | 38 | (def ^:private ^java.util.Random rnd (java.util.Random. (System/currentTimeMillis))) |
|
43 | 42 | (locking rnd |
44 | 43 | (.nextInt rnd))) |
45 | 44 |
|
46 | | -(defprotocol TestContainer |
| 45 | +(defprotocol Testable |
47 | 46 | (get-tests [_])) |
48 | 47 |
|
49 | | -(extend-protocol TestContainer |
| 48 | +(extend-protocol Testable |
50 | 49 | clojure.lang.Var |
51 | 50 | (get-tests |
52 | 51 | [v] |
|
65 | 64 | [m] m)) |
66 | 65 |
|
67 | 66 |
|
68 | | -(defn find-vars-in-namespaces |
| 67 | +(defn- find-vars-in-namespaces |
69 | 68 | [& nses] |
70 | 69 | (when nses |
71 | 70 | (reduce (fn [v ns] (into v (vals (ns-interns ns)))) [] nses))) |
72 | 71 |
|
73 | | -(defn find-vars-in-dirs |
| 72 | +(defn- find-vars-in-dirs |
74 | 73 | [& dirs] |
75 | 74 | (let [nses (mapcat #(ns/find-namespaces-in-dir (java.io.File. ^String %)) dirs)] |
76 | 75 | (doseq [ns nses] (require ns)) |
77 | 76 | (apply find-vars-in-namespaces nses))) |
78 | 77 |
|
79 | | -(defn run-one |
| 78 | +(defn- run-one |
80 | 79 | "Run f (presumably for side effects) repeatedly on n threads, |
81 | 80 | until msec has passed or somebody throws an exception. |
82 | 81 | Returns as many status maps as seeds passed in." |
83 | | - [{:keys [test input-gen]} msec seeds] |
84 | | - (prn) (prn test) |
| 82 | + [{:keys [test input-gen]} {:keys [msec seeds]}] |
85 | 83 | (let [f (eval test) |
86 | 84 | start (System/currentTimeMillis) |
87 | 85 | futs (mapv |
|
106 | 104 | seeds)] |
107 | 105 | (map deref futs))) |
108 | 106 |
|
109 | | -(defn run-n |
110 | | - "Run tests in parallel on nthreads, dividing msec equally between the tests." |
111 | | - [nthreads msec tests] |
112 | | - (mapcat #(run-one % (/ msec (count tests)) (repeatedly nthreads next-seed)) tests)) |
113 | | - |
114 | | -(defn failed? |
| 107 | +(defn- failed? |
115 | 108 | "Does test result indicate a failure?" |
116 | 109 | [result] |
117 | 110 | (contains? result :exception)) |
118 | 111 |
|
119 | | -(defn run-vars |
120 | | - "Designed for interactive use. Prints results to *out* and throws |
121 | | - on first failure encountered." |
122 | | - [nthreads msec & test-containers] |
123 | | - (doseq [result (run-n nthreads msec (mapcat get-tests test-containers))] |
124 | | - (if (failed? result) |
125 | | - (throw (ex-info "Generative test failed" result)) |
126 | | - (prn result)))) |
| 112 | +(defn- run-n |
| 113 | + "Run tests in parallel on nthreads, dividing msec equally between the tests. |
| 114 | + Returns a list of maps of :iter, :seed, :test." |
| 115 | + [{:keys [nthreads msec]} tests] |
| 116 | + (mapcat #(run-one % |
| 117 | + {:msec (/ msec (count tests)) |
| 118 | + :seeds (repeatedly nthreads next-seed)}) |
| 119 | + tests)) |
| 120 | + |
| 121 | +(defn- prf |
| 122 | + "Print and flush." |
| 123 | + [s] |
| 124 | + (print s) (flush)) |
127 | 125 |
|
128 | 126 | (defn dir-tests |
129 | 127 | "Returns all tests in dirs" |
|
134 | 132 | (apply find-vars-in-namespaces) |
135 | 133 | (mapcat get-tests)))) |
136 | 134 |
|
| 135 | +(defn inputs |
| 136 | + "For interactive use. Returns an infinite sequence of inputs for |
| 137 | + a test." |
| 138 | + [test] |
| 139 | + ((:input-gen test))) |
| 140 | + |
| 141 | +(defn run |
| 142 | + "Designed for interactive use. Prints results to *out* and throws |
| 143 | + on first failure encountered." |
| 144 | + [nthreads msec & test-containers] |
| 145 | + (doseq [result (run-n {:nthreads nthreads |
| 146 | + :msec msec} |
| 147 | + (mapcat get-tests test-containers))] |
| 148 | + (if (failed? result) |
| 149 | + (throw (ex-info "Generative test failed" result)) |
| 150 | + (prn result)))) |
| 151 | + |
137 | 152 | (defn run-suite |
138 | 153 | "Designed for test suite use." |
139 | | - [{:keys [threads msec verbose]} tests] |
140 | | - (reduce |
141 | | - (fn [{:keys [failures iters tests]} result] |
142 | | - (if (or verbose (:exception result)) |
143 | | - (do (prn) (prn result)) |
144 | | - (print ".")) |
145 | | - (when (:exception result) |
146 | | - (.printStackTrace ^Throwable (:exception result))) |
147 | | - (flush) |
148 | | - {:failures (+ failures (if (:exception result) 1 0)) |
149 | | - :iters (+ iters (:iter result)) |
150 | | - :tests (inc tests)}) |
151 | | - {:failures 0 :iters 0 :tests 0} |
152 | | - (run-n threads msec tests))) |
| 154 | + [{:keys [nthreads msec progress]} tests] |
| 155 | + (let [progress (or progress #(prf "."))] |
| 156 | + (reduce |
| 157 | + (fn [{:keys [failures iters tests]} result] |
| 158 | + (when (:exception result) |
| 159 | + (.printStackTrace ^Throwable (:exception result))) |
| 160 | + (if (:exception result) |
| 161 | + (prn result) |
| 162 | + (progress)) |
| 163 | + {:failures (+ failures (if (:exception result) 1 0)) |
| 164 | + :iters (+ iters (:iter result)) |
| 165 | + :tests (+ tests (/ 1 nthreads))}) |
| 166 | + {:failures 0 :iters 0 :tests 0} |
| 167 | + (run-n {:nthreads nthreads |
| 168 | + :msec msec} |
| 169 | + tests)))) |
153 | 170 |
|
154 | 171 | (defn -main |
155 | 172 | "Command line entry point. Calls System.exit!" |
|
0 commit comments