在midje
框架內測試的process-async
函數產生不一致的結果。大多數情況下,它按照預期進行檢查,但時常會在初始狀態(""
)中顯示out.json
。我在檢查前依靠async-blocker
函數等待process-async
。測試chan處理函數時結果不一致
我的方法有什麼問題?
(require '[[test-with-files.core :refer [with-files public-dir]])
(defn async-blocker [fun & args]
(let [chan-test (chan)]
(go (>! chan-test (apply fun args)))
(<!! chan-test)))
(defn process-async
[channel func]
(go-loop []
(when-let [response (<! channel)]
(func response)
(recur))))
(with-files [["/out.json" ""]]
(facts "About `process-async"
(let [channel (chan)
file (io/resource (str public-dir "/out.json"))
write #(spit file (str % "\n") :append true)]
(doseq [m ["m1" "m2"]] (>!! channel m))
(async-blocker process-async channel write)
(clojure.string/split-lines (slurp file)) => (just ["m1" "m2"] :in-any-order)
)
)
)