I’ve got a supervised GenServer that loads some data in a post-init message after its start_link completes. Once it completes, it uses gproc to send a message to other processes that have registered the “topic” as a property, which I’ve used to write a test for it that uses the completion message in order to test that the data is loaded properly. My question is whether this is the right way to go about it, or whether there’s a better way I’m missing. Right now the only function of the message is to provide a hook for the test, which doesn’t feel right to me, but its the path that requires the lowest degree of coupling that I can see, and can conceivably be useful in the future as the application expands.
Does this feel like the right solution to anyone else?
FWIW I used that very approach to provide hooks for tests, and don’t have a guilty conscience about it
If you really want to avoid polluting the prod code with this, you could setup a trace from your test process to get a message when the post-init fun has been invoked, and a message with a return value of the function. That would allow the test process to assert that post-init fun has been invoked with proper parameters, and returned a proper value without changing the prod code. I don’t have an example right now, but I used it once, and if you insist I can try to compose a simple example (can’t commit on time though).
However, as I said, I think firing an event (message) and asserting on it in a test is a low cost solution for the problem, so it’s the one I tend to use most often.
Consider my guilty conscience resolved. I think I came across some code like you describe, but honestly it felt like a similar amount of overkill with a heavier conceptual load. Thanks for validating the idea!