interstar
interstar

Reputation: 27166

Clojure core.async in core.test

I have some core.async code with a pipeline of two chans and three nodes :

This code works as expected when I run it as a simple program. But when I copy and paste it to work within a unit-test, it freezes up.

My test code is roughly

(deftest a-test 
  (testing "blah"  
    (is (= (let [c1 (chan)
                 c2 (chan)                 
                 gen (make-generator c1)
                 filt (make-filter c1 c2)
                 result (collector c2 10) ]
              result)
           [0 2 4 6 8 10 12 14 16 18 20]))
))

where the generator creates a sequence of integers counting up from zero and the filter tests for evenness.

As far as I can tell, the filter is able to pull the first value from the c1, but is blocked waiting for a second value. Meanwhile, the generator is blocking while waiting to push its next value into c1.

But this doesn't happen when I run the code in a simple stand-alone program.

So, is there any reason that the unit-test framework might be interfering or causing problems with the threading management that core.async is providing? Is it possible to do unit-testing on async code like this?

I'm concerned that I'm not running the collector in any kind of go-block or go-loop so presumably it might be blocking the main thread. But equally, I presume I have to pull all the data back into the main thread eventually. And if not through that mechanism, how?

Upvotes: 0

Views: 484

Answers (1)

OlegTheCat
OlegTheCat

Reputation: 4513

While using blocking IO within go-blocks/go-loops isn't the best solution, thread macro may be better fit here. It will execute passed body on separate thread, so you may freely use blocking operations there.

Upvotes: 2

Related Questions