在 Clojure 中,如何对异步 Pedestal 拦截器进行单元测试?

In Clojure, how to unit test an async Pedestal interceptor?

我有一个要测试的异步 Pedestal 拦截器:

(def my-interceptor
  (io.pedestal.interceptor/interceptor
    {:name  :my-interceptor
     :enter (fn [context]
              (as/go
                (do
                  (Thread/sleep 1000)
                  (assoc context :answer 42))))}))

我首先尝试了一个简单的测试:

(deftest my-test
  (is (= 42
         (:answer (io.pedestal.interceptor.chain/execute {} [my-interceptor])))))

这不起作用,因为 chain/execute returns nil 当它有异步拦截器时。我尝试了另一种解决方案,在测试后的拦截器中添加测试:

(deftest my-test
  (io.pedestal.interceptor.chain/execute
    {}
    [my-interceptor
     (io.pedestal.interceptor/interceptor
       {:name  :test
        :enter (fn [context]
                 (is (= 41 (:answer context))) ; should fail
                 context)})]))

但是这不起作用,因为测试在测试执行之前终止,因此成功......即使测试在一秒后失败:

Ran 1 test containing 0 assertions.
No failures.

FAIL in (my-test) (somefile_test.clj:49)
expected: (= 41 (:answer context))
  actual: (not (= 41 42))

实际上我的测试套件(使用 Kaocha)失败了,因为有一个 deftest 里面没有断言。

鉴于 chain/execute returns nil 而不是 chan,我无法将其包装在 as/<!! 中以阻塞直到它终止。

此时我卡住了。我可以做些什么来测试那种拦截器吗?

这种方法怎么样?

(require '[clojure.test :as test])
(require '[clojure.core.async :as async)

(test/deftest async-test []
 (let [c (async/chan)]
      (future (println "Running mah interceptors") (async/>!! c :done))
      (test/is (= :done (async/<!! c)))
      (async/close! c)))

在单独的线程中运行实际的拦截器代码。测试代码只需要 post 完成后 c 的内容。