[Git][clojure-team/puppetlabs-http-client-clojure][upstream] New upstream version 2.1.1
Jérôme Charaoui (@lavamind)
gitlab at salsa.debian.org
Tue Feb 7 21:13:55 GMT 2023
Jérôme Charaoui pushed to branch upstream at Debian Clojure Maintainers / puppetlabs-http-client-clojure
Commits:
58415ed3 by Jérôme Charaoui at 2023-02-07T16:02:17-05:00
New upstream version 2.1.1
- - - - -
6 changed files:
- CHANGELOG.md
- project.clj
- src/java/com/puppetlabs/http/client/impl/JavaClient.java
- test/puppetlabs/http/client/async_plaintext_test.clj
- test/puppetlabs/http/client/async_unbuffered_test.clj
- test/puppetlabs/http/client/metrics_test.clj
Changes:
=====================================
CHANGELOG.md
=====================================
@@ -1,5 +1,9 @@
## Unreleased
+# 2.1.1
+* [PE-34843](https://tickets.puppetlabs.com/browse/PE-34843) Properly reuse connections when using a client certificate
+* Update to clj-parent 5.2.11
+
# 2.1.0
* update to clj-parent 5.2.6 to move from bouncy-castle `15on` libraries to the `18on` version
* Use `OPTIONS` request method when calling the synchronous client's `options` function.
=====================================
project.clj
=====================================
@@ -1,11 +1,11 @@
-(defproject puppetlabs/http-client "2.1.0"
+(defproject puppetlabs/http-client "2.1.1"
:description "HTTP client wrapper"
:license {:name "Apache License, Version 2.0"
:url "http://www.apache.org/licenses/LICENSE-2.0.html"}
:min-lein-version "2.9.1"
- :parent-project {:coords [puppetlabs/clj-parent "5.2.6"]
+ :parent-project {:coords [puppetlabs/clj-parent "5.2.11"]
:inherit [:managed-dependencies]}
;; Abort when version ranges or version conflicts are detected in
=====================================
src/java/com/puppetlabs/http/client/impl/JavaClient.java
=====================================
@@ -532,6 +532,15 @@ public class JavaClient {
HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
clientBuilder.setMaxConnPerRoute(clientOptions.getMaxConnectionsPerRoute());
clientBuilder.setMaxConnTotal(clientOptions.getMaxConnectionsTotal());
+ // Typically, the HttpClient library tracks the "user token" for a
+ // connection, which in our case is the SSL certificate name, and
+ // refuses to reuse a connection if the SSL certificate is different
+ // for the next request. Because we create a new HttpClientContext for
+ // every request, this tracking doesn't work in our case. However,
+ // since we bake the SSL context in at the time we create the client,
+ // it's impossible for it to change from request to request, so we can
+ // simply disable that tracking altogether.
+ clientBuilder.disableConnectionState();
SSLContext context = coercedOptions.getSslContext();
if (context != null) {
=====================================
test/puppetlabs/http/client/async_plaintext_test.clj
=====================================
@@ -56,6 +56,11 @@
{:status 200
:body "cookie has been set"}))
+(defn clear-latch
+ [latch]
+ (dotimes [_ (.getCount latch)]
+ (.countDown latch)))
+
(tk/defservice test-cookie-service
[[:WebserverService add-ring-handler]]
(init [this context]
@@ -671,8 +676,10 @@
(testing "clojure persistent async client"
(with-open [client (async/create-client {})]
(dotimes [n 10] (future (common/get client url {:as :text})))
- (is (= false (.await countdown 5 TimeUnit/SECONDS)))
- (is (= 2 @actual-count)))))))))
+ (is (= false (.await countdown 1 TimeUnit/SECONDS)))
+ (is (= 2 @actual-count))
+ ;; Clear the latch so the webserver can shutdown
+ (clear-latch countdown))))))))
(testing "passing client route limit of 0 selects default behavior (a limit of 2)"
(let [actual-count (atom 0)
@@ -689,8 +696,10 @@
(testing "clojure persistent async client"
(with-open [client (async/create-client {:max-connections-per-route 0})]
(dotimes [n 10] (future (common/get client url {:as :text})))
- (is (= false (.await countdown 5 TimeUnit/SECONDS)))
- (is (= 2 @actual-count)))))))))
+ (is (= false (.await countdown 1 TimeUnit/SECONDS)))
+ (is (= 2 @actual-count))
+ ;; Clear the latch so the webserver can shutdown
+ (clear-latch countdown))))))))
(testing "client limits specified requests per route"
@@ -708,8 +717,10 @@
(testing "clojure persistent async client"
(with-open [client (async/create-client {:max-connections-per-route 3})]
(dotimes [n 10] (future (common/get client url {:as :text})))
- (is (= false (.await countdown 5 TimeUnit/SECONDS)))
- (is (= 3 @actual-count)))))))))
+ (is (= false (.await countdown 1 TimeUnit/SECONDS)))
+ (is (= 3 @actual-count))
+ ;; Clear the latch so the webserver can shutdown
+ (clear-latch countdown))))))))
(testing "client route limit of 11 does not limit requests per route when less than 11"
(let [actual-count (atom 0)
@@ -745,5 +756,7 @@
(with-open [client (async/create-client {:max-connections-per-route 11
:max-connections-total 3})]
(dotimes [n 10] (future (common/get client url {:as :text})))
- (is (= false (.await countdown 5 TimeUnit/SECONDS)))
- (is (= 3 @actual-count))))))))))
+ (is (= false (.await countdown 1 TimeUnit/SECONDS)))
+ (is (= 3 @actual-count))
+ ;; Clear the latch so the webserver can shutdown
+ (clear-latch countdown)))))))))
=====================================
test/puppetlabs/http/client/async_unbuffered_test.clj
=====================================
@@ -19,7 +19,7 @@
[data-size]
(apply str "xxxx" (repeatedly (/ data-size 35) #(UUID/randomUUID))))
-(defn successful-handler
+(defn streaming-handler
"A Ring handler that asynchronously sends some data, waits for confirmation the data has been received then sends
some more data"
[data send-more-data]
@@ -39,32 +39,19 @@
{:status 200
:body instream})))
-(defn blocking-handler
- "A Ring handler that sends some data but then never closes the socket"
- [data]
- (fn [_]
- (let [outstream (PipedOutputStream.)
- instream (PipedInputStream.)]
- (.connect instream outstream)
- ;; Return the response immediately and asynchronously stream some data into it
- (future
- (.write outstream (.getBytes data)))
- {:status 200
- :body instream})))
-
(defn- clojure-non-blocking-streaming
- "Stream 32M of data (roughly) which is large enough to ensure the client won't buffer it all. Checks the data is
+ "Stream 1M of data (roughly) which is large enough to ensure the client won't buffer it all. Checks the data is
streamed in a non-blocking manner i.e some data is received by the client before the server has finished
transmission"
[decompress-body?]
(testlogging/with-test-logging
- (let [data (generate-data (* 32 1024 1024))
+ (let [data (generate-data (* 1024 1024))
opts {:as :unbuffered-stream :decompress-body decompress-body?}]
(testing " - check data can be streamed successfully"
(let [send-more-data (promise)]
(testwebserver/with-test-webserver-and-config
- (successful-handler data send-more-data) port {:shutdown-timeout-seconds 1}
+ (streaming-handler data send-more-data) port {:shutdown-timeout-seconds 1}
(with-open [client (async/create-client {:connect-timeout-milliseconds 100
:socket-timeout-milliseconds 20000})]
(let [response @(common/get client (str "http://localhost:" port "/hello") opts)
@@ -78,19 +65,21 @@
(is (= (str data "yyyy") (str "xxxx" (slurp instream)))))))))) ;; Read the rest and validate
(testing " - check socket timeout is handled"
- (try
- (testwebserver/with-test-webserver-and-config
- (blocking-handler data) port {:shutdown-timeout-seconds 1}
- (with-open [client (async/create-client {:connect-timeout-milliseconds 100
- :socket-timeout-milliseconds 200})]
- (let [response @(common/get client (str "http://localhost:" port "/hello") opts)
- {:keys [body error]} response]
- (is (nil? error))
- ;; Consume the body to get the exception
- (is (thrown? SocketTimeoutException (slurp body))))))
- (catch TimeoutException e
- ;; Expected whenever a server-side failure is generated
- nil)))
+ (let [send-more-data (promise)]
+ (try
+ (testwebserver/with-test-webserver-and-config
+ (streaming-handler data send-more-data) port {:shutdown-timeout-seconds 1}
+ (with-open [client (async/create-client {:connect-timeout-milliseconds 100
+ :socket-timeout-milliseconds 200})]
+ (let [response @(common/get client (str "http://localhost:" port "/hello") opts)
+ {:keys [body error]} response]
+ (is (nil? error))
+ ;; Consume the body to get the exception
+ (is (thrown? SocketTimeoutException (slurp body)))))
+ (deliver send-more-data true))
+ (catch TimeoutException e
+ ;; Expected whenever a server-side failure is generated
+ nil))))
(testing " - check connection timeout is handled"
(with-open [client (async/create-client {:connect-timeout-milliseconds 100})]
@@ -100,11 +89,11 @@
(is (instance? ConnectException error))))))))
(deftest clojure-non-blocking-streaming-without-decompression
- (testing "clojure :unbuffered-stream with 32MB payload and no decompression"
+ (testing "clojure :unbuffered-stream with 1MB payload and no decompression"
(clojure-non-blocking-streaming false)))
(deftest clojure-non-blocking-streaming-with-decompression
- (testing "clojure :unbuffered-stream with 32MB payload and decompression"
+ (testing "clojure :unbuffered-stream with 1MB payload and decompression"
(clojure-non-blocking-streaming true)))
(defn- clojure-blocking-streaming
@@ -114,7 +103,7 @@
(testing " - check data can be streamed successfully"
(testwebserver/with-test-webserver-and-config
- (successful-handler data nil) port {:shutdown-timeout-seconds 1}
+ (streaming-handler data nil) port {:shutdown-timeout-seconds 1}
(with-open [client (async/create-client {:connect-timeout-milliseconds 100
:socket-timeout-milliseconds 20000})]
(let [response @(common/get client (str "http://localhost:" port "/hello") opts)
@@ -127,17 +116,19 @@
(is (= (str data "yyyy") (str "xxxx" (slurp instream))))))))) ;; Read the rest and validate
(testing " - check socket timeout is handled"
- (try
- (testwebserver/with-test-webserver-and-config
- (blocking-handler data) port {:shutdown-timeout-seconds 1}
- (with-open [client (async/create-client {:connect-timeout-milliseconds 100
- :socket-timeout-milliseconds 200})]
- (let [response @(common/get client (str "http://localhost:" port "/hello") opts)
- {:keys [error]} response]
- (is (instance? SocketTimeoutException error)))))
- (catch TimeoutException e
- ;; Expected whenever a server-side failure is generated
- nil)))
+ (let [send-more-data (promise)]
+ (try
+ (testwebserver/with-test-webserver-and-config
+ (streaming-handler data send-more-data) port {:shutdown-timeout-seconds 1}
+ (with-open [client (async/create-client {:connect-timeout-milliseconds 100
+ :socket-timeout-milliseconds 200})]
+ (let [response @(common/get client (str "http://localhost:" port "/hello") opts)
+ {:keys [error]} response]
+ (is (instance? SocketTimeoutException error))))
+ (deliver send-more-data true))
+ (catch TimeoutException e
+ ;; Expected whenever a server-side failure is generated
+ nil))))
(testing " - check connection timeout is handled"
@@ -168,25 +159,25 @@
(clojure-blocking-streaming (generate-data 1024) {:as :stream :decompress-body true})))
(deftest clojure-existing-streaming-with-large-payload-without-decompression
- (testing "clojure :stream with 32M payload and no decompression"
- (clojure-blocking-streaming (generate-data (* 32 1024 1024)) {:as :stream :decompress-body false})))
+ (testing "clojure :stream with 1M payload and no decompression"
+ (clojure-blocking-streaming (generate-data (* 1024 1024)) {:as :stream :decompress-body false})))
(deftest clojure-existing-streaming-with-large-payload-with-decompression
- (testing "clojure :stream with 32M payload and decompression"
- (clojure-blocking-streaming (generate-data (* 32 1024 1024)) {:as :stream :decompress-body true})))
+ (testing "clojure :stream with 1M payload and decompression"
+ (clojure-blocking-streaming (generate-data (* 1024 1024)) {:as :stream :decompress-body true})))
(defn- java-non-blocking-streaming
- "Stream 32M of data (roughly) which is large enough to ensure the client won't buffer it all. Checks the data is
- streamed in a non-blocking manner i.e some data is received by the client before the server has finished
- transmission"
+ "Stream 1M of data (roughly) which is large enough to ensure the client won't
+ buffer it all. Checks the data is streamed in a non-blocking manner i.e some
+ data is received by the client before the server has finished transmission"
[decompress-body?]
(testlogging/with-test-logging
- (let [data (generate-data (* 32 1024 1024))]
+ (let [data (generate-data (* 1024 1024))]
(testing " - check data can be streamed successfully"
(let [send-more-data (promise)]
(testwebserver/with-test-webserver-and-config
- (successful-handler data send-more-data) port {:shutdown-timeout-seconds 1}
+ (streaming-handler data send-more-data) port {:shutdown-timeout-seconds 1}
(with-open [client (-> (ClientOptions.)
(.setSocketTimeoutMilliseconds 20000)
(.setConnectTimeoutMilliseconds 100)
@@ -206,25 +197,27 @@
(is (= (str data "yyyy") (str "xxxx" (slurp instream)))))))))) ;; Read the rest and validate
(testing " - check socket timeout is handled"
- (try
- (testwebserver/with-test-webserver-and-config
- (blocking-handler data) port {:shutdown-timeout-seconds 1}
- (with-open [client (-> (ClientOptions.)
- (.setSocketTimeoutMilliseconds 200)
- (.setConnectTimeoutMilliseconds 100)
- (Async/createClient))]
- (let [request-options (doto (RequestOptions. (str "http://localhost:" port "/hello"))
- (.setAs ResponseBodyType/UNBUFFERED_STREAM)
- (.setDecompressBody decompress-body?))
- response (-> client (.get request-options) .deref)
- body (.getBody response)
- error (.getError response)]
- (is (nil? error))
- ;; Consume the body to get the exception
- (is (thrown? SocketTimeoutException (slurp body))))))
- (catch TimeoutException e
- ;; Expected whenever a server-side failure is generated
- nil)))
+ (let [send-more-data (promise)]
+ (try
+ (testwebserver/with-test-webserver-and-config
+ (streaming-handler data send-more-data) port {:shutdown-timeout-seconds 1}
+ (with-open [client (-> (ClientOptions.)
+ (.setSocketTimeoutMilliseconds 200)
+ (.setConnectTimeoutMilliseconds 100)
+ (Async/createClient))]
+ (let [request-options (doto (RequestOptions. (str "http://localhost:" port "/hello"))
+ (.setAs ResponseBodyType/UNBUFFERED_STREAM)
+ (.setDecompressBody decompress-body?))
+ response (-> client (.get request-options) .deref)
+ body (.getBody response)
+ error (.getError response)]
+ (is (nil? error))
+ ;; Consume the body to get the exception
+ (is (thrown? SocketTimeoutException (slurp body)))))
+ (deliver send-more-data true))
+ (catch TimeoutException e
+ ;; Expected whenever a server-side failure is generated
+ nil))))
(testing " - check connection timeout is handled"
@@ -240,11 +233,11 @@
(is (instance? ConnectException error))))))))
(deftest java-non-blocking-streaming-without-decompression
- (testing "java :unbuffered-stream with 32MB payload and no decompression"
+ (testing "java :unbuffered-stream with 1MB payload and no decompression"
(java-non-blocking-streaming false)))
(deftest java-non-blocking-streaming-with-decompression
- (testing "java :unbuffered-stream with 32MB payload and decompression"
+ (testing "java :unbuffered-stream with 1MB payload and decompression"
(java-non-blocking-streaming true)))
(defn- java-blocking-streaming
@@ -254,7 +247,7 @@
(testing " - check data can be streamed successfully"
(testwebserver/with-test-webserver-and-config
- (successful-handler data nil) port {:shutdown-timeout-seconds 1}
+ (streaming-handler data nil) port {:shutdown-timeout-seconds 1}
(with-open [client (-> (ClientOptions.)
(.setSocketTimeoutMilliseconds 20000)
(.setConnectTimeoutMilliseconds 100)
@@ -273,22 +266,24 @@
(is (= (str data "yyyy") (str "xxxx" (slurp instream))))))))) ;; Read the rest and validate
(testing " - check socket timeout is handled"
- (try
- (testwebserver/with-test-webserver-and-config
- (blocking-handler data) port {:shutdown-timeout-seconds 1}
- (with-open [client (-> (ClientOptions.)
- (.setSocketTimeoutMilliseconds 200)
- (.setConnectTimeoutMilliseconds 100)
- (Async/createClient))]
- (let [request-options (doto (RequestOptions. (str "http://localhost:" port "/hello"))
- (.setAs response-body-type)
- (.setDecompressBody decompress-body?))
- response (-> client (.get request-options) .deref)
- error (.getError response)]
- (is (instance? SocketTimeoutException error)))))
- (catch TimeoutException e
- ;; Expected whenever a server-side failure is generated
- nil)))
+ (let [send-more-data (promise)]
+ (try
+ (testwebserver/with-test-webserver-and-config
+ (streaming-handler data send-more-data) port {:shutdown-timeout-seconds 1}
+ (with-open [client (-> (ClientOptions.)
+ (.setSocketTimeoutMilliseconds 200)
+ (.setConnectTimeoutMilliseconds 100)
+ (Async/createClient))]
+ (let [request-options (doto (RequestOptions. (str "http://localhost:" port "/hello"))
+ (.setAs response-body-type)
+ (.setDecompressBody decompress-body?))
+ response (-> client (.get request-options) .deref)
+ error (.getError response)]
+ (is (instance? SocketTimeoutException error))))
+ (deliver send-more-data true))
+ (catch TimeoutException e
+ ;; Expected whenever a server-side failure is generated
+ nil))))
(testing " - check connection timeout is handled"
@@ -324,9 +319,9 @@
(java-blocking-streaming (generate-data 1024) ResponseBodyType/STREAM true)))
(deftest java-existing-streaming-with-large-payload-without-decompression
- (testing "java :stream with 32M payload and no decompression"
- (java-blocking-streaming (generate-data (* 32 1024 1024)) ResponseBodyType/STREAM false)))
+ (testing "java :stream with 1M payload and no decompression"
+ (java-blocking-streaming (generate-data (* 1024 1024)) ResponseBodyType/STREAM false)))
(deftest java-existing-streaming-with-large-payload-with-decompression
- (testing "java :stream with 32M payload and decompression"
- (java-blocking-streaming (generate-data (* 32 1024 1024)) ResponseBodyType/STREAM true)))
+ (testing "java :stream with 1M payload and decompression"
+ (java-blocking-streaming (generate-data (* 1024 1024)) ResponseBodyType/STREAM true)))
=====================================
test/puppetlabs/http/client/metrics_test.clj
=====================================
@@ -441,7 +441,7 @@
(testing "metrics work for a successful request"
(let [metric-registry (MetricRegistry.)]
(testwebserver/with-test-webserver-and-config
- (unbuffered-test/successful-handler data nil) port {:shutdown-timeout-seconds 1}
+ (unbuffered-test/streaming-handler data nil) port {:shutdown-timeout-seconds 1}
(with-open [client (-> (ClientOptions.)
(.setSocketTimeoutMilliseconds 20000)
(.setConnectTimeoutMilliseconds 100)
@@ -486,52 +486,54 @@
(is (<= 1000 (.getMean full-response-data)))
(is (<= 1000 (.getAggregate full-response-data))))))))))
(testing "metrics work for failed request"
- (try
- (testwebserver/with-test-webserver-and-config
- (unbuffered-test/blocking-handler data) port {:shutdown-timeout-seconds 1}
- (let [metric-registry (MetricRegistry.)]
- (with-open [client (-> (ClientOptions.)
- (.setSocketTimeoutMilliseconds 200)
- (.setConnectTimeoutMilliseconds 100)
- (.setMetricRegistry metric-registry)
- (Async/createClient))]
- (let [url (str "http://localhost:" port "/hello")
- request-options (doto (RequestOptions. url)
- (.setAs ResponseBodyType/UNBUFFERED_STREAM))
- response (-> client (.get request-options) .deref)
- error (.getError response)
- body (.getBody response)]
- (is (nil? error))
- (is (thrown? SocketTimeoutException (slurp body)))
- (let [client-metric-registry (.getMetricRegistry client)
- client-metrics (Metrics/getClientMetrics client-metric-registry)
- client-metrics-data (Metrics/getClientMetricsData client-metric-registry)
- full-response-name (format "%s.with-url.%s.full-response" metric-namespace url)
- full-response-name-with-method (format "%s.with-url-and-method.%s.GET.full-response"
- metric-namespace url)]
- (is (= [full-response-name]
- (map #(.getMetricName %) (.getUrlTimers client-metrics))
- (map #(.getMetricName %) (.getUrlData client-metrics-data))))
- (is (= [full-response-name-with-method]
- (map #(.getMetricName %) (.getUrlAndMethodTimers client-metrics))
- (map #(.getMetricName %) (.getUrlAndMethodData client-metrics-data))))
- (is (= [] (.getMetricIdTimers client-metrics)
- (.getMetricIdData client-metrics-data)))
- (is (every? #(instance? ClientTimer %)
- (concat (.getUrlTimers client-metrics)
- (.getUrlAndMethodTimers client-metrics))))
- (let [full-response-data (first (.getUrlData client-metrics-data))]
- (is (every? #(instance? ClientMetricData %)
- (concat (.getUrlData client-metrics-data)
- (.getUrlAndMethodData client-metrics-data))))
-
- (is (= 1 (.getCount full-response-data)))
- (is (= full-response-name (.getMetricName full-response-data)))
- (is (<= 200 (.getMean full-response-data)))
- (is (<= 200 (.getAggregate full-response-data)))))))))
- (catch TimeoutException e
- ;; Expected whenever a server-side failure is generated
- ))))))
+ (let [send-more-data (promise)]
+ (try
+ (testwebserver/with-test-webserver-and-config
+ (unbuffered-test/streaming-handler data send-more-data) port {:shutdown-timeout-seconds 1}
+ (let [metric-registry (MetricRegistry.)]
+ (with-open [client (-> (ClientOptions.)
+ (.setSocketTimeoutMilliseconds 200)
+ (.setConnectTimeoutMilliseconds 100)
+ (.setMetricRegistry metric-registry)
+ (Async/createClient))]
+ (let [url (str "http://localhost:" port "/hello")
+ request-options (doto (RequestOptions. url)
+ (.setAs ResponseBodyType/UNBUFFERED_STREAM))
+ response (-> client (.get request-options) .deref)
+ error (.getError response)
+ body (.getBody response)]
+ (is (nil? error))
+ (is (thrown? SocketTimeoutException (slurp body)))
+ (let [client-metric-registry (.getMetricRegistry client)
+ client-metrics (Metrics/getClientMetrics client-metric-registry)
+ client-metrics-data (Metrics/getClientMetricsData client-metric-registry)
+ full-response-name (format "%s.with-url.%s.full-response" metric-namespace url)
+ full-response-name-with-method (format "%s.with-url-and-method.%s.GET.full-response"
+ metric-namespace url)]
+ (is (= [full-response-name]
+ (map #(.getMetricName %) (.getUrlTimers client-metrics))
+ (map #(.getMetricName %) (.getUrlData client-metrics-data))))
+ (is (= [full-response-name-with-method]
+ (map #(.getMetricName %) (.getUrlAndMethodTimers client-metrics))
+ (map #(.getMetricName %) (.getUrlAndMethodData client-metrics-data))))
+ (is (= [] (.getMetricIdTimers client-metrics)
+ (.getMetricIdData client-metrics-data)))
+ (is (every? #(instance? ClientTimer %)
+ (concat (.getUrlTimers client-metrics)
+ (.getUrlAndMethodTimers client-metrics))))
+ (let [full-response-data (first (.getUrlData client-metrics-data))]
+ (is (every? #(instance? ClientMetricData %)
+ (concat (.getUrlData client-metrics-data)
+ (.getUrlAndMethodData client-metrics-data))))
+
+ (is (= 1 (.getCount full-response-data)))
+ (is (= full-response-name (.getMetricName full-response-data)))
+ (is (<= 200 (.getMean full-response-data)))
+ (is (<= 200 (.getAggregate full-response-data)))))))
+ (deliver send-more-data true)))
+ (catch TimeoutException e
+ ;; Expected whenever a server-side failure is generated
+ )))))))
(deftest clojure-metrics-for-unbuffered-streaming-test
(testlogging/with-test-logging
@@ -540,7 +542,7 @@
(testing "metrics work for a successful request"
(let [metric-registry (MetricRegistry.)]
(testwebserver/with-test-webserver-and-config
- (unbuffered-test/successful-handler data nil) port {:shutdown-timeout-seconds 1}
+ (unbuffered-test/streaming-handler data nil) port {:shutdown-timeout-seconds 1}
(with-open [client (async/create-client {:connect-timeout-milliseconds 100
:socket-timeout-milliseconds 20000
:metric-registry metric-registry})]
@@ -576,43 +578,45 @@
(is (<= 1000 (:mean full-response-data)))
(is (<= 1000 (:aggregate full-response-data))))))))))
(testing "metrics work for a failed request"
- (try
- (testwebserver/with-test-webserver-and-config
- (unbuffered-test/blocking-handler data) port {:shutdown-timeout-seconds 1}
- (let [metric-registry (MetricRegistry.)
- url (str "http://localhost:" port "/hello")]
- (with-open [client (async/create-client {:connect-timeout-milliseconds 100
- :socket-timeout-milliseconds 200
- :metric-registry metric-registry})]
- (let [response @(common/get client url opts)
- {:keys [body error]} response]
- (is (nil? error))
- ;; Consume the body to get the exception
- (is (thrown? SocketTimeoutException (slurp body))))
- (let [client-metric-registry (common/get-client-metric-registry client)
- client-metrics (metrics/get-client-metrics client-metric-registry)
- client-metrics-data (metrics/get-client-metrics-data client-metric-registry)
- full-response-name (format "%s.with-url.%s.full-response" metric-namespace url)
- full-response-name-with-method (format "%s.with-url-and-method.%s.GET.full-response"
- metric-namespace url)]
- (is (= [full-response-name]
- (map #(.getMetricName %) (:url client-metrics))
- (map :metric-name (:url client-metrics-data))))
- (is (= [full-response-name-with-method]
- (map #(.getMetricName %) (:url-and-method client-metrics))
- (map :metric-name (:url-and-method client-metrics-data))))
- (is (= [] (:metric-id client-metrics) (:metric-id client-metrics-data)))
- (is (every? #(instance? ClientTimer %)
- (concat (:url client-metrics)
- (:url-and-method client-metrics))))
- (let [full-response-data (first (:url client-metrics-data))]
- (is (= {:count 1 :metric-name full-response-name}
- (select-keys full-response-data [:metric-name :count])))
- (is (<= 200 (:mean full-response-data)))
- (is (<= 200 (:aggregate full-response-data))))))))
- (catch TimeoutException e
- ;; Expected whenever a server-side failure is generated
- ))))))
+ (let [send-more-data (promise)]
+ (try
+ (testwebserver/with-test-webserver-and-config
+ (unbuffered-test/streaming-handler data send-more-data) port {:shutdown-timeout-seconds 1}
+ (let [metric-registry (MetricRegistry.)
+ url (str "http://localhost:" port "/hello")]
+ (with-open [client (async/create-client {:connect-timeout-milliseconds 100
+ :socket-timeout-milliseconds 200
+ :metric-registry metric-registry})]
+ (let [response @(common/get client url opts)
+ {:keys [body error]} response]
+ (is (nil? error))
+ ;; Consume the body to get the exception
+ (is (thrown? SocketTimeoutException (slurp body))))
+ (let [client-metric-registry (common/get-client-metric-registry client)
+ client-metrics (metrics/get-client-metrics client-metric-registry)
+ client-metrics-data (metrics/get-client-metrics-data client-metric-registry)
+ full-response-name (format "%s.with-url.%s.full-response" metric-namespace url)
+ full-response-name-with-method (format "%s.with-url-and-method.%s.GET.full-response"
+ metric-namespace url)]
+ (is (= [full-response-name]
+ (map #(.getMetricName %) (:url client-metrics))
+ (map :metric-name (:url client-metrics-data))))
+ (is (= [full-response-name-with-method]
+ (map #(.getMetricName %) (:url-and-method client-metrics))
+ (map :metric-name (:url-and-method client-metrics-data))))
+ (is (= [] (:metric-id client-metrics) (:metric-id client-metrics-data)))
+ (is (every? #(instance? ClientTimer %)
+ (concat (:url client-metrics)
+ (:url-and-method client-metrics))))
+ (let [full-response-data (first (:url client-metrics-data))]
+ (is (= {:count 1 :metric-name full-response-name}
+ (select-keys full-response-data [:metric-name :count])))
+ (is (<= 200 (:mean full-response-data)))
+ (is (<= 200 (:aggregate full-response-data))))))
+ (deliver send-more-data true)))
+ (catch TimeoutException e
+ ;; Expected whenever a server-side failure is generated
+ )))))))
(deftest metric-namespace-test
(let [metric-prefix "my-metric-prefix"
@@ -797,4 +801,4 @@
metric-names (set (map #(.getMetricName %) (.getMetricIdTimers client-metrics)))]
(is (.isEmpty (.getUrlTimers client-metrics)))
(is (.isEmpty (.getUrlAndMethodTimers client-metrics)))
- (is (= #{long-foo-name long-foo-bar-name} metric-names)))))))))
\ No newline at end of file
+ (is (= #{long-foo-name long-foo-bar-name} metric-names)))))))))
View it on GitLab: https://salsa.debian.org/clojure-team/puppetlabs-http-client-clojure/-/commit/58415ed3f745f5714ea6107926fc1b7fe7de6e21
--
View it on GitLab: https://salsa.debian.org/clojure-team/puppetlabs-http-client-clojure/-/commit/58415ed3f745f5714ea6107926fc1b7fe7de6e21
You're receiving this email because of your account on salsa.debian.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-java-commits/attachments/20230207/b662368b/attachment.htm>
More information about the pkg-java-commits
mailing list