Skip to content

Commit

Permalink
(PE-37466) monitor-test/connection-reuse: include a non-streaming query
Browse files Browse the repository at this point in the history
Add a test that includes a non-streaming query since they weren't
being properly forgotten before, which caused a race between the query
monitor and jetty which could cause the monitor to consume an
unrelated query's data when jetty re-used the connection.

[[email protected]: add commit message]
  • Loading branch information
austb authored and rbrw committed Jan 16, 2024
1 parent 780fbd3 commit a6aae45
Showing 1 changed file with 45 additions and 26 deletions.
71 changes: 45 additions & 26 deletions test/puppetlabs/puppetdb/query/monitor_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -277,30 +277,49 @@
summary)))))))

(deftest connection-reuse
;; Test multiple queries over the same connection. Run the test
;; multiple times because the first problem we encountered could
;; only occur during a narrow window in the monitor loop if a new
;; request came in between select invocations, after the key had
;; been cancelled.
;; https://github.com/puppetlabs/puppetdb/issues/3866
(with-puppetdb nil
(jdbc/with-db-transaction [] (add-certnames certnames))
;; Just use curl, since it's trivally easy to get it to do what we
;; need, and we already require it for test setup (via ext/). (It
;; looks like both clj-http and the JDK HttpClient have more
;; indirect control over reuse.)
(let [nodes (-> (assoc *base-url* :prefix "/pdb/query/v4/nodes")
base-url->str-with-prefix)
cmd ["curl" "--no-progress-meter" "--show-error" "--fail-early"
"--fail" nodes "-o" "/dev/null"
"--next" "--fail" nodes "-o" "/dev/null"
"--next" "--fail" nodes "-o" "/dev/null"]]
(loop [i 0]
(testing "connection reuse with simple queries"
;; Test multiple queries over the same connection. Run the test
;; multiple times because the first problem we encountered could
;; only occur during a narrow window in the monitor loop if a new
;; request came in between select invocations, after the key had
;; been cancelled.
;; https://github.com/puppetlabs/puppetdb/issues/3866
(with-puppetdb nil
(jdbc/with-db-transaction [] (add-certnames certnames))
;; Just use curl, since it's trivally easy to get it to do what we
;; need, and we already require it for test setup (via ext/). (It
;; looks like both clj-http and the JDK HttpClient have more
;; indirect control over reuse.)
(let [nodes (-> (assoc *base-url* :prefix "/pdb/query/v4/nodes")
base-url->str-with-prefix)
cmd ["curl" "--no-progress-meter" "--show-error" "--fail-early"
"--fail" nodes "-o" "/dev/null"
"--next" "--fail" nodes "-o" "/dev/null"
"--next" "--fail" nodes "-o" "/dev/null"]]
(loop [i 0]
(let [{:keys [exit out err]} (apply sh cmd)]
(when (< i 10)
(if (is (= 0 exit))
(recur (inc i))
(do
(apply println "Failed:" cmd)
(print out)
(print err)))))))))

(testing "connection reuse with non-streaming queries"
(with-puppetdb nil
(jdbc/with-db-transaction [] (add-certnames certnames))
(let [q (-> (assoc *base-url* :prefix "/pdb/query/v4")
base-url->str-with-prefix)
query (fn [q] (str "{\"query\":\"" q "\"}"))
ast-q (fn [q] (str "{\"query\":\"" q "\", \"ast_only\":true}"))
cmd ["curl" "-X" "POST" "--no-progress-meter" "--show-error" "--fail-early"
"--fail" "--max-time" "1" q "-H" "Content-Type:application/json" "-d" (ast-q "nodes{}") "-o" "/dev/null"
"--next" "--fail" "--max-time" "1" q "-H" "Content-Type:application/json" "-d" (query "nodes{}") "-o" "/dev/null"]]
(let [{:keys [exit out err]} (apply sh cmd)]
(when (< i 10)
(if (is (= 0 exit))
(recur (inc i))
(do
(apply println "Failed:" cmd)
(print out)
(print err)))))))))
(if (is (= 0 exit))
nil
(do
(apply println "Failed:" cmd)
(print out)
(print err))))))))

0 comments on commit a6aae45

Please sign in to comment.