Testing OTP and race conditions

I have recently started working on a payment gateway (starting with PayPal) and I am occasionally getting failing tests which don’t fail if I re-run.

Foolishly I haven’t actually kept hold of the error message that occasionally crops up but I remember one of them was complaining that a process was already running for a particular PID. I will catch the message next time it happens.

I wondered if anyone would mind taking a look at my tests to see if there is something obvious I have missed (or misunderstood - I am still learning :smile:).

Repo - https://github.com/swelham/cashier

1 Like

I wasn’t able to reproduce it locally, and it’s hard to tell without a stack trace, but I think your problem might be here, coupled with this line, where you specify the registered name of the test gateway. Basically, each of your test gateways has the same registered name, and that can only work if the gateway from the previous test terminates. That will in fact happen, since each test runs in a separate process which terminates after it’s done, so all of its children will terminate as well.

However, the termination of the child process (in this case the test gateway) happens asynchronously. An exit signal needs to be delivered to the process which then stops with the same reason. So it perhaps the next test starts before the gateway started in the previous test has terminated.

You could try removing the :name option for your test gateway and see if that helps. Its just a guesswork at the moment. Seeing an error message & stack trace might help. However, glancing at your code, I think it’s the only place in tests that could cause this, so that’s what I’m basing my guess on :slight_smile:


Thanks @sasajuric, that makes complete sense! I have removed the name from the test gateway and tests are still passing, so time will tell if that has fully resolved the issue.

Would I be right in assuming that if I had to keep the :name option I could put my start_link inside of the setup_all callback? However I would guess this isn’t really desirable due possible side effects of state in the OTP process?

Also while you’re here, a big thanks for your book :thumbsup: I am a third of the way through and very much enjoying it!


Precisely! Moreover, you might still encounter the problem if different suites (i.e. cases) would start the same gateway from setup_all. In this case, you couldn’t run those cases concurrently (i.e.using async: true might lead to similar errors).

Thanks, happy to hear that :slight_smile:

1 Like