我如何单元测试clojure.core.async去宏?

我如何单元测试clojure.core.async去宏?,第1张

我在尝试使用core.async go宏时编写单元测试。如下编写测试,看起来go块内的代码不会被执行。

(ns app.core-test
  (:require [clojure.test :refer :all]
            [clojure.core.async :as async]))

(deftest test1 []
  (let [chan (async/chan)]
    (async/go
     (is (= (async/<! chan) "Hello")))
    (async/go
     (async/>! chan "Hello"))))

我已经设法让以下工作,但它非常hacky。

(deftest test1 []
  (let [result (async/chan)
        chan (async/chan)]
    (async/go
     (is (= (async/<! chan) "Hello"))
     (async/>! result true))
    (async/go
     (async/>! chan "Hello"))
    (async/alts!! [result (async/timeout 10000)])))

有关我如何正确执行此操作的任何建议吗?

最佳答案:

3 个答案:

答案 0 :(得分:22)

测试是同步执行的,所以如果你去异步,测试运行器就不会。在Clojure中,您需要通过<!!阻止测试运行器,在ClojureScript中您必须返回异步测试对象。这是我在所有异步CLJC测试中使用的通用辅助函数:

(defn test-async
  "Asynchronous test awaiting ch to produce a value or close."
  [ch]
  #?(:clj
     (<!! ch)
     :cljs
     (async done
       (take! ch (fn [_] (done))))))

你的测试使用它,CLJC兼容并且看起来不那么“hacky”:

(deftest test1
  (let [ch (chan)]
    (go (>! ch "Hello"))
    (test-async
      (go (is (= "Hello" (<! ch)))))))

优良作法是断言测试解除阻塞,特别是在测试驱动开发期间,您要避免锁定测试运行器。此外,锁定是异步编程失败的常见原因,因此对其进行测试是非常合理的。

为此,我写了一个类似于你的超时事件的助手:

(defn test-within
  "Asserts that ch does not close or produce a value within ms. Returns a
  channel from which the value can be taken."
  [ms ch]
  (go (let [t (timeout ms)
            [v ch] (alts! [ch t])]
        (is (not= ch t)
            (str "Test should have finished within " ms "ms."))
        v)))

您可以使用它来编写测试,如:

(deftest test1
  (let [ch (chan)]
    (go (>! ch "Hello"))
    (test-async
      (test-within 1000
        (go (is (= "Hello" (<! ch)))))))

答案 1 :(得分:4)

你的测试正在完成,然后失败。如果我进入睡眠然后使其失败,这种情况会更可靠地发生:

user> (deftest test1 []
        (async/<!!
         (let [chan (async/chan)]
           (async/go
             (async/go
               (async/<! (async/timeout 1000))
               (is (= (async/<! chan) "WRONG")))
             (async/go
               (async/>! chan "Hello"))))))
#'user/test1
user> (clojure.test/run-tests)

Testing user

Ran 1 tests containing 0 assertions.
0 failures, 0 errors.
{:test 1, :pass 0, :fail 0, :error 0, :type :summary}
user> 
FAIL in (test1) (form-init8563497779572341831.clj:5)
expected: (= (async/<! chan) "WRONG")
  actual: (not (= "Hello" "WRONG"))

在这里我们可以看到它报告没有任何失败,然后它打印失败消息。我们可以通过明确地协调测试的结束和完成的操作来解决这个问题,就像在core.async中的大多数解决方案一样,再添加一个chan。

user> (deftest test1 []
        (async/<!!
         (let [all-done-chan (async/chan)
               chan (async/chan)]
           (async/go
             (async/go
               (async/<! (async/timeout 1000))
               (is (= (async/<! chan) "WRONG"))
               (async/close! all-done-chan ))
             (async/go
               (async/>! chan "Hello"))
             (async/<! all-done-chan)))))
#'user/test1
user> (clojure.test/run-tests)

Testing user

FAIL in (test1) (form-init8563497779572341831.clj:6)
expected: (= (async/<! chan) "WRONG")
  actual: (not (= "Hello" "WRONG"))

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
{:test 1, :pass 0, :fail 1, :error 0, :type :summary}

这相当于使用alts的解决方案。我不认为你的解决方案是hackey。使用异步代码时,即使您有意识地决定忽略结果,也始终需要注意事情的完成时间。

答案 2 :(得分:0)

我使用类似于Leon的方法,但没有额外的去块:

(defn <!!?
  "Reads from chan synchronously, waiting for a given maximum of milliseconds.
  If the value does not come in during that period, returns :timed-out. If
  milliseconds is not given, a default of 1000 is used."
  ([chan]
   (<!!? chan 1000))
  ([chan milliseconds]
   (let [timeout (async/timeout milliseconds)
         [value port] (async/alts!! [chan timeout])]
     (if (= chan port)
       value
       :timed-out))))

您可以简单地将其用作:

(is (= 42 (<!!? result-chan)))

大多数时候,我只是想从频道中读取价值而不会有任何额外的麻烦。

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复