Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

bit more cleanup (#68300)

dpsutton17f473a9f175
diff --git a/.clj-kondo/macros/metabase/server/statistics_handler_test.clj b/.clj-kondo/macros/metabase/server/statistics_handler_test.clj
deleted file mode 100644
index 088e7149404..00000000000
--- a/.clj-kondo/macros/metabase/server/statistics_handler_test.clj
+++ /dev/null
@@ -1,6 +0,0 @@
-(ns macros.metabase.server.statistics-handler-test)
-
-(defmacro with-server
- [[_hold-chan _hit-chan] & body]
- `(let []
- ~@body))
diff --git a/test/metabase/server/statistics_handler_test.clj b/test/metabase/server/statistics_handler_test.clj
index 69b046e5d10..6cf42e6aeff 100644
--- a/test/metabase/server/statistics_handler_test.clj
+++ b/test/metabase/server/statistics_handler_test.clj
@@ -2,8 +2,9 @@
(:require
[clj-http.client :as http]
[clojure.core.async :as a]
- [clojure.test :refer [deftest is testing]]
+ [clojure.test :refer [deftest is]]
[compojure.core :as compojure :refer #_{:clj-kondo/ignore [:discouraged-var]} [GET]]
+ [compojure.route :as route]
[metabase.analytics.prometheus-test :as prometheus-test]
[metabase.server.instance :as server.instance]
[metabase.test :as mt])
@@ -12,73 +13,56 @@
(set! *warn-on-reflection* true)
-(defn routes
- [hold-chan hit-chan]
+(defn- routes [started continue]
#_{:clj-kondo/ignore [:discouraged-var]}
(compojure/routes
- (GET "/sync" [] {:status 200 :headers {} :body "you got it sync"})
- (GET "/hold" []
- (a/>!! hit-chan :hit)
- (let [timeout (a/timeout 500)
- [ch _val] (a/alts!! [hold-chan timeout])]
- (if (= ch timeout)
- (throw (ex-info "We timed out!" {}))
- {:status 200 :headers {} :body "you got held"})))
- (GET "/async" []
+ (GET "/route" []
(fn [_req respond _raise]
- (respond {:status 200 :headers {} :body "you got it async"})))
+ (respond {:status 200 :headers {} :body "just a route"})))
+ (GET "/paused" []
+ (fn [_req respond _raise]
+ (a/>!! started :started)
+ (a/<!! continue)
+ (respond {:status 200 :body "continue"})))
(GET "/blowup" []
(throw (ex-info "Kaboom. you get a 500" {})))
(GET "/reroute" []
- {:status 301 :headers {"Location" "http://localhost:3000/app"} :body "go somewher else"})))
-
-(defmacro with-server
- [[hold-chan hit-chan] f]
- `(let [^Server server# (doto (server.instance/create-server (routes ~hold-chan ~hit-chan) {:port 0 :join? false})
- (.start))
- port# (.. server# getURI getPort)
- wait# (promise)]
- (~f server# port#)
- (.stop server#)))
+ {:status 301 :headers {"Location" "http://localhost:3000/app"} :body "go somewher else"})
+ (route/not-found {:status 404 :body "not found"})))
(deftest server-stats-test
(mt/with-prometheus-system! [_ system]
- (let [hold-chan (a/chan)
- hit-chan (a/chan)
- value! (fn value! [& metric-args]
- (apply mt/metric-value system metric-args))]
- (with-server [hold-chan hit-chan]
- (fn [_server port]
- (testing "async"
- (let [route (format "http://localhost:%d/sync" port)]
- (is (= 200 (:status (http/get route))))
- (is (prometheus-test/approx= 1 (value! :jetty/requests-total)))
- (is (prometheus-test/approx= 1 (value! :jetty/dispatched-total)))
- (is (prometheus-test/approx= 1 (value! :jetty/dispatched-active-max)))
- (is (prometheus-test/approx= 1 (value! :jetty/dispatched-total)))
- (is (prometheus-test/approx= 0 (value! :jetty/dispatched-active)))
- (is (prometheus-test/approx= 1 (value! :jetty/dispatched-active-max)))
- (is (prometheus-test/approx= 1 (value! :jetty/responses-total {:code "2xx"})))))
- (testing "requests active"
- (let [route (format "http://localhost:%d/hold" port)]
- (future
- ;; this will wait on the hold chan
- (is (= 200 (:status (http/get route))))
- (is (prometheus-test/approx= 0 (value! :jetty/requests-active)))
- (is (prometheus-test/approx= 0 (value! :jetty/dispatched-active))))
- (let [timeout (a/timeout 500)
- [ch _v] (a/alts!! [hit-chan timeout])]
- (is (not= ch timeout) "We timed out!")
- (is (prometheus-test/approx= 1 (value! :jetty/requests-active)))
- (is (prometheus-test/approx= 1 (value! :jetty/dispatched-active)))
- (a/>!! hold-chan :continue))))
- (testing "300"
- (testing "when server responds 300"
- (let [route (format "http://localhost:%d/reroute" port)]
- (http/get route {:redirect-strategy :none})
- (is (prometheus-test/approx= 1 (value! :jetty/responses-total {:code "3xx"}))))))
- (testing "500"
- (testing "when server responds 500"
- (let [route (format "http://localhost:%d/blowup" port)]
- (http/get route {:throw-exceptions false})
- (is (prometheus-test/approx= 1 (value! :jetty/responses-total {:code "5xx"})))))))))))
+ (let [value! (fn value! [& metric-args]
+ (apply mt/metric-value system metric-args))
+ started (a/promise-chan)
+ continue (a/promise-chan)
+ carry-on (a/promise-chan)
+ ^Server server (doto (server.instance/create-server (routes started continue)
+ {:port 0 :join? false})
+ (.start))
+ port (.. server getURI getPort)]
+ (try
+ (is (= 200 (:status (http/get (format "http://localhost:%d/route" port)))))
+ (is (= 301 (:status (http/get (format "http://localhost:%d/reroute" port)
+ {:redirect-strategy :none}))))
+ (is (= 404 (:status (http/get (format "http://localhost:%d/unknown" port)
+ {:throw-exceptions false}))))
+ (is (= 500 (:status (http/get (format "http://localhost:%d/blowup" port)
+ {:throw-exceptions false}))))
+ (future (http/get (format "http://localhost:%d/paused" port))
+ (a/>!! carry-on :done))
+ (a/<!! started)
+ (is (prometheus-test/approx= 1 (value! :jetty/requests-active)))
+ (is (prometheus-test/approx= 1 (value! :jetty/dispatched-active)))
+ (a/>!! continue :continue)
+ (a/<!! carry-on) ;; try to synchronize after the paused route
+ (is (prometheus-test/approx= 5 (value! :jetty/requests-total)))
+ (is (prometheus-test/approx= 5 (value! :jetty/dispatched-total)))
+ (is (pos? (value! :jetty/dispatched-active-max)))
+ (is (prometheus-test/approx= 0 (value! :jetty/dispatched-active)))
+ (is (pos? (value! :jetty/dispatched-active-max)))
+ (is (pos? (value! :jetty/responses-total {:code "2xx"})))
+ (is (pos? (value! :jetty/responses-total {:code "3xx"})))
+ (is (pos? (value! :jetty/responses-total {:code "4xx"})))
+ (is (pos? (value! :jetty/responses-total {:code "5xx"})))
+ (finally (.stop server))))))