How do I unit test clojure.core.async go macros?

前端 未结 3 1267
感情败类
感情败类 2021-02-13 09:12

I\'m trying to write unit tests when using core.async go macros. Writing the test naively, as follows, appears that the code inside the go blocks doesn\'t get executed.

3条回答
  •  时光说笑
    2021-02-13 09:35

    your test is finishing, and then failing. This happens more reliably if I put a sleep in and then make it fail:

    user> (deftest test1 []
            (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/

    here we can see it report that nothing fails, then it prints the failure message. We can fix this by explicitly coordinating the end of the test and that action finishing by, like most solutions in core.async, adding one more chan.

    user> (deftest test1 []
            (async/! chan "Hello"))
                 (async/ (clojure.test/run-tests)
    
    Testing user
    
    FAIL in (test1) (form-init8563497779572341831.clj:6)
    expected: (= (async/

    Which is equivalent to your solution using alts. I don't think your solution is hackey. With asynchronous code it's always required to pay attention to when things finish, even if you conciously decide to ignore the result.

提交回复
热议问题