The Crystal Programming Language Forum

[RFC] Async specs

I’ve been wanting to be able to run async specs for a while (seriously, I’ve been on this kick for a while). One of the things that bugs me about testing my Rails apps at work is that the specs can’t run in parallel and RSpec sits there using 10% CPU for way too long. :-)

I tested out async specs a while back in my neo4j driver but I had to remove it after some changes to Spec that my shortsighted monkeypatch didn’t work with. I knew at the time that was a possibility, but it was a fun experiment.

I posted PR 8944 this evening that provides this if you want to take a look. It adds it as an argument to describe / context and it, so that if you make a describe block async, all examples in it become async. This lets you work on making parts of your test suite concurrency-friendly.

For most database-backed apps, you’re probably going to be bottlenecked on your DB pool (this is a good thing — Postgres forks a new process for each connection and you don’t want 1000 processes fighting over your CPU), and you’ll probably want to ensure you either run all specs in an isolated transaction that gets rolled back or scope DB state to that spec (no User.count.should eq 1).

5 Likes