I’ve been working my way through Kevin Smith’s Erlang In Practice screencast series and decided to write some eunit tests for episode 6 (covering Mochiweb). The plan was to do a basic test which would register a nickname, then register the recipient which would also send back the waiting message and give me a chance to compare it with the original message.

Well, it took a LOT longer than it should have. The tests were inconsistent and would fail often but not predictably. Reworking the tests and underlying code didn’t yield much help. Debugging and tracing were not helpful either. There’s some timeouts in mochiweb or the http handling that are triggered when stepping through the code in the debugger. The get on the right track, I created a more specific test for the message_store.

Eventually it became clear that mnesia:start has some delay and that the unit tests run so fast that accessing the table becomes a race. If mnesia cannot see the table yet when we try to read the table, there’s an error that looks like this