George
George

Reputation: 7317

Is this an acceptable way to test core.async functionality?

Consider the following unit-test snippet:

(deftest async-test
 (testing "Testing some core.async functionality."
  (go (is (= (<! (go "val")) "val1")))))

Running the test yields:

Ran 1 tests containing 0 assertions. 0 failures, 0 errors.

FAIL in () (at repl:253:51) expected: (= (<! (go "val")) "val1")  
actual: (not (= "val" "val1"))

But this is weird because: (i) there is 1 assertion in this test, yet the test output says there are 0, and (ii) the test statement also says there were 0 failures, even though there was 1 failure.

I suppose this is because the (is...) assertion is in a go block. So my question is, is this the best way to go about unit testing with core.async functionality? Is there some best practice here I'm missing?

Upvotes: 2

Views: 213

Answers (1)

DanLebrero
DanLebrero

Reputation: 8593

This issue is not exclusive of core.async but to all multithreaded code. You need to make sure that the test assertions are run by the thread that starts the tests as that thread also collects and prints the results from all the tests.

In the case of Clojure, you just need to use the blocking <!! operator:

(deftest async-test
 (testing "Testing some core.async functionality."
  (is (= (<!! (go "val")) "val1")))

In the case of ClojureScript use the async macro. This has plenty of details about testing in ClojureScript

(deftest async-test
  (testing "Testing some core.async functionality."
    (async done
      (go
        (is (= (<! (go "val")) "val1"))
        (done)))))

Upvotes: 3

Related Questions