Skip to content

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

Fix flaky with-query-execution! test helper delivering stale batched executions (#75831)

Alexander Polyankine6670cff3581
diff --git a/test/metabase/query_processor/middleware/process_userland_query_test.clj b/test/metabase/query_processor/middleware/process_userland_query_test.clj
index b63b03dcb83..61cfb3ac05b 100644
--- a/test/metabase/query_processor/middleware/process_userland_query_test.clj
+++ b/test/metabase/query_processor/middleware/process_userland_query_test.clj
@@ -26,37 +26,35 @@
(mt/with-clock #t "2020-02-04T12:22-08:00[US/Pacific]"
(let [original-hash (qp.util/query-hash query)
result (promise)]
- ;; save-execution-metadata!* is invoked from the QP pipeline transducer, which runs on a thread
- ;; that doesn't inherit *local-redefs* — use with-redefs so worker threads see the replacement.
- ;; `*execute-async?*` is redef'd (not bound) for the same reason: it makes the save happen synchronously
- ;; on whatever thread runs the query instead of going through the (batched, async) grouper queue.
- #_{:clj-kondo/ignore [:metabase/prefer-with-dynamic-fn-redefs]}
- (with-redefs [qp.util/*execute-async?* false
- process-userland-query/save-execution-metadata!*
- (fn [query-executions]
- (doseq [{qe-hash :hash, :as query-execution} query-executions
- :when qe-hash]
- (deliver
- result
- (if (java.util.Arrays/equals ^bytes qe-hash original-hash)
- query-execution
- ;; if you're seeing this there is probably some
- ;; bug that is causing query hashes to get
- ;; calculated in an inconsistent manner; check
- ;; `:query` vs `:query-execution-query`
- (ex-info (format "%s: Query hashes are not equal!" `do-with-query-execution!)
- {:query query
- :original-hash (some-> original-hash codecs/bytes->hex)
- :query-execution query-execution
- :query-execution-hash (some-> ^bytes qe-hash codecs/bytes->hex)
- :query-execution-query (:json_query query-execution)})))))]
- (run
- (fn qe-result* []
- (let [qe (deref result 1000 ::timed-out)]
- (cond-> qe
- (:running_time qe) (update :running_time int?)
- (:hash qe) (update :hash (fn [^bytes a-hash]
- (some-> a-hash codecs/bytes->hex)))))))))))
+ (mt/with-temporary-setting-values [synchronous-batch-updates true]
+ ;; save-execution-metadata!* is invoked from the QP pipeline transducer, which runs on a thread
+ ;; that doesn't inherit *local-redefs* — use with-redefs so worker threads see the replacement.
+ #_{:clj-kondo/ignore [:metabase/prefer-with-dynamic-fn-redefs]}
+ (with-redefs [process-userland-query/save-execution-metadata!*
+ (fn [query-executions]
+ (doseq [{qe-hash :hash, :as query-execution} query-executions
+ :when qe-hash]
+ (deliver
+ result
+ (if (java.util.Arrays/equals ^bytes qe-hash original-hash)
+ query-execution
+ ;; if you're seeing this there is probably some
+ ;; bug that is causing query hashes to get
+ ;; calculated in an inconsistent manner; check
+ ;; `:query` vs `:query-execution-query`
+ (ex-info (format "%s: Query hashes are not equal!" `do-with-query-execution!)
+ {:query query
+ :original-hash (some-> original-hash codecs/bytes->hex)
+ :query-execution query-execution
+ :query-execution-hash (some-> ^bytes qe-hash codecs/bytes->hex)
+ :query-execution-query (:json_query query-execution)})))))]
+ (run
+ (fn qe-result* []
+ (let [qe (deref result 1000 ::timed-out)]
+ (cond-> qe
+ (:running_time qe) (update :running_time int?)
+ (:hash qe) (update :hash (fn [^bytes a-hash]
+ (some-> a-hash codecs/bytes->hex))))))))))))
(defmacro with-query-execution! {:style/indent 1} [[qe-result-binding query] & body]
`(do-with-query-execution! ~query (fn [~qe-result-binding] ~@body)))