[英]Clojure core.async in core.test
I have some core.async code with a pipeline of two chans and three nodes : 我有一些带有两个通道和三个节点的管道的core.async代码:
a producer - function that puts values into chan1 with >!! 生产者-函数,使用> !!将值放入chan1 (it's not in a go-block but the function is called from inside a go-loop)
(它不在go块中,但从go循环内部调用了该函数)
a filter - another function that's not in a go-block but is called within a go-loop, which pulls items from chan1 (with <!!), does a test and if the test passes pushes them onto chan2 (with >!!) 过滤器-另一个不在go块中但在go循环中调用的函数,该函数从chan1(带<!!)中提取项目,进行测试,如果测试通过,则将其推入chan2(带> !!) )
a consumer - an ordinary loop that pulls n values of chan2 with 消费者-一个普通的循环,它使用以下命令拉取chan2的n个值
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. generator
创建一个从零开始计数的整数序列,然后filter
测试均匀性。
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. 据我所知,过滤器能够从c1中提取第一个值,但被阻塞,等待第二个值。 Meanwhile, the generator is blocking while waiting to push its next value into c1.
同时,在等待将其下一个值推入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? 因此,是否有任何原因导致单元测试框架可能干扰或引起core.async提供的线程管理问题? 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. 我担心我没有在任何类型的go-block或go-loop中运行收集器,因此大概会阻塞主线程。 But equally, I presume I have to pull all the data back into the main thread eventually.
但是同样,我想我必须最终将所有数据拉回到主线程中。 And if not through that mechanism, how?
如果不通过这种机制,怎么办?
While using blocking IO within go
-blocks/ go
-loops isn't the best solution, thread
macro may be better fit here. 虽然在
go
-blocks / go
-loops中使用阻塞IO并不是最佳解决方案,但thread
宏可能更适合此处。 It will execute passed body on separate thread, so you may freely use blocking operations there. 它将在单独的线程上执行传递的主体,因此您可以在那里自由使用阻塞操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.