Testing core.async code ?

1,037 views
Skip to first unread message

Deniz Kurucu

unread,
Nov 30, 2013, 7:03:24 AM11/30/13
to clojur...@googlegroups.com
Hi,

I'm writing a library that uses core.async and trying to write some unit-tests for it. What is the way to test async code in Clojurescript ? My tests are finishing before my go blocks.

Thanks.

Mimmo Cosenza

unread,
Nov 30, 2013, 7:13:30 AM11/30/13
to clojur...@googlegroups.com
I never tried to unit testing a core.async based lib, but I think you could test it with clojurescript.test in the same way you would test in clj with clojure.test having the two libs almost the same API.

mimmo

--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to the Google Groups "ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.

signature.asc

Tim Gluz

unread,
Nov 30, 2013, 7:43:05 AM11/30/13
to clojur...@googlegroups.com
Hi, i've related question.

How i can test which type of buffer the channel is using? Is it possible to get the channel's buffer? And is there a way to get size of buffer?

Specifically I want to test does my constructor-function build correct channel.
Here's my current unit test: https://gist.github.com/timgluz/7718519
I used core.async's cljs test (https://github.com/clojure/core.async/blob/master/src/test/cljs/cljs/core/async/buffer_tests.cljs) as example, but this example uses plain buffers, but not channels.

No need to say, it fails for last 3 tests.

Timo

Gary Trakhman

unread,
Nov 30, 2013, 12:26:18 PM11/30/13
to clojur...@googlegroups.com
'My tests are finishing before my go blocks.'

Seems like the tests themselves need to have go-blocks.


--

Mark Mandel

unread,
Dec 1, 2013, 10:54:16 PM12/1/13
to clojur...@googlegroups.com
I'm having exactly the same issue at the moment. Did you ever find a pattern to solve this?

Mark

Tom Jack

unread,
Dec 1, 2013, 11:27:03 PM12/1/13
to clojur...@googlegroups.com
I don't think this can be solved without support from the testing library, since <!! is impossible in cljs.

clojurescript.test does not (yet..?) have the necessary support.

Mark Mandel

unread,
Dec 1, 2013, 11:56:10 PM12/1/13
to clojur...@googlegroups.com
I'm using Purnam to do my testing:

Which is an overlay on top of Jasmine - which does have support:

I'm just trying to work out how to apply it to cljs... I'll post something if I get it working. I think I have an idea.

Mark

Mark Mandel

unread,
Dec 2, 2013, 12:13:09 AM12/2/13
to clojur...@googlegroups.com
Using Purnam, this was actually quite easy once I read the Jasmine docs. Here's an example:

Think I'll fork Purnam and see if I can get a pull request going to include these as native, rather than having to skip over to the JS.

Mark

Travis Vachon

unread,
Dec 5, 2013, 5:03:23 PM12/5/13
to clojur...@googlegroups.com
I spent a few hours this afternoon down this rabbithole and came up frustrated. I've actually modified clojurescript.test to return channels in all the important places, which allows a test to be a big go block, and sort of works.

The big problem is that clojurescript.test uses `binding` all over the place to keep track of test failures and fiddle with test output, and because clojurescript doesn't support binding conveyance in go blocks (https://groups.google.com/forum/#!msg/clojure/xtxRVuSOLg4/2xj-UqO5NSAJ) this falls apart pretty quickly.

Does anyone know how much effort it'd be to add binding conveyance to clojurescript go blocks? Is there any existing work along these lines?

David Nolen

unread,
Dec 5, 2013, 5:07:06 PM12/5/13
to clojur...@googlegroups.com
I think hacking around the problem is counterproductive. Someone needs to put together a proper pull request together for clojurescript.test that makes it friendly for asynchronous testing and Chas can merge it in when he returns from vacation. We chatted a bit about the async issue at the Conj, he is aware of it.

Supporting binding conveyance in ClojureScript is a different subject with other considerations.

David

Travis Vachon

unread,
Dec 5, 2013, 5:15:57 PM12/5/13
to clojur...@googlegroups.com
Oh I agree re: hacking - the stuff I've come up with this afternoon is
basically backwards compatible with the documented usage of
clojurescript.test and feels pretty clean.

The hitch is that I can't see a way to make async testing feel
idiomatic to clojurescript.test without binding conveyance - I would
not be surprised if this is just a lack of imagination on my part
though - is there a specific approach you guys had in mind? I'd
definitely be interested in putting the pull request together, I just
don't see a solution without conveyance right now.

Thanks!

David Nolen

unread,
Dec 5, 2013, 5:18:34 PM12/5/13
to clojur...@googlegroups.com
Async testing frameworks exist that don't need dynamic binding. I don't see why clojurescript.test does either.

Travis Vachon

unread,
Dec 5, 2013, 5:20:13 PM12/5/13
to clojur...@googlegroups.com
got it - that's fair. I'll poke around and see how the others do it. thanks

David Nolen

unread,
Dec 5, 2013, 5:39:48 PM12/5/13
to clojur...@googlegroups.com
The solution is pretty simple, the test itself supplies a callback function to be invoked when it is complete.

Travis Vachon

unread,
Dec 5, 2013, 5:59:05 PM12/5/13
to clojur...@googlegroups.com
That's the easy part though. The part I'm having trouble imagining is
how `is` will work - right now the reporting functions all take
advantage of a `binding` in `test-ns` so any assertions inside a `go`
block don't get included in the final report.

David Nolen

unread,
Dec 5, 2013, 6:02:02 PM12/5/13
to clojur...@googlegroups.com
Got it, I can't offer any more advice. Chas is the man to talk to :)

Chas Emerick

unread,
Dec 11, 2013, 7:53:58 AM12/11/13
to clojur...@googlegroups.com
Thanks for working on the PoC, Travis. It's a solid approach, though
complicated by the bindings (as you've discovered) and the core.async
dep (which I want to avoid using at this level).

I've created https://github.com/cemerick/clojurescript.test/issues/34 to
track the development of async testing capabilities in
clojurescript.test. I have an approach in mind that should address the
objective and constraints listed there. As David hinted, the result
will not be unlike other JavaScript testing frameworks, though some
cleverness will be needed to keep clojurescript.test acting and feeling
like clojure.test as much as possible…

Cheers,

- Chas

Nikita Beloglazov

unread,
Jan 18, 2014, 11:40:41 AM1/18/14
to clojur...@googlegroups.com
I've implemented my vision of async support in clojurescript.test. It doesn't depend on core.async and works with any async operations like ajax or setTimeout. Basically it adds 2 functions: acquire-lock and release-lock that allow current test to "pause" until some async operation is completed.

I tried to preserve same API compatibility. If async features are not used everything should work as before. With async tests some features doesn't work very well by design: fixtures, testing scopes and probably some other.

Here is pull request: https://github.com/cemerick/clojurescript.test/pull/36

I'd be happy to hear some feedback. Especially how to add async API to clojurescript.test by preserving compatibility with classic clojure.test. I couldn't think of something better than adding 2 functions for locking/unlocking test execution.

Thank you,
Nikita

Chas Emerick

unread,
Jan 21, 2014, 3:23:33 PM1/21/14
to clojur...@googlegroups.com
Hi Nikita,

Just FYI, your PR is in the queue, as it were. Thank you very much for
taking a whack at the problem.

- Chas
Reply all
Reply to author
Forward
0 new messages