Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/puppetlabs/puppetdb/cli/benchmark.clj
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,15 @@

TODO: handle arrays and maps."
class)
(defmethod touch-parameter-value String [p] (.nextAscii (RandomStringUtils/secure) (count p)))
(defmethod touch-parameter-value String [p]
(let [len (count p)]
(if (zero? len)
(.nextAscii (RandomStringUtils/secure) 1) ; empty -> single char
(loop [attempts 5]
(let [new-val (.nextAscii (RandomStringUtils/secure) len)]
(if (or (not= new-val p) (zero? attempts))
new-val
(recur (dec attempts))))))))
(defmethod touch-parameter-value Number [_] (rand-int 1000000))
(defmethod touch-parameter-value Boolean [p] (not p))
;; Allow other types to pass through unmutated for now
Expand Down
17 changes: 15 additions & 2 deletions test/puppetlabs/puppetdb/cli/benchmark_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,16 @@
(get-in modded-cat [:resource-hash k1 "parameters"]))
"parameters have changed")))))

(deftest touch-parameter-value-test
(testing "empty string produces non-empty string"
(let [result (benchmark/touch-parameter-value "")]
(is (= 1 (count result)))
(is (not= "" result))))
(testing "single character string produces different string"
(let [result (benchmark/touch-parameter-value "x")]
(is (= 1 (count result)))
(is (not= "x" result)))))

(deftest touch-parameters-test
(let [psmall {"a" "b"}
pbig (build-parameters 2000)
Expand All @@ -563,22 +573,25 @@
(is (= 2 (weigh ps-mutated)))
(is (not= psmall ps-mutated))
(is (= pbig-count (count pb-mutated)))
(is (= pbig-weight (weigh pb-mutated)))
(is (#{pbig-weight (inc pbig-weight)} (weigh pb-mutated))
"weight same or +1 if empty string was mutated")
(is (not= pbig pb-mutated))))

(deftest rebuild-parameters-test
(let [psmall {"a" "b"}
pbig (build-parameters 2000)
pbig-count (count pbig)
pbig-weight (weigh pbig)
pbig-empty-count (count (filter #(= "" %) (vals pbig)))

ps-rebuilt (benchmark/rebuild-parameters psmall)
pb-rebuilt (benchmark/rebuild-parameters pbig)]
(is (= {} (benchmark/rebuild-parameters {})) "Handles the empty case.")
(is (= 1 (count ps-rebuilt)))
(is (= 6 (weigh ps-rebuilt)))
(is (= pbig-count (count pb-rebuilt)))
(is (= pbig-weight (weigh pb-rebuilt)))
(is (= (+ pbig-weight pbig-empty-count) (weigh pb-rebuilt))
"weight increases by count of empty strings")
(is (not= pbig pb-rebuilt))
(let [pb-keys (set (keys pbig))
pb-vals (set (vals pbig))
Expand Down
Loading